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