Sun, 26 Jan 2020 00:55:36 +0200
fix remaining rendering control issues
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 | ||
26 | 26 | PartRenderer::PartRenderer(Model* model, DocumentManager* documents, const ColorTable& colorTable, QWidget* parent) : |
21 | 27 | QOpenGLWidget{parent}, |
28 | model{model}, | |
29 | documents{documents}, | |
26 | 30 | colorTable{colorTable}, |
31 | compiler{new gl::Compiler{this->colorTable, this}} | |
17 | 32 | { |
33 | this->setMouseTracking(true); | |
34 | } | |
35 | ||
26 | 36 | PartRenderer::~PartRenderer() |
37 | { | |
38 | } | |
39 | ||
17 | 40 | void PartRenderer::initializeGL() |
41 | { | |
42 | this->initializeOpenGLFunctions(); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
43 | if (glGetError() != GL_NO_ERROR) |
17 | 44 | { |
45 | abort(); | |
46 | } | |
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
|
47 | 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
|
48 | this->compiler->build(this->model, this->documents); |
17 | 49 | this->initializeLighting(); |
50 | this->initialized = true; | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
51 | this->modelQuaternion = glm::angleAxis(glm::radians(30.0f), glm::vec3{1, 0, 0}); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
52 | this->modelQuaternion *= glm::angleAxis(glm::radians(330.0f), glm::vec3{0, 1, 0}); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
53 | this->updateViewMatrix(); |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
54 | glLineWidth(2.0); |
30 | 55 | this->update(); |
17 | 56 | } |
57 | ||
58 | void PartRenderer::initializeLighting() | |
59 | { | |
60 | GLfloat materialShininess[] = {5.0}; | |
61 | GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0}; | |
62 | GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0}; | |
63 | glShadeModel(GL_SMOOTH); | |
64 | glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess); | |
65 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightingLevel); | |
66 | glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLightingLevel); | |
67 | glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); | |
68 | glEnable(GL_LIGHTING); | |
69 | glEnable(GL_LIGHT0); | |
70 | glEnable(GL_COLOR_MATERIAL); | |
71 | glEnable(GL_DEPTH_TEST); | |
72 | } | |
73 | ||
74 | void PartRenderer::resizeGL(int width, int height) | |
75 | { | |
28 | 76 | glViewport(0, 0, width, height); |
77 | this->projectionMatrix = glm::perspective( | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
78 | glm::radians(45.0f), |
28 | 79 | static_cast<float>(width) / static_cast<float>(height), |
80 | 0.1f, | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
81 | 10000.f); |
17 | 82 | } |
83 | ||
26 | 84 | static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass) |
21 | 85 | { |
86 | switch (vboClass) | |
87 | { | |
26 | 88 | case gl::ArrayClass::Lines: |
89 | case gl::ArrayClass::ConditionalLines: | |
21 | 90 | return GL_LINES; |
26 | 91 | case gl::ArrayClass::Triangles: |
21 | 92 | return GL_TRIANGLES; |
26 | 93 | case gl::ArrayClass::Quads: |
21 | 94 | return GL_QUADS; |
95 | } | |
96 | throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"}; | |
97 | } | |
98 | ||
17 | 99 | void PartRenderer::paintGL() |
100 | { | |
26 | 101 | /* |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
102 | glEnable (GL_BLEND); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
103 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
26 | 104 | */ |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
105 | glEnable (GL_DEPTH_TEST); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
106 | glShadeModel (GL_SMOOTH); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
107 | glEnable (GL_MULTISAMPLE); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
108 | glEnable (GL_LINE_SMOOTH); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
109 | glHint (GL_LINE_SMOOTH_HINT, GL_NICEST); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
110 | this->renderScene(); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
111 | } |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
112 | |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
113 | void PartRenderer::renderScene() |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
114 | { |
26 | 115 | glClearColor(0.8f, 0.8f, 0.8f, 1.0f); |
116 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
117 | glEnable(GL_DEPTH_TEST); | |
118 | glEnable(GL_LIGHTING); | |
119 | glEnable(GL_POLYGON_OFFSET_FILL); | |
120 | glPolygonOffset(1.0f, 1.0f); | |
18 | 121 | switch (this->renderStyle) |
122 | { | |
123 | case gl::RenderStyle::Normal: | |
124 | case gl::RenderStyle::BfcRedGreen: | |
125 | case gl::RenderStyle::RandomColors: | |
126 | break; | |
127 | case gl::RenderStyle::Wireframe: | |
128 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); | |
129 | break; | |
130 | } | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
131 | this->compiler->setUniformMatrix("CameraTransformation", this->projectionMatrix * this->viewMatrix * glm::mat4_cast(this->modelQuaternion)); |
26 | 132 | // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. |
133 | renderVao(gl::ArrayClass::Triangles); | |
134 | renderVao(gl::ArrayClass::Quads); | |
135 | renderVao(gl::ArrayClass::Lines); | |
136 | glDisable(GL_POLYGON_OFFSET_FILL); | |
137 | } | |
138 | ||
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
139 | void PartRenderer::updateViewMatrix() |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
140 | { |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
141 | // 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
|
142 | 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
|
143 | this->viewMatrix = glm::lookAt(glm::vec3{0, 0, z}, {0, 0, 0}, {0, -1, 0}); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
144 | } |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
145 | |
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
|
146 | void PartRenderer::renderVao(const gl::ArrayClass arrayClass) |
26 | 147 | { |
148 | this->compiler->bindVertexArray(arrayClass); | |
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
|
149 | const std::size_t vertexCount = this->compiler->vboSize(arrayClass) / gl::FLOATS_PER_VERTEX; |
26 | 150 | glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); |
151 | this->compiler->releaseVertexArray(arrayClass); | |
152 | this->checkForGLErrors(); | |
153 | } | |
154 | ||
155 | void PartRenderer::checkForGLErrors() | |
156 | { | |
157 | GLenum glError; | |
158 | QStringList errors; | |
159 | while ((glError = glGetError()) != GL_NO_ERROR) | |
21 | 160 | { |
161 | const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError))); | |
26 | 162 | errors.append(glErrorString); |
163 | } | |
164 | if (not errors.isEmpty()) | |
165 | { | |
21 | 166 | QMessageBox::critical( |
167 | this, | |
168 | tr("Rendering error"), | |
26 | 169 | QString{"Failed to render.\n%1"}.arg(errors.join("\n"))); |
21 | 170 | } |
17 | 171 | } |
172 | ||
173 | void PartRenderer::mouseMoveEvent(QMouseEvent* event) | |
174 | { | |
175 | const bool left = event->buttons() & Qt::LeftButton; | |
176 | const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition; | |
177 | if (left and not move.isNull()) | |
178 | { | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
179 | // 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
|
180 | // 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
|
181 | // 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
|
182 | const auto scalar = 0.006f; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
183 | const float move_x = static_cast<float>(move.x()); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
184 | const float move_y = static_cast<float>(move.y()); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
185 | 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
|
186 | 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
|
187 | this->modelQuaternion = q_x * q_y * this->modelQuaternion; |
17 | 188 | this->update(); |
189 | } | |
190 | this->lastMousePosition = pointToPointF(event->pos()); | |
191 | } | |
18 | 192 | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
193 | void PartRenderer::wheelEvent(QWheelEvent* event) |
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 | static constexpr double WHEEL_STEP = 1 / 1000.0; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
196 | const double move = (-event->angleDelta().y()) * WHEEL_STEP; |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
197 | 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
|
198 | this->updateViewMatrix(); |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
199 | this->update(); |
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 | |
18 | 202 | void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle) |
203 | { | |
204 | this->renderStyle = newStyle; | |
205 | this->update(); | |
206 | } | |
207 |