Sat, 01 Feb 2020 15:33:57 +0200
wired renderstyle up
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(); |
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->build(this->model, this->documents); |
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(); |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
57 | glLineWidth(2.0); |
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 | { | |
26 | 89 | /* |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
90 | glEnable (GL_BLEND); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
91 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
26 | 92 | */ |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
93 | glEnable (GL_DEPTH_TEST); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
94 | glShadeModel (GL_SMOOTH); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
95 | glEnable (GL_MULTISAMPLE); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
96 | glEnable (GL_LINE_SMOOTH); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
97 | glHint (GL_LINE_SMOOTH_HINT, GL_NICEST); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
98 | this->renderScene(); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
99 | } |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
100 | |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
101 | void PartRenderer::renderScene() |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
102 | { |
26 | 103 | glClearColor(0.8f, 0.8f, 0.8f, 1.0f); |
104 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
105 | glEnable(GL_DEPTH_TEST); | |
106 | glEnable(GL_POLYGON_OFFSET_FILL); | |
107 | glPolygonOffset(1.0f, 1.0f); | |
18 | 108 | switch (this->renderStyle) |
109 | { | |
110 | case gl::RenderStyle::Normal: | |
37 | 111 | this->setFragmentStyle(gl::FragmentStyle::Normal); |
112 | this->renderAllArrays(); | |
113 | break; | |
18 | 114 | case gl::RenderStyle::BfcRedGreen: |
37 | 115 | glEnable(GL_CULL_FACE); |
116 | glCullFace(GL_BACK); | |
117 | this->setFragmentStyle(gl::FragmentStyle::BfcGreen); | |
118 | renderVao(gl::ArrayClass::Triangles); | |
119 | renderVao(gl::ArrayClass::Quads); | |
120 | glCullFace(GL_FRONT); | |
121 | this->setFragmentStyle(gl::FragmentStyle::BfcRed); | |
122 | renderVao(gl::ArrayClass::Triangles); | |
123 | renderVao(gl::ArrayClass::Quads); | |
124 | glDisable(GL_CULL_FACE); | |
125 | this->setFragmentStyle(gl::FragmentStyle::Normal); | |
126 | renderVao(gl::ArrayClass::Lines); | |
18 | 127 | case gl::RenderStyle::RandomColors: |
37 | 128 | this->setFragmentStyle(gl::FragmentStyle::RandomColors); |
129 | this->renderAllArrays(); | |
18 | 130 | break; |
131 | case gl::RenderStyle::Wireframe: | |
132 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); | |
37 | 133 | this->setFragmentStyle(gl::FragmentStyle::Normal); |
134 | this->renderAllArrays(); | |
18 | 135 | break; |
136 | } | |
37 | 137 | glDisable(GL_POLYGON_OFFSET_FILL); |
138 | } | |
139 | ||
140 | void PartRenderer::renderAllArrays() | |
141 | { | |
26 | 142 | // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. |
143 | renderVao(gl::ArrayClass::Triangles); | |
144 | renderVao(gl::ArrayClass::Quads); | |
145 | renderVao(gl::ArrayClass::Lines); | |
146 | } | |
147 | ||
37 | 148 | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
149 | void PartRenderer::updateViewMatrix() |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
150 | { |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
151 | // 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
|
152 | 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
|
153 | 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
|
154 | this->compiler->setUniformMatrix("viewMatrix", this->viewMatrix); |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
155 | } |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
156 | |
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
|
157 | void PartRenderer::renderVao(const gl::ArrayClass arrayClass) |
26 | 158 | { |
159 | this->compiler->bindVertexArray(arrayClass); | |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
160 | const std::size_t vertexCount = this->compiler->vertexCount(arrayClass); |
26 | 161 | glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); |
162 | this->compiler->releaseVertexArray(arrayClass); | |
163 | this->checkForGLErrors(); | |
164 | } | |
165 | ||
166 | void PartRenderer::checkForGLErrors() | |
167 | { | |
168 | GLenum glError; | |
169 | QStringList errors; | |
170 | while ((glError = glGetError()) != GL_NO_ERROR) | |
21 | 171 | { |
172 | const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError))); | |
26 | 173 | errors.append(glErrorString); |
174 | } | |
175 | if (not errors.isEmpty()) | |
176 | { | |
21 | 177 | QMessageBox::critical( |
178 | this, | |
179 | tr("Rendering error"), | |
26 | 180 | QString{"Failed to render.\n%1"}.arg(errors.join("\n"))); |
21 | 181 | } |
17 | 182 | } |
183 | ||
184 | void PartRenderer::mouseMoveEvent(QMouseEvent* event) | |
185 | { | |
186 | const bool left = event->buttons() & Qt::LeftButton; | |
187 | const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition; | |
188 | if (left and not move.isNull()) | |
189 | { | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
190 | // 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
|
191 | // 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
|
192 | // 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
|
193 | const auto scalar = 0.006f; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
194 | const float move_x = static_cast<float>(move.x()); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
195 | const float move_y = static_cast<float>(move.y()); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
196 | 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
|
197 | 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
|
198 | this->modelQuaternion = q_x * q_y * this->modelQuaternion; |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
199 | this->compiler->setUniformMatrix("modelMatrix", glm::mat4_cast(this->modelQuaternion)); |
17 | 200 | this->update(); |
201 | } | |
202 | this->lastMousePosition = pointToPointF(event->pos()); | |
203 | } | |
18 | 204 | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
205 | void PartRenderer::wheelEvent(QWheelEvent* event) |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
206 | { |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
207 | static constexpr double WHEEL_STEP = 1 / 1000.0; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
208 | const double move = (-event->angleDelta().y()) * WHEEL_STEP; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
209 | 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
|
210 | this->updateViewMatrix(); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
211 | this->update(); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
212 | } |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
213 | |
37 | 214 | /** |
215 | * @brief Changes the color of rendered fragments | |
216 | * @param newFragmentStyle new fragment style to use | |
217 | */ | |
218 | void PartRenderer::setFragmentStyle(gl::FragmentStyle newFragmentStyle) | |
219 | { | |
220 | this->compiler->setUniform("fragmentStyle", static_cast<int>(newFragmentStyle)); | |
221 | } | |
222 | ||
223 | /** | |
224 | * @brief Changes the way the scene is rendered | |
225 | * @param newStyle new render style to use | |
226 | */ | |
18 | 227 | void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle) |
228 | { | |
229 | this->renderStyle = newStyle; | |
230 | this->update(); | |
231 | } | |
232 |