Wed, 25 May 2022 17:48:18 +0300
added a missing const
47 | 1 | #include <QMouseEvent> |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
2 | #include <QPainter> |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
3 | #include "modeleditor.h" |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
4 | #include "document.h" |
47 | 5 | #include "canvas.h" |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
6 | #include "linetypes/edge.h" |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
7 | #include "linetypes/triangle.h" |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
8 | #include "linetypes/quadrilateral.h" |
47 | 9 | |
10 | Canvas::Canvas( | |
11 | Model* model, | |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
12 | Document *document, |
47 | 13 | DocumentManager* documents, |
14 | const ldraw::ColorTable& colorTable, | |
15 | QWidget* parent) : | |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
16 | PartRenderer{model, documents, colorTable, parent}, |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
17 | document{document} |
47 | 18 | { |
19 | this->setMouseTracking(true); | |
20 | } | |
21 | ||
129 | 22 | /** |
23 | * @brief Handles a change of selection | |
24 | * @param selectedIds IDs of objects to select | |
25 | * @param deselectedIds IDs of objects to deselect. | |
26 | */ | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
71
diff
changeset
|
27 | void Canvas::handleSelectionChange(const QSet<ldraw::id_t>& selectedIds, const QSet<ldraw::id_t>& deselectedIds) |
51 | 28 | { |
52
eee644f88e93
avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
29 | Q_ASSERT(not selectedIds.contains(ldraw::NULL_ID)); |
51 | 30 | this->selection.subtract(deselectedIds); |
31 | this->selection.unite(selectedIds); | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
32 | gl::setModelShaderSelectedObjects(&this->shaders, this->selection); |
51 | 33 | this->update(); |
34 | } | |
35 | ||
129 | 36 | /** |
37 | * @brief Updates vertex rendering | |
38 | * @param document Document to get vertices from | |
39 | */ | |
118 | 40 | void Canvas::rebuildVertices(Document* document) |
41 | { | |
42 | if (this->vertexProgram.has_value()) | |
43 | { | |
44 | this->vertexProgram->build(document); | |
45 | this->update(); | |
46 | } | |
47 | } | |
48 | ||
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
49 | void updatePreviewPolygon(DrawState* drawState) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
50 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
51 | drawState->previewPolygon = drawState->polygon; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
52 | drawState->previewPolygon.resize(drawState->polygon.size() + 1); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
53 | drawState->previewPolygon.back() = drawState->previewPoint; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
54 | if (drawState->previewPolygon.size() > 2) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
55 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
56 | drawState->isconcave = not geom::isConvex(drawState->previewPolygon); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
57 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
58 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
59 | |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
60 | void removeLastPoint(DrawState* drawState) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
61 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
62 | if (drawState->polygon.size() > 0) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
63 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
64 | drawState->polygon.erase(drawState->polygon.end() - 1); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
65 | updatePreviewPolygon(drawState); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
66 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
67 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
68 | |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
69 | bool isCloseToExistingPoints(const std::vector<glm::vec3>& points, const glm::vec3 &pos) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
70 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
71 | return any(points, std::bind(geom::isclose, std::placeholders::_1, pos)); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
72 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
73 | |
47 | 74 | void Canvas::mouseMoveEvent(QMouseEvent* event) |
75 | { | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
71
diff
changeset
|
76 | 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
|
77 | this->highlighted = id; |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
78 | 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
|
79 | 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
|
80 | if (this->worldPosition.has_value()) |
55 | 81 | { |
65
87c906545fc3
document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
82 | /* |
87c906545fc3
document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
83 | * 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
|
84 | * 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
|
85 | * 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
|
86 | * 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
|
87 | * not a linear transformation, but fits the pattern anyway. |
87c906545fc3
document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
88 | */ |
87c906545fc3
document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
89 | // 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
|
90 | 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
|
91 | // Then round the coordinates to integer precision... |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
92 | this->worldPosition = glm::round(*this->worldPosition); |
65
87c906545fc3
document the grid snapping transformations
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
93 | // 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
|
94 | // grid matrix. |
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
95 | 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
|
96 | } |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
97 | switch(this->mode) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
98 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
99 | case SelectMode: |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
100 | break; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
101 | case DrawMode: |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
102 | const auto& worldPosition = this->getWorldPosition(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
103 | if (worldPosition.has_value()) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
104 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
105 | this->drawState.previewPoint = worldPosition.value(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
106 | updatePreviewPolygon(&this->drawState); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
107 | this->update(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
108 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
109 | event->accept(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
110 | break; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
111 | } |
47 | 112 | PartRenderer::mouseMoveEvent(event); |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
120
diff
changeset
|
113 | this->update(); |
47 | 114 | } |
51 | 115 | |
116 | void Canvas::mousePressEvent(QMouseEvent* event) | |
117 | { | |
118 | this->totalMouseMove = 0; | |
119 | this->lastMousePosition = event->pos(); | |
120 | PartRenderer::mousePressEvent(event); | |
121 | } | |
122 | ||
123 | void Canvas::mouseReleaseEvent(QMouseEvent* event) | |
124 | { | |
125 | if (this->totalMouseMove < (2.0 / sqrt(2)) * 5.0) | |
126 | { | |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
127 | switch(this->mode) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
128 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
129 | case SelectMode: |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
130 | if (event->button() == Qt::LeftButton) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
131 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
132 | const ldraw::id_t highlighted = this->getHighlightedObject(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
133 | this->clearSelection(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
134 | if (highlighted != ldraw::NULL_ID) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
135 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
136 | this->addToSelection(highlighted); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
137 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
138 | event->accept(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
139 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
140 | break; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
141 | case DrawMode: |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
142 | if (event->button() == Qt::LeftButton and this->worldPosition.has_value()) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
143 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
144 | const glm::vec3& pos = worldPosition.value(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
145 | if (isCloseToExistingPoints(this->drawState.polygon, pos)) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
146 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
147 | this->closeShape(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
148 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
149 | else |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
150 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
151 | this->drawState.polygon.push_back(pos); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
152 | updatePreviewPolygon(&this->drawState); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
153 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
154 | event->accept(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
155 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
156 | else if (true |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
157 | and event->button() == Qt::RightButton |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
158 | and this->drawState.polygon.size() > 0 |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
159 | ) { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
160 | this->drawState.polygon.erase(this->drawState.polygon.end() - 1); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
161 | updatePreviewPolygon(&this->drawState); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
162 | event->accept(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
163 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
164 | break; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
165 | } |
51 | 166 | } |
167 | PartRenderer::mouseReleaseEvent(event); | |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
120
diff
changeset
|
168 | this->update(); |
51 | 169 | } |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
170 | |
61
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
171 | void Canvas::initializeGL() |
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
172 | { |
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
173 | // 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
|
174 | // 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
|
175 | this->gridProgram.emplace(this); |
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
176 | this->gridProgram->initialize(); |
70 | 177 | this->axesProgram.emplace(this); |
178 | this->axesProgram->initialize(); | |
118 | 179 | this->vertexProgram.emplace(this); |
180 | this->vertexProgram->initialize(); | |
70 | 181 | for (AbstractBasicShaderProgram* program : { |
182 | static_cast<AbstractBasicShaderProgram*>(&*this->gridProgram), | |
183 | static_cast<AbstractBasicShaderProgram*>(&*this->axesProgram), | |
118 | 184 | static_cast<AbstractBasicShaderProgram*>(&*this->vertexProgram), |
70 | 185 | }) |
61
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
186 | { |
70 | 187 | connect(this, &PartRenderer::projectionMatrixChanged, |
188 | program, &AbstractBasicShaderProgram::setProjectionMatrix); | |
189 | connect(this, &PartRenderer::modelMatrixChanged, | |
190 | program, &AbstractBasicShaderProgram::setModelMatrix); | |
191 | connect(this, &PartRenderer::viewMatrixChanged, | |
192 | program, &AbstractBasicShaderProgram::setViewMatrix); | |
193 | } | |
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
|
194 | connect(this, &PartRenderer::renderPreferencesChanged, this, &Canvas::updateCanvasRenderPreferences); |
61
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
195 | PartRenderer::initializeGL(); |
70 | 196 | // Set up XZ grid matrix |
129 | 197 | 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
|
198 | this->updateCanvasRenderPreferences(); |
61
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
199 | } |
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
200 | |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
201 | static const struct |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
202 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
203 | const QBrush pointBrush = {Qt::white}; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
204 | const QPen polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine}; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
205 | const QPen badPolygonPen = {QBrush{Qt::red}, 2.0, Qt::DashLine}; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
206 | const QPen pointPen = {QBrush{Qt::black}, 2.0}; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
207 | const QBrush greenPolygonBrush = {QColor{64, 255, 128, 192}}; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
208 | const QBrush redPolygonBrush = {QColor{255, 96, 96, 192}}; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
209 | } pens; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
210 | |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
211 | void Canvas::paintGL() |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
212 | { |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
213 | PartRenderer::paintGL(); |
79
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
214 | if (this->renderPreferences.style != gl::RenderStyle::PickScene) |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
215 | { |
79
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
216 | // Render axes |
170
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
217 | if (this->renderPreferences.drawAxes) |
79
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
218 | { |
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
219 | glLineWidth(5); |
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
220 | glEnable(GL_LINE_SMOOTH); |
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
221 | glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); |
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
222 | this->axesProgram->draw(); |
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
223 | glDisable(GL_LINE_SMOOTH); |
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
224 | } |
119 | 225 | // Render vertices |
226 | { | |
172
50f055543ff6
Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents:
170
diff
changeset
|
227 | glCullFace(GL_FRONT); |
119 | 228 | this->vertexProgram->draw(); |
229 | } | |
79
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
230 | // Render grid |
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
231 | { |
157 | 232 | glLineWidth(1); |
79
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
233 | glEnable(GL_BLEND); |
157 | 234 | glLineStipple(1, 0x8888); |
235 | glEnable(GL_LINE_STIPPLE); | |
79
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
236 | 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
|
237 | this->gridProgram->draw(); |
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
238 | glDisable(GL_BLEND); |
157 | 239 | glDisable(GL_LINE_STIPPLE); |
79
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
240 | } |
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
241 | if (this->worldPosition.has_value()) |
71
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
242 | { |
79
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
243 | QPainter painter{this}; |
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
244 | painter.setRenderHint(QPainter::Antialiasing); |
166
8857351912d0
Fix rendering of cursor coordinates on bright background
Teemu Piippo <teemu@hecknology.net>
parents:
165
diff
changeset
|
245 | 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
|
246 | painter.setBrush(Qt::green); |
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
247 | const QPointF pos = this->modelToScreenCoordinates(*this->worldPosition); |
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
248 | painter.drawEllipse(pos, 5, 5); |
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
249 | 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
|
250 | } |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
251 | QPainter painter{this}; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
252 | painter.setRenderHint(QPainter::Antialiasing); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
253 | if (this->renderPreferences.drawAxes) |
79
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
254 | { |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
255 | this->renderAxesLabels(painter); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
256 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
257 | switch(this->mode) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
258 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
259 | case SelectMode: |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
260 | break; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
261 | case DrawMode: |
71
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
262 | { |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
263 | painter.setPen(this->drawState.isconcave ? ::pens.badPolygonPen : ::pens.polygonPen); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
264 | if (this->drawState.previewPolygon.size() > 2 and not this->drawState.isconcave) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
265 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
266 | if (this->worldPolygonWinding(this->drawState.previewPolygon) == Winding::Clockwise) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
267 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
268 | painter.setBrush(::pens.greenPolygonBrush); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
269 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
270 | else |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
271 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
272 | painter.setBrush(::pens.redPolygonBrush); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
273 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
274 | this->drawWorldPolygon(&painter, this->drawState.previewPolygon); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
275 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
276 | else |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
277 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
278 | this->drawWorldPolyline(&painter, this->drawState.previewPolygon); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
279 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
280 | painter.setBrush(::pens.pointBrush); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
281 | painter.setPen(::pens.pointPen); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
282 | for (const glm::vec3& point : this->drawState.polygon) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
283 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
284 | this->drawWorldPoint(&painter, point); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
285 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
286 | this->drawWorldPoint(&painter, this->drawState.previewPoint); |
71
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
287 | } |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
288 | break; |
71
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
289 | } |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
290 | } |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
291 | } |
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
292 | |
129 | 293 | /** |
170
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
294 | * @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
|
295 | * @param painter |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
296 | */ |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
297 | void Canvas::renderAxesLabels(QPainter& painter) |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
298 | { |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
299 | QFont font; |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
300 | //font.setStyle(QFont::StyleItalic); |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
301 | painter.setFont(font); |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
302 | QFontMetrics fontMetrics{font}; |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
303 | 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
|
304 | { |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
305 | QPointF position = toQPointF(intersection.position); |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
306 | 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
|
307 | switch (side) |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
308 | { |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
309 | case geom::RectangleSide::Top: |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
310 | 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
|
311 | break; |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
312 | case geom::RectangleSide::Left: |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
313 | break; |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
314 | case geom::RectangleSide::Bottom: |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
315 | 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
|
316 | break; |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
317 | case geom::RectangleSide::Right: |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
318 | 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
|
319 | break; |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
320 | } |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
321 | painter.drawText(position, text); |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
322 | }; |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
323 | 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
|
324 | 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
|
325 | static const struct |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
326 | { |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
327 | QString text; |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
328 | glm::vec3 direction; |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
329 | } directions[] = |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
330 | { |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
331 | {"+𝑥", {1, 0, 0}}, |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
332 | {"-𝑥", {-1, 0, 0}}, |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
333 | {"+𝑦", {0, 1, 0}}, |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
334 | {"-𝑦", {0, -1, 0}}, |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
335 | {"+𝑧", {0, 0, 1}}, |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
336 | {"-𝑧", {0, 0, -1}}, |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
337 | }; |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
338 | for (const auto& axis : directions) |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
339 | { |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
340 | 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
|
341 | const auto intersection = geom::rayRectangleIntersection( |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
342 | 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
|
343 | box); |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
344 | if (intersection.has_value()) |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
345 | { |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
346 | renderText(axis.text, *intersection); |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
347 | } |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
348 | } |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
349 | } |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
350 | |
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
169
diff
changeset
|
351 | /** |
164
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
157
diff
changeset
|
352 | * @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
|
353 | * @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
|
354 | * @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
|
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 | 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
|
357 | { |
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
157
diff
changeset
|
358 | 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
|
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 | |
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
157
diff
changeset
|
361 | /** |
129 | 362 | * @brief Draws a polygon to where the specified vector of 3D points would appear on the screen. |
363 | * @param painter Painter to use to draw with | |
364 | * @param points 3D points to render | |
365 | */ | |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
366 | 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
|
367 | { |
164
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
157
diff
changeset
|
368 | 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
|
369 | } |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
370 | |
168
24590af32ad6
Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents:
166
diff
changeset
|
371 | 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
|
372 | { |
24590af32ad6
Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents:
166
diff
changeset
|
373 | 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
|
374 | } |
24590af32ad6
Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents:
166
diff
changeset
|
375 | |
129 | 376 | /** |
377 | * @brief Gets the current position of the cursor in the model | |
378 | * @return 3D vector | |
379 | */ | |
110 | 380 | const std::optional<glm::vec3>& Canvas::getWorldPosition() const |
381 | { | |
382 | return this->worldPosition; | |
383 | } | |
384 | ||
128
7c834fe36b25
Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents:
121
diff
changeset
|
385 | /** |
7c834fe36b25
Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents:
121
diff
changeset
|
386 | * @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
|
387 | */ |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
388 | void adjustGridToView(Canvas* canvas) |
128
7c834fe36b25
Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents:
121
diff
changeset
|
389 | { |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
390 | const glm::vec3 cameraDirection = canvas->cameraVector(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
391 | const glm::mat4& grid = canvas->getGridMatrix(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
392 | const glm::vec3 vector_x = glm::normalize(grid * glm::vec4{1, 0, 0, 1}); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
393 | const glm::vec3 vector_y = glm::normalize(grid * glm::vec4{0, 1, 0, 1}); |
128
7c834fe36b25
Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents:
121
diff
changeset
|
394 | 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
|
395 | const float angle_y = std::abs(glm::dot(vector_y, cameraDirection)); |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
396 | canvas->setGridMatrix(glm::rotate( |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
397 | grid, |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
398 | pi<> * 0.5f, |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
399 | (angle_x < angle_y) ? glm::vec3{1, 0, 0} : glm::vec3{0, 1, 0} |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
400 | )); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
401 | canvas->update(); |
128
7c834fe36b25
Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents:
121
diff
changeset
|
402 | } |
7c834fe36b25
Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents:
121
diff
changeset
|
403 | |
129 | 404 | /** |
169
6da096930534
Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents:
168
diff
changeset
|
405 | * @returns the ids of the currently selected objects |
6da096930534
Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents:
168
diff
changeset
|
406 | */ |
6da096930534
Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents:
168
diff
changeset
|
407 | const QSet<ldraw::id_t> Canvas::selectedObjects() const |
6da096930534
Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents:
168
diff
changeset
|
408 | { |
6da096930534
Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents:
168
diff
changeset
|
409 | return this->selection; |
6da096930534
Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents:
168
diff
changeset
|
410 | } |
6da096930534
Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents:
168
diff
changeset
|
411 | |
187 | 412 | const glm::mat4 &Canvas::getGridMatrix() const |
413 | { | |
414 | return this->gridMatrix; | |
415 | } | |
416 | ||
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
417 | void Canvas::closeShape() |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
418 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
419 | if (this->drawState.polygon.size() >= 2 and this->drawState.polygon.size() <= 4) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
420 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
421 | std::unique_ptr<ModelEditor> modelEditor = this->document->editModel(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
422 | switch (this->drawState.polygon.size()) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
423 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
424 | case 2: |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
425 | modelEditor->append<ldraw::Edge>( |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
426 | vectorToArray<2>(this->drawState.polygon), |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
427 | ldraw::EDGE_COLOR); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
428 | break; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
429 | case 3: |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
430 | modelEditor->append<ldraw::Triangle>( |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
431 | vectorToArray<3>(this->drawState.polygon), |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
432 | ldraw::MAIN_COLOR); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
433 | break; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
434 | case 4: |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
435 | modelEditor->append<ldraw::Quadrilateral>( |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
436 | vectorToArray<4>(this->drawState.polygon), |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
437 | ldraw::MAIN_COLOR); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
438 | break; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
439 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
440 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
441 | this->drawState.polygon.clear(); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
442 | updatePreviewPolygon(&this->drawState); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
443 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
444 | |
169
6da096930534
Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents:
168
diff
changeset
|
445 | /** |
129 | 446 | * @brief Paints a circle at where @c worldPoint is located on the screen. |
447 | * @param painter Painter to use to render | |
448 | * @param worldPoint Point to render | |
449 | */ | |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
450 | 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
|
451 | { |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
452 | const QPointF center = this->modelToScreenCoordinates(worldPoint); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
453 | 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
|
454 | } |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
455 | |
129 | 456 | /** |
457 | * @brief Changes the grid matrix to the one specified. Updates relevant member variables. | |
458 | * @param newMatrix New matrix to use | |
459 | */ | |
460 | 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
|
461 | { |
129 | 462 | this->gridMatrix = newMatrix; |
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
463 | const geom::Triangle triangle { |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
464 | 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
|
465 | 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
|
466 | 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
|
467 | }; |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
468 | this->gridPlane = geom::planeFromTriangle(triangle); |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
469 | this->gridProgram->setGridMatrix(this->gridMatrix); |
188
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
470 | this->update(); |
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
471 | } |
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
65
diff
changeset
|
472 | |
129 | 473 | /** |
474 | * @brief Gets the current camera vector, i.e. the vector from the camera to the grid origin. | |
475 | * @return vector | |
476 | */ | |
67 | 477 | glm::vec3 Canvas::cameraVector() const |
478 | { | |
479 | // Find out where the grid is projected on the screen | |
480 | const QPoint gridOrigin2d = pointFToPoint(this->modelToScreenCoordinates(this->gridPlane.anchor)); | |
481 | // Find out which direction the camera is looking at the grid origin in 3d | |
482 | return glm::normalize(this->cameraLine(gridOrigin2d).direction); | |
483 | } | |
484 | ||
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
65
diff
changeset
|
485 | /** |
129 | 486 | * @brief Calculates whether the screen is perpendicular to the current grid |
487 | * @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
|
488 | */ |
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
65
diff
changeset
|
489 | 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
|
490 | { |
67 | 491 | 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
|
492 | // 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
|
493 | // - 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
|
494 | // - 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
|
495 | // 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
|
496 | // 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
|
497 | // 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
|
498 | 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
|
499 | 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
|
500 | } |
105
6ca6e8c647d4
added preview layer code and fixed build warnings
Teemu Piippo <teemu@hecknology.net>
parents:
104
diff
changeset
|
501 | |
168
24590af32ad6
Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents:
166
diff
changeset
|
502 | 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
|
503 | { |
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
157
diff
changeset
|
504 | 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
|
505 | 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
|
506 | 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
|
507 | { |
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
157
diff
changeset
|
508 | 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
|
509 | } |
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
157
diff
changeset
|
510 | 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
|
511 | } |
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
157
diff
changeset
|
512 | |
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
|
513 | 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
|
514 | { |
166
8857351912d0
Fix rendering of cursor coordinates on bright background
Teemu Piippo <teemu@hecknology.net>
parents:
165
diff
changeset
|
515 | 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
|
516 | 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
|
517 | { |
166
8857351912d0
Fix rendering of cursor coordinates on bright background
Teemu Piippo <teemu@hecknology.net>
parents:
165
diff
changeset
|
518 | 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
|
519 | } |
f6eab2bd46c2
fixed the grid not being black on startup if settings has bright background color
Teemu Piippo <teemu@hecknology.net>
parents:
164
diff
changeset
|
520 | } |
f6eab2bd46c2
fixed the grid not being black on startup if settings has bright background color
Teemu Piippo <teemu@hecknology.net>
parents:
164
diff
changeset
|
521 | |
129 | 522 | /** |
523 | * @brief Clears the selection. | |
524 | */ | |
107
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
106
diff
changeset
|
525 | void Canvas::clearSelection() |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
106
diff
changeset
|
526 | { |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
106
diff
changeset
|
527 | this->selection.clear(); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
528 | gl::setModelShaderSelectedObjects(&this->shaders, this->selection); |
112 | 529 | Q_EMIT selectionChanged(this->selection); |
107
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
106
diff
changeset
|
530 | this->update(); |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
106
diff
changeset
|
531 | } |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
106
diff
changeset
|
532 | |
129 | 533 | /** |
534 | * @brief Adds an object to selection. | |
535 | * @param id ID of object to add | |
536 | */ | |
107
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
106
diff
changeset
|
537 | void Canvas::addToSelection(ldraw::id_t id) |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
106
diff
changeset
|
538 | { |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
106
diff
changeset
|
539 | this->selection.insert(id); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
540 | gl::setModelShaderSelectedObjects(&this->shaders, this->selection); |
112 | 541 | Q_EMIT selectionChanged(this->selection); |
107
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
106
diff
changeset
|
542 | this->update(); |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
106
diff
changeset
|
543 | } |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
544 | |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
545 | void Canvas::setOverpaintCallback(Canvas::OverpaintCallback fn) |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
546 | { |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
547 | this->overpaintCallback = fn; |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
548 | } |