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