Wed, 25 Mar 2020 16:07:20 +0200
added PolygonObject and refactored away a lot of boilerplate
24 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2020 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
26 | 19 | #include <GL/glut.h> |
28 | 20 | #include <glm/ext/matrix_transform.hpp> |
21 | #include <glm/ext/matrix_clip_space.hpp> | |
17 | 22 | #include <QMouseEvent> |
26 | 23 | #include <QMessageBox> |
47 | 24 | #include <QAbstractButton> |
55 | 25 | #include "geometry.h" |
17 | 26 | #include "partrenderer.h" |
77
028798a72591
added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
27 | #include "model.h" |
17 | 28 | |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
29 | PartRenderer::PartRenderer( |
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
30 | Model* model, |
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
31 | DocumentManager* documents, |
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
32 | const ldraw::ColorTable& colorTable, |
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
33 | QWidget* parent) : |
21 | 34 | QOpenGLWidget{parent}, |
35 | model{model}, | |
36 | documents{documents}, | |
26 | 37 | colorTable{colorTable}, |
61
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
38 | compiler{new gl::Compiler{this->colorTable, this}} |
17 | 39 | { |
40 | this->setMouseTracking(true); | |
41 | } | |
42 | ||
26 | 43 | PartRenderer::~PartRenderer() |
44 | { | |
45 | } | |
46 | ||
53 | 47 | static QVector3D vec3FromQColor(const QColor& color) |
48 | { | |
61
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
49 | return { |
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
50 | toFloat(color.redF()), |
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
51 | toFloat(color.greenF()), |
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
52 | toFloat(color.blueF()), |
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
53 | }; |
53 | 54 | } |
55 | ||
17 | 56 | void PartRenderer::initializeGL() |
57 | { | |
58 | this->initializeOpenGLFunctions(); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
59 | if (glGetError() != GL_NO_ERROR) |
17 | 60 | { |
61 | abort(); | |
62 | } | |
27
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
63 | this->compiler->initialize(); |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
64 | connect(this->model, &Model::dataChanged, this, &PartRenderer::build); |
17 | 65 | this->initialized = true; |
32 | 66 | this->modelQuaternion = glm::angleAxis(glm::radians(30.0f), glm::vec3{-1, 0, 0}); |
67 | this->modelQuaternion *= glm::angleAxis(glm::radians(225.0f), glm::vec3{-0, 1, 0}); | |
55 | 68 | this->setupBackgroundColor(); |
69 | this->updateModelMatrix(); | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
70 | this->updateViewMatrix(); |
30 | 71 | this->update(); |
17 | 72 | } |
73 | ||
74 | void PartRenderer::resizeGL(int width, int height) | |
75 | { | |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
76 | this->viewportVector = {0, 0, width, height}; |
28 | 77 | glViewport(0, 0, width, height); |
78 | this->projectionMatrix = glm::perspective( | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
79 | glm::radians(45.0f), |
28 | 80 | static_cast<float>(width) / static_cast<float>(height), |
81 | 0.1f, | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
82 | 10000.f); |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
83 | this->compiler->setUniformMatrix("projectionMatrix", this->projectionMatrix); |
70 | 84 | emit projectionMatrixChanged(this->projectionMatrix); |
17 | 85 | } |
86 | ||
26 | 87 | static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass) |
21 | 88 | { |
89 | switch (vboClass) | |
90 | { | |
26 | 91 | case gl::ArrayClass::Lines: |
92 | case gl::ArrayClass::ConditionalLines: | |
21 | 93 | return GL_LINES; |
26 | 94 | case gl::ArrayClass::Triangles: |
21 | 95 | return GL_TRIANGLES; |
26 | 96 | case gl::ArrayClass::Quads: |
21 | 97 | return GL_QUADS; |
98 | } | |
99 | throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"}; | |
100 | } | |
101 | ||
17 | 102 | void PartRenderer::paintGL() |
103 | { | |
47 | 104 | glEnable(GL_DEPTH_TEST); |
105 | glShadeModel(GL_SMOOTH); | |
106 | this->renderScene(); | |
107 | } | |
108 | ||
109 | void PartRenderer::renderScene() | |
110 | { | |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
111 | if (this->needBuild) |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
112 | { |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
113 | this->compiler->build(this->model, this->documents, this->renderPreferences); |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
114 | this->needBuild = false; |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
115 | } |
53 | 116 | this->checkForGLErrors(); |
46 | 117 | if (this->renderPreferences.lineAntiAliasing && this->renderPreferences.style != gl::RenderStyle::PickScene) |
45
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
118 | { |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
119 | glEnable(GL_LINE_SMOOTH); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
120 | glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
121 | } |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
122 | else { |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
123 | glDisable(GL_LINE_SMOOTH); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
124 | } |
46 | 125 | if (this->renderPreferences.style != gl::RenderStyle::PickScene) |
126 | { | |
47 | 127 | const QColor& backgroundColor = this->renderPreferences.backgroundColor; |
46 | 128 | glClearColor( |
129 | static_cast<float>(backgroundColor.redF()), | |
130 | static_cast<float>(backgroundColor.greenF()), | |
131 | static_cast<float>(backgroundColor.blueF()), | |
132 | 1.0f); | |
51 | 133 | this->compiler->setUniform("useLighting", GL_TRUE); |
46 | 134 | } |
135 | else | |
136 | { | |
137 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
51 | 138 | this->compiler->setUniform("useLighting", GL_FALSE); |
46 | 139 | } |
53 | 140 | this->checkForGLErrors(); |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
141 | this->compiler->setUniform("selectedColor", vec3FromQColor(this->renderPreferences.selectedColor)); |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
142 | this->compiler->setUniform("highlighted", this->highlighted.value); |
51 | 143 | this->checkForGLErrors(); |
26 | 144 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
145 | glEnable(GL_DEPTH_TEST); | |
146 | glEnable(GL_POLYGON_OFFSET_FILL); | |
147 | glPolygonOffset(1.0f, 1.0f); | |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
148 | glLineWidth(this->renderPreferences.lineThickness); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
149 | switch (this->renderPreferences.style) |
18 | 150 | { |
151 | case gl::RenderStyle::Normal: | |
37 | 152 | this->setFragmentStyle(gl::FragmentStyle::Normal); |
153 | this->renderAllArrays(); | |
154 | break; | |
18 | 155 | case gl::RenderStyle::BfcRedGreen: |
37 | 156 | glEnable(GL_CULL_FACE); |
157 | glCullFace(GL_BACK); | |
158 | this->setFragmentStyle(gl::FragmentStyle::BfcGreen); | |
159 | renderVao(gl::ArrayClass::Triangles); | |
160 | renderVao(gl::ArrayClass::Quads); | |
161 | glCullFace(GL_FRONT); | |
162 | this->setFragmentStyle(gl::FragmentStyle::BfcRed); | |
163 | renderVao(gl::ArrayClass::Triangles); | |
164 | renderVao(gl::ArrayClass::Quads); | |
165 | glDisable(GL_CULL_FACE); | |
166 | this->setFragmentStyle(gl::FragmentStyle::Normal); | |
167 | renderVao(gl::ArrayClass::Lines); | |
18 | 168 | case gl::RenderStyle::RandomColors: |
37 | 169 | this->setFragmentStyle(gl::FragmentStyle::RandomColors); |
170 | this->renderAllArrays(); | |
18 | 171 | break; |
46 | 172 | case gl::RenderStyle::PickScene: |
173 | glLineWidth(3.0f); | |
174 | this->setFragmentStyle(gl::FragmentStyle::Id); | |
175 | this->renderAllArrays(); | |
176 | break; | |
18 | 177 | case gl::RenderStyle::Wireframe: |
178 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); | |
37 | 179 | this->setFragmentStyle(gl::FragmentStyle::Normal); |
180 | this->renderAllArrays(); | |
18 | 181 | break; |
182 | } | |
37 | 183 | glDisable(GL_POLYGON_OFFSET_FILL); |
184 | } | |
185 | ||
186 | void PartRenderer::renderAllArrays() | |
187 | { | |
26 | 188 | // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. |
189 | renderVao(gl::ArrayClass::Triangles); | |
190 | renderVao(gl::ArrayClass::Quads); | |
191 | renderVao(gl::ArrayClass::Lines); | |
192 | } | |
193 | ||
37 | 194 | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
195 | void PartRenderer::updateViewMatrix() |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
196 | { |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
197 | // I'm not quite sure why using the exponent function on the zoom factor causes linear zoom behavior |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
198 | const double z = 2 * std::exp(this->zoom) * (1 + this->compiler->modelDistance()); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
199 | this->viewMatrix = glm::lookAt(glm::vec3{0, 0, z}, {0, 0, 0}, {0, -1, 0}); |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
200 | this->compiler->setUniformMatrix("viewMatrix", this->viewMatrix); |
70 | 201 | emit this->viewMatrixChanged(this->viewMatrix); |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
202 | } |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
203 | |
55 | 204 | void PartRenderer::updateModelMatrix() |
205 | { | |
61
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
206 | this->modelMatrix = glm::mat4_cast(this->modelQuaternion); |
55 | 207 | this->compiler->setUniformMatrix("modelMatrix", modelMatrix); |
70 | 208 | emit this->modelMatrixChanged(this->modelMatrix); |
55 | 209 | this->update(); |
210 | } | |
211 | ||
212 | void PartRenderer::setupBackgroundColor() | |
213 | { | |
214 | } | |
215 | ||
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
216 | void PartRenderer::build() |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
217 | { |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
218 | this->needBuild = true; |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
219 | } |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
220 | |
27
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
221 | void PartRenderer::renderVao(const gl::ArrayClass arrayClass) |
26 | 222 | { |
223 | this->compiler->bindVertexArray(arrayClass); | |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
224 | const std::size_t vertexCount = this->compiler->vertexCount(arrayClass); |
51 | 225 | this->checkForGLErrors(); |
26 | 226 | glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); |
51 | 227 | this->checkForGLErrors(); |
26 | 228 | this->compiler->releaseVertexArray(arrayClass); |
229 | this->checkForGLErrors(); | |
230 | } | |
231 | ||
232 | void PartRenderer::checkForGLErrors() | |
233 | { | |
53 | 234 | gl::checkForGLErrors(this); |
235 | } | |
236 | ||
237 | void gl::checkForGLErrors(QWidget* parent) | |
238 | { | |
26 | 239 | GLenum glError; |
240 | QStringList errors; | |
241 | while ((glError = glGetError()) != GL_NO_ERROR) | |
21 | 242 | { |
243 | const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError))); | |
26 | 244 | errors.append(glErrorString); |
245 | } | |
246 | if (not errors.isEmpty()) | |
247 | { | |
53 | 248 | QMessageBox box{parent}; |
47 | 249 | box.setIcon(QMessageBox::Critical); |
53 | 250 | box.setText(QObject::tr("OpenGL error: %1").arg(errors.join("\n"))); |
251 | box.setWindowTitle(QObject::tr("OpenGL error")); | |
47 | 252 | box.setStandardButtons(QMessageBox::Close); |
53 | 253 | box.button(QMessageBox::Close)->setText(QObject::tr("Damn it")); |
47 | 254 | box.exec(); |
21 | 255 | } |
17 | 256 | } |
257 | ||
258 | void PartRenderer::mouseMoveEvent(QMouseEvent* event) | |
259 | { | |
260 | const bool left = event->buttons() & Qt::LeftButton; | |
61
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
261 | const QPoint move = event->pos() - this->lastMousePosition; |
17 | 262 | if (left and not move.isNull()) |
263 | { | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
264 | // q_x is the rotation of the brick along the vertical y-axis, because turning the |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
265 | // vertical axis causes horizontal (=x) rotation. Likewise q_y is the rotation of the |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
266 | // brick along the horizontal x-axis, which causes vertical rotation. |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
267 | const auto scalar = 0.006f; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
268 | const float move_x = static_cast<float>(move.x()); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
269 | const float move_y = static_cast<float>(move.y()); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
270 | const glm::quat q_x = glm::angleAxis(scalar * move_x, glm::vec3{0, -1, 0}); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
271 | const glm::quat q_y = glm::angleAxis(scalar * move_y, glm::vec3{-1, 0, 0}); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
272 | this->modelQuaternion = q_x * q_y * this->modelQuaternion; |
55 | 273 | this->updateModelMatrix(); |
17 | 274 | } |
61
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
275 | this->lastMousePosition = event->pos(); |
17 | 276 | } |
18 | 277 | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
278 | void PartRenderer::wheelEvent(QWheelEvent* event) |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
279 | { |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
280 | static constexpr double WHEEL_STEP = 1 / 1000.0; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
281 | const double move = (-event->angleDelta().y()) * WHEEL_STEP; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
282 | this->zoom = std::clamp(this->zoom + move, MIN_ZOOM, MAX_ZOOM); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
283 | this->updateViewMatrix(); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
284 | this->update(); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
285 | } |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
286 | |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
287 | /** |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
288 | * @brief Converts the specified on the screen into the 3D world. The point is unprojected twice into 3D and the |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
289 | * intersection of the resulting line with the specified plane is returned. If the intersection point lies behind |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
290 | * the camera, no value is returned. |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
291 | * @param point 2D window co-ordinates to convert. |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
292 | * @param plane Plane to raycast against |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
293 | * @return world co-ordinates, or no value if the point is behind the camera. |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
294 | */ |
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
295 | std::optional<glm::vec3> PartRenderer::screenToModelCoordinates(const QPoint& point, const geom::Plane& plane) const |
55 | 296 | { |
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
297 | const geom::Line line = this->cameraLine(point); |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
298 | std::optional<glm::vec3> result; |
59
60650929dc82
fixed testing of whether screenToModelCoordinates's result value is behind the camera
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
299 | result = geom::linePlaneIntersection(line, plane, 0.01f); |
60650929dc82
fixed testing of whether screenToModelCoordinates's result value is behind the camera
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
300 | // If the point lies behind the camera, do not return a result. |
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
301 | if (result.has_value() and glm::dot(line.direction, *result - line.anchor) < 0) |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
302 | { |
59
60650929dc82
fixed testing of whether screenToModelCoordinates's result value is behind the camera
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
303 | result.reset(); |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
304 | } |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
305 | return result; |
55 | 306 | } |
307 | ||
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
308 | /** |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
309 | * @brief Converts the specified point to 2D window coordinates, with Y-coordinate inverted for Qt |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
310 | * @param point Point to unproject |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
311 | * @return screen coordinates |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
312 | */ |
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
313 | QPointF PartRenderer::modelToScreenCoordinates(const glm::vec3& point) const |
55 | 314 | { |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
315 | const glm::vec3 projected = glm::project( |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
316 | point, |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
317 | this->viewMatrix * glm::mat4_cast(this->modelQuaternion), |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
318 | this->projectionMatrix, |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
319 | this->viewportVector); |
60 | 320 | return toQPointF(glm::vec2{projected.x, this->height() - projected.y}); |
55 | 321 | } |
322 | ||
71
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
323 | geom::Line<3> PartRenderer::cameraLine(const QPoint& point) const |
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
324 | { |
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
325 | const glm::vec3 p1 = this->unproject({point.x(), point.y(), 0}); |
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
326 | const glm::vec3 p2 = this->unproject({point.x(), point.y(), 1}); |
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
327 | return geom::lineFromPoints(p1, p2); |
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
328 | } |
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
329 | |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
330 | /** |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
331 | * @brief Unprojects the specified window coordinates to model coordinates |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
332 | * @param win Window coordinates to project. Z-coordinate indicates depth |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
333 | * @return model coordinates |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
334 | */ |
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
335 | glm::vec3 PartRenderer::unproject(const glm::vec3& win) const |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
336 | { |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
337 | return glm::unProject( |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
338 | glm::vec3{win.x, this->height() - win.y, win.z}, |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
339 | this->viewMatrix * glm::mat4_cast(this->modelQuaternion), |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
340 | this->projectionMatrix, |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
341 | viewportVector); |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
342 | } |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
343 | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
71
diff
changeset
|
344 | ldraw::id_t PartRenderer::pick(const QPoint& where) |
47 | 345 | { |
346 | const gl::RenderStyle oldRenderStyle = this->renderPreferences.style; | |
347 | this->renderPreferences.style = gl::RenderStyle::PickScene; | |
348 | this->makeCurrent(); | |
349 | this->renderScene(); | |
78
97c3ce5aa498
fixed signed vs unsigned nonsense in gl::Compiler::idFromColor
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
350 | std::array<GLubyte, 3> data; |
51 | 351 | this->checkForGLErrors(); |
47 | 352 | glReadPixels(where.x(), this->height() - where.y(), 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &data[0]); |
51 | 353 | this->checkForGLErrors(); |
47 | 354 | this->renderPreferences.style = oldRenderStyle; |
355 | this->update(); | |
356 | return gl::Compiler::idFromColor(data); | |
357 | } | |
358 | ||
37 | 359 | /** |
360 | * @brief Changes the color of rendered fragments | |
361 | * @param newFragmentStyle new fragment style to use | |
362 | */ | |
363 | void PartRenderer::setFragmentStyle(gl::FragmentStyle newFragmentStyle) | |
364 | { | |
365 | this->compiler->setUniform("fragmentStyle", static_cast<int>(newFragmentStyle)); | |
366 | } | |
367 | ||
368 | /** | |
369 | * @brief Changes the way the scene is rendered | |
370 | * @param newStyle new render style to use | |
371 | */ | |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
372 | void PartRenderer::setRenderPreferences(const gl::RenderPreferences& newPreferences) |
18 | 373 | { |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
374 | bool mainColorChanged = this->renderPreferences.mainColor != newPreferences.mainColor; |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
375 | bool backgroundColorChanged = this->renderPreferences.backgroundColor != newPreferences.backgroundColor; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
376 | this->renderPreferences = newPreferences; |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
377 | if (mainColorChanged or backgroundColorChanged) |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
378 | { |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
379 | this->build(); |
55 | 380 | this->setupBackgroundColor(); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
381 | } |
61
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
382 | emit this->renderPreferencesChanged(); |
18 | 383 | this->update(); |
384 | } |