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