Thu, 06 Feb 2020 20:33:05 +0200
added the pick scene
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> |
17 | 24 | #include "partrenderer.h" |
25 | ||
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
26 | 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
|
27 | 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
|
28 | 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
|
29 | 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
|
30 | QWidget* parent) : |
21 | 31 | QOpenGLWidget{parent}, |
32 | model{model}, | |
33 | documents{documents}, | |
26 | 34 | colorTable{colorTable}, |
35 | compiler{new gl::Compiler{this->colorTable, this}} | |
17 | 36 | { |
37 | this->setMouseTracking(true); | |
38 | } | |
39 | ||
26 | 40 | PartRenderer::~PartRenderer() |
41 | { | |
42 | } | |
43 | ||
17 | 44 | void PartRenderer::initializeGL() |
45 | { | |
46 | this->initializeOpenGLFunctions(); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
47 | if (glGetError() != GL_NO_ERROR) |
17 | 48 | { |
49 | abort(); | |
50 | } | |
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
|
51 | this->compiler->initialize(); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
52 | this->compiler->build(this->model, this->documents, this->renderPreferences); |
17 | 53 | this->initialized = true; |
32 | 54 | this->modelQuaternion = glm::angleAxis(glm::radians(30.0f), glm::vec3{-1, 0, 0}); |
55 | this->modelQuaternion *= glm::angleAxis(glm::radians(225.0f), glm::vec3{-0, 1, 0}); | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
56 | this->updateViewMatrix(); |
30 | 57 | this->update(); |
17 | 58 | } |
59 | ||
60 | void PartRenderer::resizeGL(int width, int height) | |
61 | { | |
28 | 62 | glViewport(0, 0, width, height); |
63 | this->projectionMatrix = glm::perspective( | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
64 | glm::radians(45.0f), |
28 | 65 | static_cast<float>(width) / static_cast<float>(height), |
66 | 0.1f, | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
67 | 10000.f); |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
68 | this->compiler->setUniformMatrix("projectionMatrix", this->projectionMatrix); |
17 | 69 | } |
70 | ||
26 | 71 | static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass) |
21 | 72 | { |
73 | switch (vboClass) | |
74 | { | |
26 | 75 | case gl::ArrayClass::Lines: |
76 | case gl::ArrayClass::ConditionalLines: | |
21 | 77 | return GL_LINES; |
26 | 78 | case gl::ArrayClass::Triangles: |
21 | 79 | return GL_TRIANGLES; |
26 | 80 | case gl::ArrayClass::Quads: |
21 | 81 | return GL_QUADS; |
82 | } | |
83 | throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"}; | |
84 | } | |
85 | ||
17 | 86 | void PartRenderer::paintGL() |
87 | { | |
26 | 88 | /* |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
89 | glEnable (GL_BLEND); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
90 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
26 | 91 | */ |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
92 | glEnable (GL_DEPTH_TEST); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
93 | glShadeModel (GL_SMOOTH); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
94 | glEnable (GL_MULTISAMPLE); |
46 | 95 | 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
|
96 | { |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
97 | glEnable(GL_LINE_SMOOTH); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
98 | glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
99 | } |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
100 | else { |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
101 | glDisable(GL_LINE_SMOOTH); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
102 | } |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
103 | this->renderScene(); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
104 | } |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
105 | |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
106 | void PartRenderer::renderScene() |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
107 | { |
40
30cb5e836736
added configurable background color
Teemu Piippo <teemu@hecknology.net>
parents:
39
diff
changeset
|
108 | const QColor& backgroundColor = this->renderPreferences.backgroundColor; |
46 | 109 | if (this->renderPreferences.style != gl::RenderStyle::PickScene) |
110 | { | |
111 | glClearColor( | |
112 | static_cast<float>(backgroundColor.redF()), | |
113 | static_cast<float>(backgroundColor.greenF()), | |
114 | static_cast<float>(backgroundColor.blueF()), | |
115 | 1.0f); | |
116 | this->compiler->setUniform("useLighting", true); | |
117 | } | |
118 | else | |
119 | { | |
120 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
121 | this->compiler->setUniform("useLighting", false); | |
122 | } | |
26 | 123 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
124 | glEnable(GL_DEPTH_TEST); | |
125 | glEnable(GL_POLYGON_OFFSET_FILL); | |
126 | glPolygonOffset(1.0f, 1.0f); | |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
127 | glLineWidth(this->renderPreferences.lineThickness); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
128 | switch (this->renderPreferences.style) |
18 | 129 | { |
130 | case gl::RenderStyle::Normal: | |
37 | 131 | this->setFragmentStyle(gl::FragmentStyle::Normal); |
132 | this->renderAllArrays(); | |
133 | break; | |
18 | 134 | case gl::RenderStyle::BfcRedGreen: |
37 | 135 | glEnable(GL_CULL_FACE); |
136 | glCullFace(GL_BACK); | |
137 | this->setFragmentStyle(gl::FragmentStyle::BfcGreen); | |
138 | renderVao(gl::ArrayClass::Triangles); | |
139 | renderVao(gl::ArrayClass::Quads); | |
140 | glCullFace(GL_FRONT); | |
141 | this->setFragmentStyle(gl::FragmentStyle::BfcRed); | |
142 | renderVao(gl::ArrayClass::Triangles); | |
143 | renderVao(gl::ArrayClass::Quads); | |
144 | glDisable(GL_CULL_FACE); | |
145 | this->setFragmentStyle(gl::FragmentStyle::Normal); | |
146 | renderVao(gl::ArrayClass::Lines); | |
18 | 147 | case gl::RenderStyle::RandomColors: |
37 | 148 | this->setFragmentStyle(gl::FragmentStyle::RandomColors); |
149 | this->renderAllArrays(); | |
18 | 150 | break; |
46 | 151 | case gl::RenderStyle::PickScene: |
152 | glLineWidth(3.0f); | |
153 | this->setFragmentStyle(gl::FragmentStyle::Id); | |
154 | this->renderAllArrays(); | |
155 | break; | |
18 | 156 | case gl::RenderStyle::Wireframe: |
157 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); | |
37 | 158 | this->setFragmentStyle(gl::FragmentStyle::Normal); |
159 | this->renderAllArrays(); | |
18 | 160 | break; |
161 | } | |
37 | 162 | glDisable(GL_POLYGON_OFFSET_FILL); |
163 | } | |
164 | ||
165 | void PartRenderer::renderAllArrays() | |
166 | { | |
26 | 167 | // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. |
168 | renderVao(gl::ArrayClass::Triangles); | |
169 | renderVao(gl::ArrayClass::Quads); | |
170 | renderVao(gl::ArrayClass::Lines); | |
171 | } | |
172 | ||
37 | 173 | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
174 | void PartRenderer::updateViewMatrix() |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
175 | { |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
176 | // 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
|
177 | 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
|
178 | 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
|
179 | this->compiler->setUniformMatrix("viewMatrix", this->viewMatrix); |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
180 | } |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
181 | |
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
|
182 | void PartRenderer::renderVao(const gl::ArrayClass arrayClass) |
26 | 183 | { |
184 | this->compiler->bindVertexArray(arrayClass); | |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
185 | const std::size_t vertexCount = this->compiler->vertexCount(arrayClass); |
26 | 186 | glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); |
187 | this->compiler->releaseVertexArray(arrayClass); | |
188 | this->checkForGLErrors(); | |
189 | } | |
190 | ||
191 | void PartRenderer::checkForGLErrors() | |
192 | { | |
193 | GLenum glError; | |
194 | QStringList errors; | |
195 | while ((glError = glGetError()) != GL_NO_ERROR) | |
21 | 196 | { |
197 | const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError))); | |
26 | 198 | errors.append(glErrorString); |
199 | } | |
200 | if (not errors.isEmpty()) | |
201 | { | |
21 | 202 | QMessageBox::critical( |
203 | this, | |
204 | tr("Rendering error"), | |
26 | 205 | QString{"Failed to render.\n%1"}.arg(errors.join("\n"))); |
21 | 206 | } |
17 | 207 | } |
208 | ||
209 | void PartRenderer::mouseMoveEvent(QMouseEvent* event) | |
210 | { | |
211 | const bool left = event->buttons() & Qt::LeftButton; | |
212 | const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition; | |
213 | if (left and not move.isNull()) | |
214 | { | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
215 | // 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
|
216 | // 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
|
217 | // 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
|
218 | const auto scalar = 0.006f; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
219 | const float move_x = static_cast<float>(move.x()); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
220 | const float move_y = static_cast<float>(move.y()); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
221 | 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
|
222 | 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
|
223 | this->modelQuaternion = q_x * q_y * this->modelQuaternion; |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
224 | this->compiler->setUniformMatrix("modelMatrix", glm::mat4_cast(this->modelQuaternion)); |
17 | 225 | this->update(); |
226 | } | |
227 | this->lastMousePosition = pointToPointF(event->pos()); | |
228 | } | |
18 | 229 | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
230 | void PartRenderer::wheelEvent(QWheelEvent* event) |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
231 | { |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
232 | static constexpr double WHEEL_STEP = 1 / 1000.0; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
233 | const double move = (-event->angleDelta().y()) * WHEEL_STEP; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
234 | 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
|
235 | this->updateViewMatrix(); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
236 | this->update(); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
237 | } |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
238 | |
37 | 239 | /** |
240 | * @brief Changes the color of rendered fragments | |
241 | * @param newFragmentStyle new fragment style to use | |
242 | */ | |
243 | void PartRenderer::setFragmentStyle(gl::FragmentStyle newFragmentStyle) | |
244 | { | |
245 | this->compiler->setUniform("fragmentStyle", static_cast<int>(newFragmentStyle)); | |
246 | } | |
247 | ||
248 | /** | |
249 | * @brief Changes the way the scene is rendered | |
250 | * @param newStyle new render style to use | |
251 | */ | |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
252 | void PartRenderer::setRenderPreferences(const gl::RenderPreferences& newPreferences) |
18 | 253 | { |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
254 | bool mainColorChanged = this->renderPreferences.mainColor != newPreferences.mainColor; |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
255 | bool backgroundColorChanged = this->renderPreferences.backgroundColor != newPreferences.backgroundColor; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
256 | this->renderPreferences = newPreferences; |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
257 | if (mainColorChanged or backgroundColorChanged) |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
258 | { |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
259 | this->compiler->build(this->model, this->documents, this->renderPreferences); |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
260 | } |
18 | 261 | this->update(); |
262 | } | |
263 |