src/ui/canvas.cpp

Wed, 28 Jul 2021 08:23:09 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 28 Jul 2021 08:23:09 +0300
changeset 119
24275a4064f4
parent 118
8e1c9f18ae15
child 120
8c9fff699241
permissions
-rw-r--r--

update

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
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 71
diff changeset
15 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
16 {
52
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
17 Q_ASSERT(not selectedIds.contains(ldraw::NULL_ID));
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
18 this->selection.subtract(deselectedIds);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
19 this->selection.unite(selectedIds);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
20 this->compiler->setSelectedObjects(this->selection);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
21 this->update();
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
22 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
23
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
24 void Canvas::rebuildVertices(Document* document)
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
25 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
26 if (this->vertexProgram.has_value())
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
27 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
28 this->vertexProgram->build(document);
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
29 this->update();
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
30 }
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
31 }
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
32
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 void Canvas::mouseMoveEvent(QMouseEvent* event)
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 {
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 71
diff changeset
35 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
36 this->highlighted = id;
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
37 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
38 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
39 if (this->worldPosition.has_value())
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 52
diff changeset
40 {
65
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
41 /*
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
42 * 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
43 * 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
44 * 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
45 * 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
46 * not a linear transformation, but fits the pattern anyway.
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
47 */
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
48 // 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
49 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
50 // Then round the coordinates to integer precision...
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
51 this->worldPosition = glm::round(*this->worldPosition);
65
87c906545fc3 document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
52 // 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
53 // grid matrix.
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
54 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
55 }
66
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
56 /*
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
57 if (this->worldPosition.has_value())
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
58 {
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
59 this->newStatusText("Position: (%1, %2, %3)"_q
61
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
60 .arg(toDouble(this->worldPosition->x))
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
61 .arg(toDouble(this->worldPosition->y))
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
62 .arg(toDouble(this->worldPosition->z)));
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 52
diff changeset
63 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 52
diff changeset
64 else
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 52
diff changeset
65 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 52
diff changeset
66 this->newStatusText("Position: <none>"_q);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 52
diff changeset
67 }
66
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
68 */
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
69 // use a relatively high threshold so that we know when the grid is somewhat perpendicular so we can
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
70 // automatically change it properly
67
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
71 if (isGridPerpendicularToScreen(0.03f))
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
72 {
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
73 const glm::vec3 cameraDirection = this->cameraVector();
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
74 const glm::vec3 vector_x = glm::normalize(this->gridMatrix * glm::vec4{1, 0, 0, 1});
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
75 const glm::vec3 vector_y = glm::normalize(this->gridMatrix * glm::vec4{0, 1, 0, 1});
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
76 const float angle_x = std::abs(glm::dot(vector_x, cameraDirection));
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
77 const float angle_y = std::abs(glm::dot(vector_y, cameraDirection));
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
78 if (angle_x < angle_y)
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
79 {
96
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 79
diff changeset
80 this->gridMatrix = glm::rotate(this->gridMatrix, PI<float> / 2, glm::vec3{1, 0, 0});
67
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
81 }
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
82 else
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
83 {
96
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 79
diff changeset
84 this->gridMatrix = glm::rotate(this->gridMatrix, PI<float> / 2, glm::vec3{0, 1, 0});
67
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
85 }
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
86 this->updateGridMatrix();
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
87 this->update();
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
88 }
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 110
diff changeset
89 Q_EMIT this->mouseMove(this);
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 PartRenderer::mouseMoveEvent(event);
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 }
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
92
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
93 void Canvas::mousePressEvent(QMouseEvent* event)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
94 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
95 this->totalMouseMove = 0;
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
96 this->lastMousePosition = event->pos();
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
97 PartRenderer::mousePressEvent(event);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
98 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
99
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
100 void Canvas::mouseReleaseEvent(QMouseEvent* event)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
101 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
102 if (this->totalMouseMove < (2.0 / sqrt(2)) * 5.0)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
103 {
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 110
diff changeset
104 Q_EMIT this->mouseClick(this);
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
105 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
106 PartRenderer::mouseReleaseEvent(event);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
107 }
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
108
61
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
109 void Canvas::initializeGL()
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
110 {
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
111 // 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
112 // 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
113 this->gridProgram.emplace(this);
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
114 this->gridProgram->initialize();
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
115 this->axesProgram.emplace(this);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
116 this->axesProgram->initialize();
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
117 this->vertexProgram.emplace(this);
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
118 this->vertexProgram->initialize();
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
119 for (AbstractBasicShaderProgram* program : {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
120 static_cast<AbstractBasicShaderProgram*>(&*this->gridProgram),
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
121 static_cast<AbstractBasicShaderProgram*>(&*this->axesProgram),
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
122 static_cast<AbstractBasicShaderProgram*>(&*this->vertexProgram),
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
123 })
61
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
124 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
125 connect(this, &PartRenderer::projectionMatrixChanged,
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
126 program, &AbstractBasicShaderProgram::setProjectionMatrix);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
127 connect(this, &PartRenderer::modelMatrixChanged,
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
128 program, &AbstractBasicShaderProgram::setModelMatrix);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
129 connect(this, &PartRenderer::viewMatrixChanged,
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
130 program, &AbstractBasicShaderProgram::setViewMatrix);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
131 }
61
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
132 connect(this, &PartRenderer::renderPreferencesChanged, [&]()
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
133 {
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
134 if (this->gridProgram.has_value())
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
135 {
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
136 const bool isDark = luma(this->renderPreferences.backgroundColor) < 0.25;
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
137 this->gridProgram->setGridColor(isDark ? Qt::white : Qt::black);
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
138 }
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
139 });
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
140 PartRenderer::initializeGL();
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
141 // Set up XZ grid matrix
68
ddb07bb6840c default to XZ
Teemu Piippo <teemu@hecknology.net>
parents: 67
diff changeset
142 this->gridMatrix = glm::mat4{
ddb07bb6840c default to XZ
Teemu Piippo <teemu@hecknology.net>
parents: 67
diff changeset
143 {1, 0, 0, 0},
ddb07bb6840c default to XZ
Teemu Piippo <teemu@hecknology.net>
parents: 67
diff changeset
144 {0, 0, 1, 0},
ddb07bb6840c default to XZ
Teemu Piippo <teemu@hecknology.net>
parents: 67
diff changeset
145 {0, 1, 0, 0},
ddb07bb6840c default to XZ
Teemu Piippo <teemu@hecknology.net>
parents: 67
diff changeset
146 {0, 0, 0, 1}
ddb07bb6840c default to XZ
Teemu Piippo <teemu@hecknology.net>
parents: 67
diff changeset
147 };
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
148 this->updateGridMatrix();
61
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
149 }
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
150
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
151 void Canvas::paintGL()
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
152 {
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
153 PartRenderer::paintGL();
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
154 if (this->renderPreferences.style != gl::RenderStyle::PickScene)
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
155 {
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
156 // Render axes
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
157 {
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
158 glLineWidth(5);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
159 glEnable(GL_LINE_SMOOTH);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
160 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
161 this->axesProgram->draw();
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
162 glDisable(GL_LINE_SMOOTH);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
163 }
119
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
164 // Render vertices
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
165 {
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
166 glLineWidth(2);
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
167 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
168 glEnable(GL_LINE_SMOOTH);
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
169 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
170 this->vertexProgram->draw();
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
171 glDisable(GL_LINE_SMOOTH);
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
172 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
173 }
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
174 // Render grid
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
175 {
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
176 glEnable(GL_BLEND);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
177 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
178 this->gridProgram->draw();
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
179 glDisable(GL_BLEND);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
180 }
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
181 if (this->worldPosition.has_value())
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
182 {
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
183 QPainter painter{this};
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
184 painter.setRenderHint(QPainter::Antialiasing);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
185 painter.setPen(Qt::black);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
186 painter.setBrush(Qt::green);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
187 const QPointF pos = this->modelToScreenCoordinates(*this->worldPosition);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
188 painter.drawEllipse(pos, 5, 5);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
189 painter.setPen(Qt::white);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
190 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
191 }
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
192 {
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
193 QPainter painter{this};
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
194 painter.setRenderHint(QPainter::Antialiasing);
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
195 QFont font;
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
196 //font.setStyle(QFont::StyleItalic);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
197 painter.setFont(font);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
198 QFontMetrics fontMetrics{font};
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
199 const auto renderText = [&](const QString& text, const geom::PointOnRectagle& intersection)
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
200 {
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
201 QPointF position = toQPointF(intersection.position);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
202 const geom::RectangleSide side = intersection.side;
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
203 switch (side)
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
204 {
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
205 case geom::RectangleSide::Top:
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
206 position += QPointF{0, static_cast<qreal>(fontMetrics.ascent())};
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
207 break;
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
208 case geom::RectangleSide::Left:
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
209 break;
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
210 case geom::RectangleSide::Bottom:
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
211 position += QPointF{0, static_cast<qreal>(-fontMetrics.descent())};
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
212 break;
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
213 case geom::RectangleSide::Right:
100
43ce3672648a fix build
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
214 position += QPointF{static_cast<qreal>(-fontMetrics.horizontalAdvance(text)), 0};
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
215 break;
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
216 }
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
217 painter.drawText(position, text);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
218 };
113
c0d064521ee0 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
219 const QRectF box {QPointF{0, 0}, sizeToSizeF(this->size())};
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
220 const QPointF p1 = this->modelToScreenCoordinates(glm::vec3{0, 0, 0});
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
221
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
222 static const struct
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
223 {
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
224 QString text;
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
225 glm::vec3 direction;
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
226 } directions[] =
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
227 {
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
228 {"+𝑥", {1, 0, 0}},
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
229 {"-𝑥", {-1, 0, 0}},
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
230 {"+𝑦", {0, 1, 0}},
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
231 {"-𝑦", {0, -1, 0}},
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
232 {"+𝑧", {0, 0, 1}},
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
233 {"-𝑧", {0, 0, -1}},
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
234 };
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
235 for (const auto& axis : directions)
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
236 {
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
237 const QPointF x_p = this->modelToScreenCoordinates(axis.direction);
115
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
238 const auto intersection = geom::rayRectangleIntersection(
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
239 geom::rayFromPoints(toVec2(p1), toVec2(x_p)),
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
240 box);
79
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
241 if (intersection.has_value())
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
242 {
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
243 renderText(axis.text, *intersection);
5fe2dd4e161a added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
244 }
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
245 }
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
246 if (this->overpaintCallback != nullptr)
105
6ca6e8c647d4 added preview layer code and fixed build warnings
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
247 {
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
248 this->overpaintCallback(this, &painter);
105
6ca6e8c647d4 added preview layer code and fixed build warnings
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
249 }
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
250 }
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
251 }
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
252 }
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
253
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
254 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
255 {
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
256 QVector<QPointF> points2d;
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
257 points2d.reserve(points.size());
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
258 for (const glm::vec3& point : points)
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
259 {
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
260 points2d.push_back(this->modelToScreenCoordinates(point));
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
261 }
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
262 painter->drawPolygon(QPolygonF{points2d});
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
263 }
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
264
110
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
265 const std::optional<glm::vec3>& Canvas::getWorldPosition() const
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
266 {
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
267 return this->worldPosition;
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
268 }
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
269
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
270 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
271 {
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
272 const QPointF center = this->modelToScreenCoordinates(worldPoint);
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
273 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
274 }
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
275
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
276 void Canvas::updateGridMatrix()
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
277 {
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
278 const geom::Triangle triangle {
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
279 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
280 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
281 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
282 };
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
283 this->gridPlane = geom::planeFromTriangle(triangle);
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
284 this->gridProgram->setGridMatrix(this->gridMatrix);
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
285 }
66
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
286
67
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
287 glm::vec3 Canvas::cameraVector() const
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
288 {
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
289 // Find out where the grid is projected on the screen
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
290 const QPoint gridOrigin2d = pointFToPoint(this->modelToScreenCoordinates(this->gridPlane.anchor));
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
291 // 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
292 return glm::normalize(this->cameraLine(gridOrigin2d).direction);
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
293 }
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
294
66
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
295 /**
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
296 * @brief Calculates if the screen is perpendicular to the current grid
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
297 * @return yes no
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
298 */
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 65
diff changeset
299 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
300 {
67
612213a168da grid autorotation
Teemu Piippo <teemu@hecknology.net>
parents: 66
diff changeset
301 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
302 // 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
303 // - 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
304 // - 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
305 // 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
306 // 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
307 // 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
308 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
309 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
310 }
105
6ca6e8c647d4 added preview layer code and fixed build warnings
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
311
107
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
312 void Canvas::clearSelection()
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
313 {
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
314 this->selection.clear();
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
315 this->compiler->setSelectedObjects(this->selection);
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
316 Q_EMIT selectionChanged(this->selection);
107
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
317 this->update();
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
318 }
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
319
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
320 void Canvas::addToSelection(ldraw::id_t id)
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
321 {
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
322 this->selection.insert(id);
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
323 this->compiler->setSelectedObjects(this->selection);
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
324 Q_EMIT selectionChanged(this->selection);
107
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
325 this->update();
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
326 }
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
327
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
328 void Canvas::setOverpaintCallback(Canvas::OverpaintCallback fn)
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
329 {
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
330 this->overpaintCallback = fn;
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
331 }

mercurial