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