Thu, 27 Feb 2020 11:56:41 +0200
use glm::unProject to implement screenToModelCoordinates
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" |
27 | ||
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
28 | 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
|
29 | 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
|
30 | 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
|
31 | 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
|
32 | QWidget* parent) : |
21 | 33 | QOpenGLWidget{parent}, |
34 | model{model}, | |
35 | documents{documents}, | |
26 | 36 | colorTable{colorTable}, |
53 | 37 | compiler{new gl::Compiler{this->colorTable, this}}, |
38 | gridProgram{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 | { | |
49 | return {(float)color.redF(), (float)color.greenF(), (float)color.blueF()}; | |
50 | } | |
51 | ||
17 | 52 | void PartRenderer::initializeGL() |
53 | { | |
54 | this->initializeOpenGLFunctions(); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
55 | if (glGetError() != GL_NO_ERROR) |
17 | 56 | { |
57 | abort(); | |
58 | } | |
53 | 59 | this->gridProgram.emplace(this); |
60 | this->gridProgram->initialize(); | |
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
|
61 | this->compiler->initialize(); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
62 | this->compiler->build(this->model, this->documents, this->renderPreferences); |
17 | 63 | this->initialized = true; |
32 | 64 | this->modelQuaternion = glm::angleAxis(glm::radians(30.0f), glm::vec3{-1, 0, 0}); |
65 | this->modelQuaternion *= glm::angleAxis(glm::radians(225.0f), glm::vec3{-0, 1, 0}); | |
55 | 66 | this->setupBackgroundColor(); |
67 | this->updateModelMatrix(); | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
68 | this->updateViewMatrix(); |
30 | 69 | this->update(); |
17 | 70 | } |
71 | ||
72 | void PartRenderer::resizeGL(int width, int height) | |
73 | { | |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
74 | this->viewportVector = {0, 0, width, height}; |
28 | 75 | glViewport(0, 0, width, height); |
76 | this->projectionMatrix = glm::perspective( | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
77 | glm::radians(45.0f), |
28 | 78 | static_cast<float>(width) / static_cast<float>(height), |
79 | 0.1f, | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
80 | 10000.f); |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
81 | this->compiler->setUniformMatrix("projectionMatrix", this->projectionMatrix); |
53 | 82 | this->gridProgram->setProjectionMatrix(this->projectionMatrix); |
17 | 83 | } |
84 | ||
26 | 85 | static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass) |
21 | 86 | { |
87 | switch (vboClass) | |
88 | { | |
26 | 89 | case gl::ArrayClass::Lines: |
90 | case gl::ArrayClass::ConditionalLines: | |
21 | 91 | return GL_LINES; |
26 | 92 | case gl::ArrayClass::Triangles: |
21 | 93 | return GL_TRIANGLES; |
26 | 94 | case gl::ArrayClass::Quads: |
21 | 95 | return GL_QUADS; |
96 | } | |
97 | throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"}; | |
98 | } | |
99 | ||
17 | 100 | void PartRenderer::paintGL() |
101 | { | |
47 | 102 | glEnable(GL_DEPTH_TEST); |
103 | glShadeModel(GL_SMOOTH); | |
104 | this->renderScene(); | |
105 | } | |
106 | ||
107 | void PartRenderer::renderScene() | |
108 | { | |
53 | 109 | this->checkForGLErrors(); |
46 | 110 | 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
|
111 | { |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
112 | glEnable(GL_LINE_SMOOTH); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
113 | glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
114 | } |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
115 | else { |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
116 | glDisable(GL_LINE_SMOOTH); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
117 | } |
46 | 118 | if (this->renderPreferences.style != gl::RenderStyle::PickScene) |
119 | { | |
47 | 120 | const QColor& backgroundColor = this->renderPreferences.backgroundColor; |
46 | 121 | glClearColor( |
122 | static_cast<float>(backgroundColor.redF()), | |
123 | static_cast<float>(backgroundColor.greenF()), | |
124 | static_cast<float>(backgroundColor.blueF()), | |
125 | 1.0f); | |
51 | 126 | this->compiler->setUniform("useLighting", GL_TRUE); |
46 | 127 | } |
128 | else | |
129 | { | |
130 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
51 | 131 | this->compiler->setUniform("useLighting", GL_FALSE); |
46 | 132 | } |
53 | 133 | this->checkForGLErrors(); |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
134 | this->compiler->setUniform("selectedColor", vec3FromQColor(this->renderPreferences.selectedColor)); |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
135 | this->compiler->setUniform("highlighted", this->highlighted.value); |
51 | 136 | this->checkForGLErrors(); |
26 | 137 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
138 | glEnable(GL_DEPTH_TEST); | |
139 | glEnable(GL_POLYGON_OFFSET_FILL); | |
140 | glPolygonOffset(1.0f, 1.0f); | |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
141 | glLineWidth(this->renderPreferences.lineThickness); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
142 | switch (this->renderPreferences.style) |
18 | 143 | { |
144 | case gl::RenderStyle::Normal: | |
37 | 145 | this->setFragmentStyle(gl::FragmentStyle::Normal); |
146 | this->renderAllArrays(); | |
147 | break; | |
18 | 148 | case gl::RenderStyle::BfcRedGreen: |
37 | 149 | glEnable(GL_CULL_FACE); |
150 | glCullFace(GL_BACK); | |
151 | this->setFragmentStyle(gl::FragmentStyle::BfcGreen); | |
152 | renderVao(gl::ArrayClass::Triangles); | |
153 | renderVao(gl::ArrayClass::Quads); | |
154 | glCullFace(GL_FRONT); | |
155 | this->setFragmentStyle(gl::FragmentStyle::BfcRed); | |
156 | renderVao(gl::ArrayClass::Triangles); | |
157 | renderVao(gl::ArrayClass::Quads); | |
158 | glDisable(GL_CULL_FACE); | |
159 | this->setFragmentStyle(gl::FragmentStyle::Normal); | |
160 | renderVao(gl::ArrayClass::Lines); | |
18 | 161 | case gl::RenderStyle::RandomColors: |
37 | 162 | this->setFragmentStyle(gl::FragmentStyle::RandomColors); |
163 | this->renderAllArrays(); | |
18 | 164 | break; |
46 | 165 | case gl::RenderStyle::PickScene: |
166 | glLineWidth(3.0f); | |
167 | this->setFragmentStyle(gl::FragmentStyle::Id); | |
168 | this->renderAllArrays(); | |
169 | break; | |
18 | 170 | case gl::RenderStyle::Wireframe: |
171 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); | |
37 | 172 | this->setFragmentStyle(gl::FragmentStyle::Normal); |
173 | this->renderAllArrays(); | |
18 | 174 | break; |
175 | } | |
53 | 176 | glEnable(GL_BLEND); |
177 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
178 | this->gridProgram->draw(); | |
179 | glDisable(GL_BLEND); | |
37 | 180 | glDisable(GL_POLYGON_OFFSET_FILL); |
181 | } | |
182 | ||
183 | void PartRenderer::renderAllArrays() | |
184 | { | |
26 | 185 | // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. |
186 | renderVao(gl::ArrayClass::Triangles); | |
187 | renderVao(gl::ArrayClass::Quads); | |
188 | renderVao(gl::ArrayClass::Lines); | |
189 | } | |
190 | ||
37 | 191 | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
192 | void PartRenderer::updateViewMatrix() |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
193 | { |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
194 | // 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
|
195 | 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
|
196 | 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
|
197 | this->compiler->setUniformMatrix("viewMatrix", this->viewMatrix); |
53 | 198 | this->gridProgram->setViewMatrix(this->viewMatrix); |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
199 | } |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
200 | |
55 | 201 | void PartRenderer::updateModelMatrix() |
202 | { | |
203 | const glm::mat4 modelMatrix = glm::mat4_cast(this->modelQuaternion); | |
204 | this->compiler->setUniformMatrix("modelMatrix", modelMatrix); | |
205 | this->gridProgram->setModelMatrix(modelMatrix); | |
206 | this->update(); | |
207 | } | |
208 | ||
209 | void PartRenderer::setupBackgroundColor() | |
210 | { | |
211 | if (this->gridProgram.has_value()) | |
212 | { | |
213 | const bool isDark = luma(this->renderPreferences.backgroundColor) < 0.25; | |
214 | this->gridProgram->setGridColor(isDark ? Qt::white : Qt::black); | |
215 | } | |
216 | } | |
217 | ||
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
|
218 | void PartRenderer::renderVao(const gl::ArrayClass arrayClass) |
26 | 219 | { |
220 | this->compiler->bindVertexArray(arrayClass); | |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
221 | const std::size_t vertexCount = this->compiler->vertexCount(arrayClass); |
51 | 222 | this->checkForGLErrors(); |
26 | 223 | glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); |
51 | 224 | this->checkForGLErrors(); |
26 | 225 | this->compiler->releaseVertexArray(arrayClass); |
226 | this->checkForGLErrors(); | |
227 | } | |
228 | ||
229 | void PartRenderer::checkForGLErrors() | |
230 | { | |
53 | 231 | gl::checkForGLErrors(this); |
232 | } | |
233 | ||
234 | void gl::checkForGLErrors(QWidget* parent) | |
235 | { | |
26 | 236 | GLenum glError; |
237 | QStringList errors; | |
238 | while ((glError = glGetError()) != GL_NO_ERROR) | |
21 | 239 | { |
240 | const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError))); | |
26 | 241 | errors.append(glErrorString); |
242 | } | |
243 | if (not errors.isEmpty()) | |
244 | { | |
53 | 245 | QMessageBox box{parent}; |
47 | 246 | box.setIcon(QMessageBox::Critical); |
53 | 247 | box.setText(QObject::tr("OpenGL error: %1").arg(errors.join("\n"))); |
248 | box.setWindowTitle(QObject::tr("OpenGL error")); | |
47 | 249 | box.setStandardButtons(QMessageBox::Close); |
53 | 250 | box.button(QMessageBox::Close)->setText(QObject::tr("Damn it")); |
47 | 251 | box.exec(); |
21 | 252 | } |
17 | 253 | } |
254 | ||
255 | void PartRenderer::mouseMoveEvent(QMouseEvent* event) | |
256 | { | |
257 | const bool left = event->buttons() & Qt::LeftButton; | |
258 | const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition; | |
259 | if (left and not move.isNull()) | |
260 | { | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
261 | // 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
|
262 | // 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
|
263 | // 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
|
264 | const auto scalar = 0.006f; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
265 | const float move_x = static_cast<float>(move.x()); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
266 | const float move_y = static_cast<float>(move.y()); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
267 | 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
|
268 | 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
|
269 | this->modelQuaternion = q_x * q_y * this->modelQuaternion; |
55 | 270 | this->updateModelMatrix(); |
17 | 271 | } |
272 | this->lastMousePosition = pointToPointF(event->pos()); | |
273 | } | |
18 | 274 | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
275 | void PartRenderer::wheelEvent(QWheelEvent* event) |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
276 | { |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
277 | static constexpr double WHEEL_STEP = 1 / 1000.0; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
278 | const double move = (-event->angleDelta().y()) * WHEEL_STEP; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
279 | 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
|
280 | this->updateViewMatrix(); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
281 | this->update(); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
282 | } |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
283 | |
56
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
284 | glm::vec3 PartRenderer::viewport(const glm::vec3& point) |
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
285 | { |
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
286 | return viewport(point, this->width(), this->height()); |
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
287 | } |
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
288 | |
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
289 | std::optional<glm::vec3> PartRenderer::screenToModelCoordinates(const QPoint& point) |
55 | 290 | { |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
291 | auto p1 = this->unproject({point.x(), point.y(), 0}); |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
292 | auto p2 = this->unproject({point.x(), point.y(), 1}); |
55 | 293 | geom::Line line = geom::lineFromPoints(p1, p2); |
294 | return geom::linePlaneIntersection(line, geom::XY); | |
295 | } | |
296 | ||
56
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
297 | QPointF PartRenderer::modelToScreenCoordinates(const glm::vec3& point) |
55 | 298 | { |
299 | glm::vec4 p = {point, 1}; | |
300 | p = glm::mat4_cast(this->modelQuaternion) * p; | |
301 | p = this->viewMatrix * p; | |
302 | p = this->projectionMatrix * p; | |
56
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
303 | glm::vec2 pp = this->viewport({p.x / p.w, p.y / p.w, 1}); |
55 | 304 | return toQPointF(pp); |
305 | } | |
306 | ||
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
307 | glm::vec3 PartRenderer::unproject(const glm::vec3& win) |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
308 | { |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
309 | return glm::unProject( |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
310 | 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
|
311 | this->viewMatrix * glm::mat4_cast(this->modelQuaternion), |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
312 | this->projectionMatrix, |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
313 | viewportVector); |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
314 | } |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
315 | |
47 | 316 | ldraw::Id PartRenderer::pick(const QPoint& where) |
317 | { | |
318 | const gl::RenderStyle oldRenderStyle = this->renderPreferences.style; | |
319 | this->renderPreferences.style = gl::RenderStyle::PickScene; | |
320 | this->makeCurrent(); | |
321 | this->renderScene(); | |
322 | std::array<GLbyte, 3> data; | |
51 | 323 | this->checkForGLErrors(); |
47 | 324 | glReadPixels(where.x(), this->height() - where.y(), 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &data[0]); |
51 | 325 | this->checkForGLErrors(); |
47 | 326 | this->renderPreferences.style = oldRenderStyle; |
327 | this->update(); | |
328 | return gl::Compiler::idFromColor(data); | |
329 | } | |
330 | ||
37 | 331 | /** |
332 | * @brief Changes the color of rendered fragments | |
333 | * @param newFragmentStyle new fragment style to use | |
334 | */ | |
335 | void PartRenderer::setFragmentStyle(gl::FragmentStyle newFragmentStyle) | |
336 | { | |
337 | this->compiler->setUniform("fragmentStyle", static_cast<int>(newFragmentStyle)); | |
338 | } | |
339 | ||
340 | /** | |
341 | * @brief Changes the way the scene is rendered | |
342 | * @param newStyle new render style to use | |
343 | */ | |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
344 | void PartRenderer::setRenderPreferences(const gl::RenderPreferences& newPreferences) |
18 | 345 | { |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
346 | bool mainColorChanged = this->renderPreferences.mainColor != newPreferences.mainColor; |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
347 | bool backgroundColorChanged = this->renderPreferences.backgroundColor != newPreferences.backgroundColor; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
348 | this->renderPreferences = newPreferences; |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
349 | if (mainColorChanged or backgroundColorChanged) |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
350 | { |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
351 | this->compiler->build(this->model, this->documents, this->renderPreferences); |
55 | 352 | this->setupBackgroundColor(); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
353 | } |
18 | 354 | this->update(); |
355 | } | |
56
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
356 | |
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
357 | glm::vec3 PartRenderer::viewport(const glm::vec3& point, float width, float height) |
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
358 | { |
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
359 | return { |
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
360 | width * 0.5 * (point.x + 1), |
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
361 | height * 0.5 * (-point.y + 1), |
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
362 | 0 |
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
363 | }; |
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
364 | } |