Wed, 22 Jan 2020 22:43:28 +0200
added FindGLM.cmake
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 | } | |
26 | 47 | glEnableClientState(GL_NORMAL_ARRAY); |
48 | glEnableClientState(GL_VERTEX_ARRAY); | |
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
|
49 | 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
|
50 | this->compiler->build(this->model, this->documents); |
17 | 51 | this->initializeLighting(); |
52 | this->initialized = true; | |
53 | this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30); | |
54 | this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
55 | glLineWidth(2.0); |
17 | 56 | } |
57 | ||
58 | /* | |
59 | * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix. | |
60 | */ | |
61 | static QMatrix4x4 padMatrix(const QMatrix3x3& stub) | |
62 | { | |
63 | return { | |
64 | stub(0, 0), stub(0, 1), stub(0, 2), 0, | |
65 | stub(1, 0), stub(1, 1), stub(1, 2), 0, | |
66 | stub(2, 0), stub(2, 1), stub(2, 2), 0, | |
67 | 0, 0, 0, 1 | |
68 | }; | |
69 | } | |
70 | ||
71 | ||
72 | void PartRenderer::initializeLighting() | |
73 | { | |
74 | GLfloat materialShininess[] = {5.0}; | |
75 | GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0}; | |
76 | GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0}; | |
77 | glShadeModel(GL_SMOOTH); | |
78 | glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess); | |
79 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightingLevel); | |
80 | glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLightingLevel); | |
81 | glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); | |
82 | glEnable(GL_LIGHTING); | |
83 | glEnable(GL_LIGHT0); | |
84 | glEnable(GL_COLOR_MATERIAL); | |
85 | glEnable(GL_DEPTH_TEST); | |
86 | } | |
87 | ||
88 | void PartRenderer::resizeGL(int width, int height) | |
89 | { | |
28 | 90 | glViewport(0, 0, width, height); |
91 | this->projectionMatrix = glm::perspective( | |
92 | glm::radians(90.0f), | |
93 | static_cast<float>(width) / static_cast<float>(height), | |
94 | 0.1f, | |
95 | 100.f); | |
17 | 96 | } |
97 | ||
26 | 98 | static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass) |
21 | 99 | { |
100 | switch (vboClass) | |
101 | { | |
26 | 102 | case gl::ArrayClass::Lines: |
103 | case gl::ArrayClass::ConditionalLines: | |
21 | 104 | return GL_LINES; |
26 | 105 | case gl::ArrayClass::Triangles: |
21 | 106 | return GL_TRIANGLES; |
26 | 107 | case gl::ArrayClass::Quads: |
21 | 108 | return GL_QUADS; |
109 | } | |
110 | throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"}; | |
111 | } | |
112 | ||
17 | 113 | void PartRenderer::paintGL() |
114 | { | |
26 | 115 | /* |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
116 | glEnable (GL_BLEND); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
117 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
26 | 118 | */ |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
119 | glEnable (GL_DEPTH_TEST); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
120 | glShadeModel (GL_SMOOTH); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
121 | glEnable (GL_MULTISAMPLE); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
122 | glEnable (GL_LINE_SMOOTH); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
123 | glHint (GL_LINE_SMOOTH_HINT, GL_NICEST); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
124 | this->renderScene(); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
125 | } |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
126 | |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
127 | void PartRenderer::renderScene() |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
128 | { |
26 | 129 | glClearColor(0.8f, 0.8f, 0.8f, 1.0f); |
130 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
131 | glMatrixMode(GL_MODELVIEW); | |
132 | glEnable(GL_DEPTH_TEST); | |
133 | glEnable(GL_LIGHTING); | |
134 | glLoadIdentity(); | |
135 | //glTranslated(0.0, 0.0, -4.5 * this->compiler->modelDistance()); | |
136 | //glTranslated(0.0, 0.0, -4.5); | |
137 | //glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData()); | |
138 | //xyz(glTranslatef, -this->compiler->modelCenter()); | |
139 | auto rotationMatrix = padMatrix(this->rotation.toRotationMatrix()); | |
140 | rotationMatrix(2, 3) = 0; | |
141 | glEnable(GL_POLYGON_OFFSET_FILL); | |
142 | glPolygonOffset(1.0f, 1.0f); | |
18 | 143 | switch (this->renderStyle) |
144 | { | |
145 | case gl::RenderStyle::Normal: | |
146 | case gl::RenderStyle::BfcRedGreen: | |
147 | case gl::RenderStyle::RandomColors: | |
148 | break; | |
149 | case gl::RenderStyle::Wireframe: | |
150 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); | |
151 | break; | |
152 | } | |
28 | 153 | this->compiler->setUniform("CameraTransformation", gl::toQMatrix(this->projectionMatrix * this->viewMatrix)); |
26 | 154 | // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. |
155 | renderVao(gl::ArrayClass::Triangles); | |
156 | renderVao(gl::ArrayClass::Quads); | |
157 | renderVao(gl::ArrayClass::Lines); | |
158 | glDisable(GL_POLYGON_OFFSET_FILL); | |
159 | } | |
160 | ||
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
|
161 | void PartRenderer::renderVao(const gl::ArrayClass arrayClass) |
26 | 162 | { |
163 | 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
|
164 | const std::size_t vertexCount = this->compiler->vboSize(arrayClass) / gl::FLOATS_PER_VERTEX; |
26 | 165 | glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); |
166 | this->compiler->releaseVertexArray(arrayClass); | |
167 | this->checkForGLErrors(); | |
168 | } | |
169 | ||
170 | void PartRenderer::checkForGLErrors() | |
171 | { | |
172 | GLenum glError; | |
173 | QStringList errors; | |
174 | while ((glError = glGetError()) != GL_NO_ERROR) | |
21 | 175 | { |
176 | const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError))); | |
26 | 177 | errors.append(glErrorString); |
178 | } | |
179 | if (not errors.isEmpty()) | |
180 | { | |
21 | 181 | QMessageBox::critical( |
182 | this, | |
183 | tr("Rendering error"), | |
26 | 184 | QString{"Failed to render.\n%1"}.arg(errors.join("\n"))); |
21 | 185 | } |
17 | 186 | } |
187 | ||
188 | void PartRenderer::mouseMoveEvent(QMouseEvent* event) | |
189 | { | |
190 | const bool left = event->buttons() & Qt::LeftButton; | |
191 | const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition; | |
192 | if (left and not move.isNull()) | |
193 | { | |
194 | const QQuaternion versor = QQuaternion::fromAxisAndAngle( | |
195 | QVector3D{static_cast<float>(move.y()), static_cast<float>(move.x()), 0.0f}, | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
196 | 0.6f * static_cast<float>(std::hypot(move.x(), move.y())) |
17 | 197 | ); |
198 | this->rotation = versor * this->rotation; | |
28 | 199 | QVector3D cameraPosition = this->rotation.rotatedVector({0, 0, 4.5}); |
200 | glm::vec3 cameraPosition_glm = {cameraPosition.x(), cameraPosition.y(), cameraPosition.z()}; | |
201 | this->viewMatrix = glm::lookAt(cameraPosition_glm, {0, 0, 0}, {0, -1, 0}); | |
17 | 202 | this->update(); |
203 | } | |
204 | this->lastMousePosition = pointToPointF(event->pos()); | |
205 | } | |
18 | 206 | |
21 | 207 | void PartRenderer::setCompiler(gl::Compiler* compiler) |
208 | { | |
209 | this->compiler = compiler; | |
210 | } | |
211 | ||
18 | 212 | void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle) |
213 | { | |
214 | this->renderStyle = newStyle; | |
215 | this->update(); | |
216 | } | |
217 |