src/ui/canvas.cpp

Sun, 06 Mar 2022 10:32:54 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 06 Mar 2022 10:32:54 +0200
changeset 171
b497f5e66749
parent 170
9b655f6fe5a1
child 172
50f055543ff6
permissions
-rw-r--r--

add missing file

47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include <QMouseEvent>
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
2 #include <QPainter>
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 #include "canvas.h"
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 Canvas::Canvas(
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 Model* model,
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 DocumentManager* documents,
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 const ldraw::ColorTable& colorTable,
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 QWidget* parent) :
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
10 PartRenderer{model, documents, colorTable, parent}
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 {
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 this->setMouseTracking(true);
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 }
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
15 /**
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
16 * @brief Handles a change of selection
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
17 * @param selectedIds IDs of objects to select
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
18 * @param deselectedIds IDs of objects to deselect.
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
19 */
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 71
diff changeset
20 void Canvas::handleSelectionChange(const QSet<ldraw::id_t>& selectedIds, const QSet<ldraw::id_t>& deselectedIds)
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
21 {
52
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
22 Q_ASSERT(not selectedIds.contains(ldraw::NULL_ID));
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
23 this->selection.subtract(deselectedIds);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
24 this->selection.unite(selectedIds);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
25 this->compiler->setSelectedObjects(this->selection);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
26 this->update();
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
27 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
28
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
29 /**
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
30 * @brief Updates vertex rendering
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
31 * @param document Document to get vertices from
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
32 */
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
33 void Canvas::rebuildVertices(Document* document)
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
34 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
35 if (this->vertexProgram.has_value())
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
36 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
37 this->vertexProgram->build(document);
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
38 this->update();
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
39 }
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
40 }
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
41
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 void Canvas::mouseMoveEvent(QMouseEvent* event)
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 {
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 71
diff changeset
44 const ldraw::id_t id = this->pick(event->pos());
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
45 this->highlighted = id;
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
46 this->totalMouseMove += (event->pos() - this->lastMousePosition).manhattanLength();
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
47 this->worldPosition = this->screenToModelCoordinates(event->pos(), this->gridPlane);
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
48 if (this->worldPosition.has_value())
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 52
diff changeset
49 {
65
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
50 /*
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
51 * Snap the position to grid. This procedure is basically the "change of basis" and almost follows the
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
52 * A⁻¹ × M × A formula which is used to perform a transformation in some other coordinate system, except
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
53 * we actually use the inverted matrix first and the regular one last to perform the transformation of
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
54 * grid coordinates in our XY coordinate system. Also, we're rounding the coordinates which is obviously
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
55 * not a linear transformation, but fits the pattern anyway.
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
56 */
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
57 // First transform the coordinates to the XY plane...
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
58 this->worldPosition = glm::inverse(this->gridMatrix) * glm::vec4{*this->worldPosition, 1};
65
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
59 // Then round the coordinates to integer precision...
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
60 this->worldPosition = glm::round(*this->worldPosition);
65
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
61 // And finally transform it back to grid coordinates by transforming it with the
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
62 // grid matrix.
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
63 this->worldPosition = this->gridMatrix * glm::vec4{*this->worldPosition, 1};
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
64 }
121
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
65 Q_EMIT this->mouseMove(this, event);
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 PartRenderer::mouseMoveEvent(event);
121
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
67 this->update();
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 }
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
69
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
70 void Canvas::mousePressEvent(QMouseEvent* event)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
71 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
72 this->totalMouseMove = 0;
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
73 this->lastMousePosition = event->pos();
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
74 PartRenderer::mousePressEvent(event);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
75 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
76
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
77 void Canvas::mouseReleaseEvent(QMouseEvent* event)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
78 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
79 if (this->totalMouseMove < (2.0 / sqrt(2)) * 5.0)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
80 {
121
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
81 Q_EMIT this->mouseClick(this, event);
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
82 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
83 PartRenderer::mouseReleaseEvent(event);
121
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
84 this->update();
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
85 }
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
86
61
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
87 void Canvas::initializeGL()
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
88 {
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
89 // We first create the grid program and connect everything and only then call the part renderer's initialization
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
90 // functions so that when initialization sets up, the signals also set up the matrices on our side.
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
91 this->gridProgram.emplace(this);
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
92 this->gridProgram->initialize();
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
93 this->axesProgram.emplace(this);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
94 this->axesProgram->initialize();
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
95 this->vertexProgram.emplace(this);
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
96 this->vertexProgram->initialize();
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
97 for (AbstractBasicShaderProgram* program : {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
98 static_cast<AbstractBasicShaderProgram*>(&*this->gridProgram),
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
99 static_cast<AbstractBasicShaderProgram*>(&*this->axesProgram),
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
100 static_cast<AbstractBasicShaderProgram*>(&*this->vertexProgram),
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
101 })
61
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
102 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
103 connect(this, &PartRenderer::projectionMatrixChanged,
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
104 program, &AbstractBasicShaderProgram::setProjectionMatrix);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
105 connect(this, &PartRenderer::modelMatrixChanged,
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
106 program, &AbstractBasicShaderProgram::setModelMatrix);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
107 connect(this, &PartRenderer::viewMatrixChanged,
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
108 program, &AbstractBasicShaderProgram::setViewMatrix);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
109 }
165
f6eab2bd46c2 fixed the grid not being black on startup if settings has bright background color
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
110 connect(this, &PartRenderer::renderPreferencesChanged, this, &Canvas::updateCanvasRenderPreferences);
61
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
111 PartRenderer::initializeGL();
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
112 // Set up XZ grid matrix
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
113 this->setGridMatrix({{1, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 1}});
165
f6eab2bd46c2 fixed the grid not being black on startup if settings has bright background color
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
114 this->updateCanvasRenderPreferences();
61
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
115 }
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
116
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
117 void Canvas::paintGL()
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
118 {
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
119 PartRenderer::paintGL();
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
120 if (this->renderPreferences.style != gl::RenderStyle::PickScene)
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
121 {
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
122 // Render axes
170
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
123 if (this->renderPreferences.drawAxes)
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
124 {
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
125 glLineWidth(5);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
126 glEnable(GL_LINE_SMOOTH);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
127 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
128 this->axesProgram->draw();
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
129 glDisable(GL_LINE_SMOOTH);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
130 }
119
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
131 // Render vertices
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
132 {
120
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
133 glLineWidth(1.5);
119
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
134 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
135 glEnable(GL_LINE_SMOOTH);
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
136 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
137 this->vertexProgram->draw();
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
138 glDisable(GL_LINE_SMOOTH);
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
139 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
140 }
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
141 // Render grid
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
142 {
157
869fe95c4e5e improve grid rendering
Teemu Piippo <teemu@hecknology.net>
parents: 129
diff changeset
143 glLineWidth(1);
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
144 glEnable(GL_BLEND);
157
869fe95c4e5e improve grid rendering
Teemu Piippo <teemu@hecknology.net>
parents: 129
diff changeset
145 glLineStipple(1, 0x8888);
869fe95c4e5e improve grid rendering
Teemu Piippo <teemu@hecknology.net>
parents: 129
diff changeset
146 glEnable(GL_LINE_STIPPLE);
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
147 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
148 this->gridProgram->draw();
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
149 glDisable(GL_BLEND);
157
869fe95c4e5e improve grid rendering
Teemu Piippo <teemu@hecknology.net>
parents: 129
diff changeset
150 glDisable(GL_LINE_STIPPLE);
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
151 }
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
152 if (this->worldPosition.has_value())
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
153 {
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
154 QPainter painter{this};
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
155 painter.setRenderHint(QPainter::Antialiasing);
166
8857351912d0 Fix rendering of cursor coordinates on bright background
Teemu Piippo <teemu@hecknology.net>
parents: 165
diff changeset
156 painter.setPen(this->isDark ? Qt::white : Qt::black);
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
157 painter.setBrush(Qt::green);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
158 const QPointF pos = this->modelToScreenCoordinates(*this->worldPosition);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
159 painter.drawEllipse(pos, 5, 5);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
160 painter.drawText(pos + QPointF{5, 5}, vectorToString(*this->worldPosition));
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
161 }
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
162 {
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
163 QPainter painter{this};
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
164 painter.setRenderHint(QPainter::Antialiasing);
170
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
165 if (this->renderPreferences.drawAxes)
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
166 {
170
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
167 this->renderAxesLabels(painter);
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
168 }
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
169 if (this->overpaintCallback != nullptr)
105
6ca6e8c647d4 added preview layer code and fixed build warnings
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
170 {
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
171 this->overpaintCallback(this, &painter);
105
6ca6e8c647d4 added preview layer code and fixed build warnings
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
172 }
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
173 }
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
174 }
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
175 }
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
176
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
177 /**
170
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
178 * @brief Renders labels such as +x at the ends of axes at the screen
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
179 * @param painter
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
180 */
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
181 void Canvas::renderAxesLabels(QPainter& painter)
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
182 {
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
183 QFont font;
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
184 //font.setStyle(QFont::StyleItalic);
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
185 painter.setFont(font);
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
186 QFontMetrics fontMetrics{font};
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
187 const auto renderText = [&](const QString& text, const geom::PointOnRectagle& intersection)
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
188 {
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
189 QPointF position = toQPointF(intersection.position);
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
190 const geom::RectangleSide side = intersection.side;
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
191 switch (side)
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
192 {
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
193 case geom::RectangleSide::Top:
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
194 position += QPointF{0, static_cast<qreal>(fontMetrics.ascent())};
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
195 break;
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
196 case geom::RectangleSide::Left:
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
197 break;
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
198 case geom::RectangleSide::Bottom:
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
199 position += QPointF{0, static_cast<qreal>(-fontMetrics.descent())};
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
200 break;
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
201 case geom::RectangleSide::Right:
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
202 position += QPointF{static_cast<qreal>(-fontMetrics.horizontalAdvance(text)), 0};
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
203 break;
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
204 }
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
205 painter.drawText(position, text);
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
206 };
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
207 const QRectF box {QPointF{0, 0}, sizeToSizeF(this->size())};
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
208 const QPointF p1 = this->modelToScreenCoordinates(glm::vec3{0, 0, 0});
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
209 static const struct
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
210 {
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
211 QString text;
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
212 glm::vec3 direction;
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
213 } directions[] =
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
214 {
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
215 {"+𝑥", {1, 0, 0}},
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
216 {"-𝑥", {-1, 0, 0}},
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
217 {"+𝑦", {0, 1, 0}},
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
218 {"-𝑦", {0, -1, 0}},
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
219 {"+𝑧", {0, 0, 1}},
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
220 {"-𝑧", {0, 0, -1}},
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
221 };
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
222 for (const auto& axis : directions)
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
223 {
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
224 const QPointF x_p = this->modelToScreenCoordinates(axis.direction);
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
225 const auto intersection = geom::rayRectangleIntersection(
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
226 geom::rayFromPoints(toVec2(p1), toVec2(x_p)),
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
227 box);
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
228 if (intersection.has_value())
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
229 {
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
230 renderText(axis.text, *intersection);
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
231 }
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
232 }
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
233 }
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
234
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
235 /**
164
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
236 * @brief Draws a polyline to where the specified vector of 3D points would appear on the screen.
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
237 * @param painter Painter to use to draw with
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
238 * @param points 3D points to render
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
239 */
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
240 void Canvas::drawWorldPolyline(QPainter *painter, const std::vector<glm::vec3> &points)
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
241 {
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
242 painter->drawPolyline(QPolygonF{this->convertWorldPointsToScreenPoints(points)});
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
243 }
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
244
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
245 /**
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
246 * @brief Draws a polygon to where the specified vector of 3D points would appear on the screen.
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
247 * @param painter Painter to use to draw with
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
248 * @param points 3D points to render
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
249 */
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
250 void Canvas::drawWorldPolygon(QPainter* painter, const std::vector<glm::vec3> &points)
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
251 {
164
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
252 painter->drawPolygon(QPolygonF{this->convertWorldPointsToScreenPoints(points)});
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
253 }
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
254
168
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 166
diff changeset
255 Winding Canvas::worldPolygonWinding(const std::vector<glm::vec3> &points) const
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 166
diff changeset
256 {
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 166
diff changeset
257 return geom::winding(QPolygonF{this->convertWorldPointsToScreenPoints(points)});
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 166
diff changeset
258 }
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 166
diff changeset
259
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
260 /**
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
261 * @brief Gets the current position of the cursor in the model
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
262 * @return 3D vector
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
263 */
110
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
264 const std::optional<glm::vec3>& Canvas::getWorldPosition() const
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
265 {
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
266 return this->worldPosition;
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
267 }
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
268
128
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
269 /**
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
270 * @brief Adjusts the grid to be so that it is perpendicular to the camera.
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
271 */
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
272 void Canvas::adjustGridToView()
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
273 {
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
274 const glm::vec3 cameraDirection = this->cameraVector();
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
275 const glm::vec3 vector_x = glm::normalize(this->gridMatrix * glm::vec4{1, 0, 0, 1});
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
276 const glm::vec3 vector_y = glm::normalize(this->gridMatrix * glm::vec4{0, 1, 0, 1});
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
277 const float angle_x = std::abs(glm::dot(vector_x, cameraDirection));
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
278 const float angle_y = std::abs(glm::dot(vector_y, cameraDirection));
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
279 if (angle_x < angle_y)
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
280 {
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
281 this->setGridMatrix(glm::rotate(this->gridMatrix, PI<float> / 2, glm::vec3{1, 0, 0}));
128
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
282 }
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
283 else
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
284 {
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
285 this->setGridMatrix(glm::rotate(this->gridMatrix, PI<float> / 2, glm::vec3{0, 1, 0}));
128
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
286 }
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
287 this->update();
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
288 }
7c834fe36b25 Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
289
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
290 /**
169
6da096930534 Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
291 * @returns the ids of the currently selected objects
6da096930534 Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
292 */
6da096930534 Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
293 const QSet<ldraw::id_t> Canvas::selectedObjects() const
6da096930534 Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
294 {
6da096930534 Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
295 return this->selection;
6da096930534 Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
296 }
6da096930534 Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
297
6da096930534 Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
298 /**
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
299 * @brief Paints a circle at where @c worldPoint is located on the screen.
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
300 * @param painter Painter to use to render
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
301 * @param worldPoint Point to render
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
302 */
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
303 void Canvas::drawWorldPoint(QPainter* painter, const glm::vec3& worldPoint) const
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
304 {
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
305 const QPointF center = this->modelToScreenCoordinates(worldPoint);
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
306 painter->drawEllipse(geom::inscribe(geom::CircleF{center, 5}));
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
307 }
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
308
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
309 /**
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
310 * @brief Changes the grid matrix to the one specified. Updates relevant member variables.
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
311 * @param newMatrix New matrix to use
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
312 */
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
313 void Canvas::setGridMatrix(const glm::mat4& newMatrix)
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
314 {
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
315 this->gridMatrix = newMatrix;
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
316 const geom::Triangle triangle {
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
317 this->gridMatrix * glm::vec4{0, 0, 0, 1},
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
318 this->gridMatrix * glm::vec4{1, 0, 0, 1},
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
319 this->gridMatrix * glm::vec4{0, 1, 0, 1},
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
320 };
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
321 this->gridPlane = geom::planeFromTriangle(triangle);
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
322 this->gridProgram->setGridMatrix(this->gridMatrix);
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
323 }
66
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
324
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
325 /**
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
326 * @brief Gets the current camera vector, i.e. the vector from the camera to the grid origin.
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
327 * @return vector
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
328 */
67
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
329 glm::vec3 Canvas::cameraVector() const
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
330 {
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
331 // Find out where the grid is projected on the screen
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
332 const QPoint gridOrigin2d = pointFToPoint(this->modelToScreenCoordinates(this->gridPlane.anchor));
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
333 // Find out which direction the camera is looking at the grid origin in 3d
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
334 return glm::normalize(this->cameraLine(gridOrigin2d).direction);
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
335 }
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
336
66
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
337 /**
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
338 * @brief Calculates whether the screen is perpendicular to the current grid
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
339 * @return bool
66
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
340 */
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
341 bool Canvas::isGridPerpendicularToScreen(float threshold) const
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
342 {
67
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
343 const glm::vec3 cameraDirection = this->cameraVector();
66
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
344 // Compute the dot product. The parameters given are:
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
345 // - the normal of the grid plane, which is the vector from the grid origin perpendicular to the grid
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
346 // - the direction of the camera looking at the grid, which is the inverse of the vector from the grid
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
347 // origin towards the camera
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
348 // If the dot product between these two vectors is 0, the grid normal is perpendicular to the camera vector
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
349 // and the grid is perpendicular to the screen.
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
350 const float dot = glm::dot(glm::normalize(this->gridPlane.normal), glm::normalize(cameraDirection));
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
351 return std::abs(dot) < threshold;
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
352 }
105
6ca6e8c647d4 added preview layer code and fixed build warnings
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
353
168
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 166
diff changeset
354 QVector<QPointF> Canvas::convertWorldPointsToScreenPoints(const std::vector<glm::vec3> &worldPoints) const
164
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
355 {
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
356 QVector<QPointF> points2d;
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
357 points2d.reserve(worldPoints.size());
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
358 for (const glm::vec3& point : worldPoints)
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
359 {
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
360 points2d.push_back(this->modelToScreenCoordinates(point));
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
361 }
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
362 return points2d;
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
363 }
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 157
diff changeset
364
165
f6eab2bd46c2 fixed the grid not being black on startup if settings has bright background color
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
365 void Canvas::updateCanvasRenderPreferences()
f6eab2bd46c2 fixed the grid not being black on startup if settings has bright background color
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
366 {
166
8857351912d0 Fix rendering of cursor coordinates on bright background
Teemu Piippo <teemu@hecknology.net>
parents: 165
diff changeset
367 this->isDark = luma(this->renderPreferences.backgroundColor) < 0.25;
165
f6eab2bd46c2 fixed the grid not being black on startup if settings has bright background color
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
368 if (this->gridProgram.has_value())
f6eab2bd46c2 fixed the grid not being black on startup if settings has bright background color
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
369 {
166
8857351912d0 Fix rendering of cursor coordinates on bright background
Teemu Piippo <teemu@hecknology.net>
parents: 165
diff changeset
370 this->gridProgram->setGridColor(this->isDark ? Qt::white : Qt::black);
165
f6eab2bd46c2 fixed the grid not being black on startup if settings has bright background color
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
371 }
f6eab2bd46c2 fixed the grid not being black on startup if settings has bright background color
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
372 }
f6eab2bd46c2 fixed the grid not being black on startup if settings has bright background color
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
373
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
374 /**
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
375 * @brief Clears the selection.
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
376 */
107
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
377 void Canvas::clearSelection()
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
378 {
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
379 this->selection.clear();
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
380 this->compiler->setSelectedObjects(this->selection);
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
381 Q_EMIT selectionChanged(this->selection);
107
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
382 this->update();
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
383 }
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
384
129
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
385 /**
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
386 * @brief Adds an object to selection.
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
387 * @param id ID of object to add
f35843351601 Add documentation
Teemu Piippo <teemu@hecknology.net>
parents: 128
diff changeset
388 */
107
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
389 void Canvas::addToSelection(ldraw::id_t id)
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
390 {
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
391 this->selection.insert(id);
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
392 this->compiler->setSelectedObjects(this->selection);
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
393 Q_EMIT selectionChanged(this->selection);
107
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
394 this->update();
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
395 }
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
396
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
397 void Canvas::setOverpaintCallback(Canvas::OverpaintCallback fn)
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
398 {
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
399 this->overpaintCallback = fn;
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
400 }

mercurial