Thu, 23 Jan 2020 00:29:10 +0200
it works!
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; | |
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); |
30 | 54 | this->update(); |
17 | 55 | } |
56 | ||
57 | void PartRenderer::initializeLighting() | |
58 | { | |
59 | GLfloat materialShininess[] = {5.0}; | |
60 | GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0}; | |
61 | GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0}; | |
62 | glShadeModel(GL_SMOOTH); | |
63 | glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess); | |
64 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightingLevel); | |
65 | glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLightingLevel); | |
66 | glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); | |
67 | glEnable(GL_LIGHTING); | |
68 | glEnable(GL_LIGHT0); | |
69 | glEnable(GL_COLOR_MATERIAL); | |
70 | glEnable(GL_DEPTH_TEST); | |
71 | } | |
72 | ||
73 | void PartRenderer::resizeGL(int width, int height) | |
74 | { | |
28 | 75 | glViewport(0, 0, width, height); |
76 | this->projectionMatrix = glm::perspective( | |
77 | glm::radians(90.0f), | |
78 | static_cast<float>(width) / static_cast<float>(height), | |
79 | 0.1f, | |
80 | 100.f); | |
17 | 81 | } |
82 | ||
26 | 83 | static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass) |
21 | 84 | { |
85 | switch (vboClass) | |
86 | { | |
26 | 87 | case gl::ArrayClass::Lines: |
88 | case gl::ArrayClass::ConditionalLines: | |
21 | 89 | return GL_LINES; |
26 | 90 | case gl::ArrayClass::Triangles: |
21 | 91 | return GL_TRIANGLES; |
26 | 92 | case gl::ArrayClass::Quads: |
21 | 93 | return GL_QUADS; |
94 | } | |
95 | throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"}; | |
96 | } | |
97 | ||
17 | 98 | void PartRenderer::paintGL() |
99 | { | |
26 | 100 | /* |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
101 | glEnable (GL_BLEND); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
102 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
26 | 103 | */ |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
104 | glEnable (GL_DEPTH_TEST); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
105 | glShadeModel (GL_SMOOTH); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
106 | glEnable (GL_MULTISAMPLE); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
107 | glEnable (GL_LINE_SMOOTH); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
108 | glHint (GL_LINE_SMOOTH_HINT, GL_NICEST); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
109 | this->renderScene(); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
110 | } |
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 | void PartRenderer::renderScene() |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
113 | { |
26 | 114 | glClearColor(0.8f, 0.8f, 0.8f, 1.0f); |
115 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
116 | glEnable(GL_DEPTH_TEST); | |
117 | glEnable(GL_LIGHTING); | |
118 | glEnable(GL_POLYGON_OFFSET_FILL); | |
119 | glPolygonOffset(1.0f, 1.0f); | |
18 | 120 | switch (this->renderStyle) |
121 | { | |
122 | case gl::RenderStyle::Normal: | |
123 | case gl::RenderStyle::BfcRedGreen: | |
124 | case gl::RenderStyle::RandomColors: | |
125 | break; | |
126 | case gl::RenderStyle::Wireframe: | |
127 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); | |
128 | break; | |
129 | } | |
30 | 130 | this->compiler->setUniformMatrix("CameraTransformation", this->projectionMatrix * this->viewMatrix); |
26 | 131 | // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. |
132 | renderVao(gl::ArrayClass::Triangles); | |
133 | renderVao(gl::ArrayClass::Quads); | |
134 | renderVao(gl::ArrayClass::Lines); | |
135 | glDisable(GL_POLYGON_OFFSET_FILL); | |
136 | } | |
137 | ||
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
|
138 | void PartRenderer::renderVao(const gl::ArrayClass arrayClass) |
26 | 139 | { |
140 | 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
|
141 | const std::size_t vertexCount = this->compiler->vboSize(arrayClass) / gl::FLOATS_PER_VERTEX; |
26 | 142 | glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); |
143 | this->compiler->releaseVertexArray(arrayClass); | |
144 | this->checkForGLErrors(); | |
145 | } | |
146 | ||
147 | void PartRenderer::checkForGLErrors() | |
148 | { | |
149 | GLenum glError; | |
150 | QStringList errors; | |
151 | while ((glError = glGetError()) != GL_NO_ERROR) | |
21 | 152 | { |
153 | const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError))); | |
26 | 154 | errors.append(glErrorString); |
155 | } | |
156 | if (not errors.isEmpty()) | |
157 | { | |
21 | 158 | QMessageBox::critical( |
159 | this, | |
160 | tr("Rendering error"), | |
26 | 161 | QString{"Failed to render.\n%1"}.arg(errors.join("\n"))); |
21 | 162 | } |
17 | 163 | } |
164 | ||
165 | void PartRenderer::mouseMoveEvent(QMouseEvent* event) | |
166 | { | |
167 | const bool left = event->buttons() & Qt::LeftButton; | |
168 | const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition; | |
169 | if (left and not move.isNull()) | |
170 | { | |
30 | 171 | // FIXME: find a more elegant way to do this |
17 | 172 | const QQuaternion versor = QQuaternion::fromAxisAndAngle( |
30 | 173 | 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
|
174 | 0.6f * static_cast<float>(std::hypot(move.x(), move.y())) |
17 | 175 | ); |
176 | this->rotation = versor * this->rotation; | |
30 | 177 | QVector3D cameraPosition = this->rotation.rotatedVector({0, 0, static_cast<float>(2 + 2 * this->compiler->modelDistance())}); |
28 | 178 | glm::vec3 cameraPosition_glm = {cameraPosition.x(), cameraPosition.y(), cameraPosition.z()}; |
179 | this->viewMatrix = glm::lookAt(cameraPosition_glm, {0, 0, 0}, {0, -1, 0}); | |
17 | 180 | this->update(); |
181 | } | |
182 | this->lastMousePosition = pointToPointF(event->pos()); | |
183 | } | |
18 | 184 | |
185 | void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle) | |
186 | { | |
187 | this->renderStyle = newStyle; | |
188 | this->update(); | |
189 | } | |
190 |