Wed, 22 Jan 2020 00:23:29 +0200
at least VAOs work now
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> |
17 | 20 | #include <QMouseEvent> |
26 | 21 | #include <QMessageBox> |
17 | 22 | #include "partrenderer.h" |
23 | ||
26 | 24 | |
25 | static const char* vertexShaderSource = R"( | |
26 | #version 330 core | |
27 | ||
28 | layout(location=0) in vec3 position; | |
29 | layout(location=1) in vec4 color; | |
30 | out vec4 vColor; | |
31 | uniform mat4 CameraTransformation; | |
32 | ||
33 | void main() | |
34 | { | |
35 | vColor = color; | |
36 | gl_Position = CameraTransformation * vec4(position, 1.0); | |
37 | } | |
38 | )"; | |
39 | ||
40 | static const char* fragmentShaderSource = R"( | |
41 | #version 330 core | |
42 | ||
43 | in vec4 vColor; | |
44 | out vec4 fColor; | |
45 | ||
46 | void main() | |
47 | { | |
48 | fColor = vColor; | |
49 | } | |
50 | )"; | |
51 | ||
52 | PartRenderer::PartRenderer(Model* model, DocumentManager* documents, const ColorTable& colorTable, QWidget* parent) : | |
21 | 53 | QOpenGLWidget{parent}, |
54 | model{model}, | |
55 | documents{documents}, | |
26 | 56 | colorTable{colorTable}, |
57 | compiler{new gl::Compiler{this->colorTable, this}} | |
17 | 58 | { |
59 | this->setMouseTracking(true); | |
60 | } | |
61 | ||
26 | 62 | PartRenderer::~PartRenderer() |
63 | { | |
64 | delete this->objects.program; | |
65 | } | |
66 | ||
17 | 67 | void PartRenderer::initializeGL() |
68 | { | |
69 | this->initializeOpenGLFunctions(); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
70 | if (glGetError() != GL_NO_ERROR) |
17 | 71 | { |
72 | abort(); | |
73 | } | |
26 | 74 | glEnableClientState(GL_NORMAL_ARRAY); |
75 | glEnableClientState(GL_VERTEX_ARRAY); | |
76 | //this->compiler->initialize(); | |
77 | //this->compiler->build(this->model, this->documents); | |
17 | 78 | this->initializeLighting(); |
79 | this->initialized = true; | |
80 | this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30); | |
81 | this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
82 | glLineWidth(2.0); |
26 | 83 | this->objects.program = new QOpenGLShaderProgram; |
84 | this->objects.program->create(); | |
85 | this->checkForGLErrors(); | |
86 | this->objects.program->addShaderFromSourceCode(QOpenGLShader::Vertex, ::vertexShaderSource); | |
87 | this->checkForGLErrors(); | |
88 | this->objects.program->addShaderFromSourceCode(QOpenGLShader::Fragment, ::fragmentShaderSource); | |
89 | this->checkForGLErrors(); | |
90 | this->objects.program->link(); | |
91 | this->checkForGLErrors(); | |
92 | this->objects.program->bind(); | |
93 | this->checkForGLErrors(); | |
94 | this->objects.buffer.create(); | |
95 | this->checkForGLErrors(); | |
96 | this->objects.buffer.bind(); | |
97 | this->checkForGLErrors(); | |
98 | this->objects.buffer.setUsagePattern(QOpenGLBuffer::StaticDraw); | |
99 | this->checkForGLErrors(); | |
100 | /* | |
101 | GLfloat data[] = { | |
102 | 20.0f, 20.0f, 6.0f, 1.0f, 0.0f, 0.0f, 1.0f, | |
103 | 30.0f, 20.0f, 6.0f, 0.0f, 1.0f, 0.0f, 1.0f, | |
104 | 30.0f, 30.0f, 6.0f, 0.0f, 0.0f, 1.0f, 1.0f, | |
105 | }; | |
106 | */ | |
107 | GLfloat data[] = { | |
108 | 0.00f, 0.75f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, | |
109 | -0.75f, -0.75f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, | |
110 | 0.75f, -0.75f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f | |
111 | }; | |
112 | this->objects.buffer.allocate(data, sizeof data); | |
113 | this->checkForGLErrors(); | |
114 | this->objects.vertexArray.create(); | |
115 | this->checkForGLErrors(); | |
116 | this->objects.vertexArray.bind(); | |
117 | this->checkForGLErrors(); | |
118 | this->objects.program->enableAttributeArray(0); | |
119 | this->checkForGLErrors(); | |
120 | this->objects.program->enableAttributeArray(1); | |
121 | this->checkForGLErrors(); | |
122 | this->objects.program->setAttributeBuffer(0, GL_FLOAT, 0, 3); | |
123 | this->checkForGLErrors(); | |
124 | this->objects.program->setAttributeBuffer(1, GL_FLOAT, 3, 4); | |
125 | this->checkForGLErrors(); | |
126 | this->objects.vertexArray.release(); | |
127 | this->checkForGLErrors(); | |
128 | this->objects.buffer.release(); | |
129 | this->checkForGLErrors(); | |
130 | this->objects.program->release(); | |
131 | this->checkForGLErrors(); | |
17 | 132 | } |
133 | ||
134 | /* | |
135 | * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix. | |
136 | */ | |
137 | static QMatrix4x4 padMatrix(const QMatrix3x3& stub) | |
138 | { | |
139 | return { | |
140 | stub(0, 0), stub(0, 1), stub(0, 2), 0, | |
141 | stub(1, 0), stub(1, 1), stub(1, 2), 0, | |
142 | stub(2, 0), stub(2, 1), stub(2, 2), 0, | |
143 | 0, 0, 0, 1 | |
144 | }; | |
145 | } | |
146 | ||
147 | ||
148 | void PartRenderer::initializeLighting() | |
149 | { | |
150 | GLfloat materialShininess[] = {5.0}; | |
151 | GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0}; | |
152 | GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0}; | |
153 | glShadeModel(GL_SMOOTH); | |
154 | glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess); | |
155 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightingLevel); | |
156 | glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLightingLevel); | |
157 | glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); | |
158 | glEnable(GL_LIGHTING); | |
159 | glEnable(GL_LIGHT0); | |
160 | glEnable(GL_COLOR_MATERIAL); | |
161 | glEnable(GL_DEPTH_TEST); | |
162 | } | |
163 | ||
164 | void PartRenderer::resizeGL(int width, int height) | |
165 | { | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
166 | constexpr GLdouble near = 1.0; |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
167 | constexpr GLdouble far = 1e+05; |
17 | 168 | glViewport (0, 0, width, height); |
169 | glMatrixMode(GL_PROJECTION); | |
170 | glLoadIdentity(); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
171 | gluPerspective(45.0, static_cast<double>(width) / static_cast<double>(height), near, far); |
17 | 172 | glMatrixMode(GL_MODELVIEW); |
173 | } | |
174 | ||
26 | 175 | static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass) |
21 | 176 | { |
177 | switch (vboClass) | |
178 | { | |
26 | 179 | case gl::ArrayClass::Lines: |
180 | case gl::ArrayClass::ConditionalLines: | |
21 | 181 | return GL_LINES; |
26 | 182 | case gl::ArrayClass::Triangles: |
21 | 183 | return GL_TRIANGLES; |
26 | 184 | case gl::ArrayClass::Quads: |
21 | 185 | return GL_QUADS; |
186 | } | |
187 | throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"}; | |
188 | } | |
189 | ||
17 | 190 | void PartRenderer::paintGL() |
191 | { | |
26 | 192 | /* |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
193 | glEnable (GL_BLEND); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
194 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
26 | 195 | */ |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
196 | glEnable (GL_DEPTH_TEST); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
197 | glShadeModel (GL_SMOOTH); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
198 | glEnable (GL_MULTISAMPLE); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
199 | glEnable (GL_LINE_SMOOTH); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
200 | glHint (GL_LINE_SMOOTH_HINT, GL_NICEST); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
201 | this->renderScene(); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
202 | } |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
203 | |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
204 | void PartRenderer::renderScene() |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
205 | { |
26 | 206 | glClearColor(0.8f, 0.8f, 0.8f, 1.0f); |
207 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
208 | glMatrixMode(GL_MODELVIEW); | |
209 | glEnable(GL_DEPTH_TEST); | |
210 | glEnable(GL_LIGHTING); | |
211 | glLoadIdentity(); | |
212 | //glTranslated(0.0, 0.0, -4.5 * this->compiler->modelDistance()); | |
213 | //glTranslated(0.0, 0.0, -4.5); | |
214 | //glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData()); | |
215 | //xyz(glTranslatef, -this->compiler->modelCenter()); | |
216 | auto rotationMatrix = padMatrix(this->rotation.toRotationMatrix()); | |
217 | rotationMatrix(2, 3) = 0; | |
218 | glEnable(GL_POLYGON_OFFSET_FILL); | |
219 | glPolygonOffset(1.0f, 1.0f); | |
18 | 220 | switch (this->renderStyle) |
221 | { | |
222 | case gl::RenderStyle::Normal: | |
223 | case gl::RenderStyle::BfcRedGreen: | |
224 | case gl::RenderStyle::RandomColors: | |
225 | break; | |
226 | case gl::RenderStyle::Wireframe: | |
227 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); | |
228 | break; | |
229 | } | |
26 | 230 | this->objects.program->bind(); |
231 | this->checkForGLErrors(); | |
232 | const int cameraTransformationUniform = glGetUniformLocation(this->objects.program->programId(), "CameraTransformation"); | |
233 | this->checkForGLErrors(); | |
234 | this->objects.program->setUniformValue(cameraTransformationUniform, rotationMatrix); | |
235 | this->checkForGLErrors(); | |
236 | this->objects.vertexArray.bind(); | |
237 | this->checkForGLErrors(); | |
238 | glDrawArrays(GL_TRIANGLES, 0, 3); | |
239 | this->checkForGLErrors(); | |
240 | this->objects.vertexArray.release(); | |
241 | this->checkForGLErrors(); | |
242 | this->objects.program->release(); | |
243 | this->checkForGLErrors(); | |
244 | #if 0 | |
245 | // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. | |
246 | renderVao(gl::ArrayClass::Triangles); | |
247 | renderVao(gl::ArrayClass::Quads); | |
248 | renderVao(gl::ArrayClass::Lines); | |
249 | #endif | |
250 | glDisable(GL_POLYGON_OFFSET_FILL); | |
251 | } | |
252 | ||
253 | void PartRenderer::renderVao(const gl::ArrayClass /*arrayClass*/) | |
254 | { | |
255 | /* | |
256 | this->compiler->bindVertexArray(arrayClass); | |
257 | const std::size_t vertexCount = this->compiler->vboSize({arrayClass, gl::VboSubclass::VertexData}) / gl::FLOATS_PER_VERTEX; | |
258 | glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); | |
259 | this->compiler->releaseVertexArray(arrayClass); | |
260 | this->checkForGLErrors(); | |
261 | */ | |
262 | } | |
263 | ||
264 | void PartRenderer::checkForGLErrors() | |
265 | { | |
266 | GLenum glError; | |
267 | QStringList errors; | |
268 | while ((glError = glGetError()) != GL_NO_ERROR) | |
21 | 269 | { |
270 | const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError))); | |
26 | 271 | errors.append(glErrorString); |
272 | } | |
273 | if (not errors.isEmpty()) | |
274 | { | |
21 | 275 | QMessageBox::critical( |
276 | this, | |
277 | tr("Rendering error"), | |
26 | 278 | QString{"Failed to render.\n%1"}.arg(errors.join("\n"))); |
21 | 279 | } |
17 | 280 | } |
281 | ||
282 | void PartRenderer::mouseMoveEvent(QMouseEvent* event) | |
283 | { | |
284 | const bool left = event->buttons() & Qt::LeftButton; | |
285 | const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition; | |
286 | if (left and not move.isNull()) | |
287 | { | |
288 | const QQuaternion versor = QQuaternion::fromAxisAndAngle( | |
289 | 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
|
290 | 0.6f * static_cast<float>(std::hypot(move.x(), move.y())) |
17 | 291 | ); |
292 | this->rotation = versor * this->rotation; | |
293 | this->update(); | |
294 | } | |
295 | this->lastMousePosition = pointToPointF(event->pos()); | |
296 | } | |
18 | 297 | |
21 | 298 | void PartRenderer::setCompiler(gl::Compiler* compiler) |
299 | { | |
300 | this->compiler = compiler; | |
301 | } | |
302 | ||
18 | 303 | void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle) |
304 | { | |
305 | this->renderStyle = newStyle; | |
306 | this->update(); | |
307 | } | |
308 |