Sun, 19 Jan 2020 14:25:43 +0200
added license
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 | ||
17 | 19 | #include <QMouseEvent> |
24 | 20 | #include <GL/glut.h> |
17 | 21 | #include "partrenderer.h" |
22 | ||
21 | 23 | PartRenderer::PartRenderer(Model* model, DocumentManager* documents, QWidget* parent) : |
24 | QOpenGLWidget{parent}, | |
25 | model{model}, | |
26 | documents{documents}, | |
27 | compiler{new gl::Compiler{this}} | |
17 | 28 | { |
29 | this->setMouseTracking(true); | |
30 | } | |
31 | ||
32 | void PartRenderer::initializeGL() | |
33 | { | |
34 | this->initializeOpenGLFunctions(); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
35 | if (glGetError() != GL_NO_ERROR) |
17 | 36 | { |
37 | abort(); | |
38 | } | |
39 | this->initializeLighting(); | |
40 | this->initialized = true; | |
41 | this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30); | |
42 | this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330); | |
21 | 43 | this->compiler->build(this->model, this->documents); |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
44 | glLineWidth(2.0); |
17 | 45 | } |
46 | ||
47 | /* | |
48 | * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix. | |
49 | */ | |
50 | static QMatrix4x4 padMatrix(const QMatrix3x3& stub) | |
51 | { | |
52 | return { | |
53 | stub(0, 0), stub(0, 1), stub(0, 2), 0, | |
54 | stub(1, 0), stub(1, 1), stub(1, 2), 0, | |
55 | stub(2, 0), stub(2, 1), stub(2, 2), 0, | |
56 | 0, 0, 0, 1 | |
57 | }; | |
58 | } | |
59 | ||
60 | ||
61 | void PartRenderer::initializeLighting() | |
62 | { | |
63 | GLfloat materialShininess[] = {5.0}; | |
64 | GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0}; | |
65 | GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0}; | |
66 | glShadeModel(GL_SMOOTH); | |
67 | glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess); | |
68 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightingLevel); | |
69 | glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLightingLevel); | |
70 | glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); | |
71 | glEnable(GL_LIGHTING); | |
72 | glEnable(GL_LIGHT0); | |
73 | glEnable(GL_COLOR_MATERIAL); | |
74 | glEnable(GL_DEPTH_TEST); | |
75 | } | |
76 | ||
77 | void PartRenderer::resizeGL(int width, int height) | |
78 | { | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
79 | constexpr GLdouble near = 1.0; |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
80 | constexpr GLdouble far = 1e+05; |
17 | 81 | glViewport (0, 0, width, height); |
82 | glMatrixMode(GL_PROJECTION); | |
83 | glLoadIdentity(); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
84 | gluPerspective(45.0, static_cast<double>(width) / static_cast<double>(height), near, far); |
17 | 85 | glMatrixMode(GL_MODELVIEW); |
86 | } | |
87 | ||
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
88 | static GLenum getGlTypeForVboClass(const gl::VboClass vboClass) |
21 | 89 | { |
90 | switch (vboClass) | |
91 | { | |
92 | case gl::VboClass::Lines: | |
93 | case gl::VboClass::ConditionalLines: | |
94 | return GL_LINES; | |
95 | case gl::VboClass::Triangles: | |
96 | return GL_TRIANGLES; | |
97 | case gl::VboClass::Quads: | |
98 | return GL_QUADS; | |
99 | } | |
100 | throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"}; | |
101 | } | |
102 | ||
17 | 103 | // https://www.codemiles.com/c-opengl-examples/drawing-teapot-using-opengl-t9010.html?mobile=on |
21 | 104 | #include <QMessageBox> |
17 | 105 | void PartRenderer::paintGL() |
106 | { | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
107 | glEnable (GL_BLEND); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
108 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
109 | glEnable (GL_POLYGON_OFFSET_FILL); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
110 | glPolygonOffset (1.0f, 1.0f); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
111 | glEnable (GL_DEPTH_TEST); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
112 | glShadeModel (GL_SMOOTH); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
113 | glEnable (GL_MULTISAMPLE); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
114 | glEnable (GL_LINE_SMOOTH); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
115 | glHint (GL_LINE_SMOOTH_HINT, GL_NICEST); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
116 | this->renderScene(); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
117 | } |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
118 | |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
119 | void PartRenderer::renderScene() |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
120 | { |
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 | } | |
17 | 131 | glMatrixMode(GL_MODELVIEW); |
21 | 132 | glClearColor(1.0f, 1.0f, 1.0f, 1.0f); |
133 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
134 | glEnable(GL_DEPTH_TEST); | |
135 | glEnable(GL_LIGHTING); | |
17 | 136 | glLoadIdentity(); |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
137 | glTranslatef(0.0, 0.0, -4.5 * this->compiler->modelDistance()); |
17 | 138 | glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData()); |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
139 | xyz(glTranslatef, -this->compiler->modelCenter()); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
140 | glEnableClientState(GL_NORMAL_ARRAY); |
21 | 141 | glEnableClientState(GL_VERTEX_ARRAY); |
142 | glEnableClientState(GL_COLOR_ARRAY); | |
143 | for (const gl::VboClass vboClass : {gl::VboClass::Lines, gl::VboClass::Triangles, gl::VboClass::Quads}) | |
144 | { | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
145 | const GLuint vboSurfaces = this->compiler->vbo({vboClass, gl::VboSubclass::Surfaces}); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
146 | const GLuint vboColors = this->compiler->vbo({vboClass, gl::VboSubclass::RegularColors}); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
147 | const GLuint vboNormals = this->compiler->vbo({vboClass, gl::VboSubclass::Normals}); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
148 | const std::size_t count = this->compiler->vboSize({vboClass, gl::VboSubclass::Surfaces}) / 3_z; |
21 | 149 | glBindBuffer(GL_ARRAY_BUFFER, vboSurfaces); |
150 | glVertexPointer(3, GL_FLOAT, 0, nullptr); | |
151 | glBindBuffer(GL_ARRAY_BUFFER, vboColors); | |
152 | glColorPointer(4, GL_FLOAT, 0, nullptr); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
153 | glBindBuffer(GL_ARRAY_BUFFER, vboNormals); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
154 | glNormalPointer(GL_FLOAT, 0, nullptr); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
155 | glDrawArrays(getGlTypeForVboClass(vboClass), 0, static_cast<GLsizei>(count)); |
21 | 156 | } |
157 | glBindBuffer(GL_ARRAY_BUFFER, 0); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
158 | glDisableClientState(GL_NORMAL_ARRAY); |
21 | 159 | glDisableClientState(GL_VERTEX_ARRAY); |
160 | glDisableClientState(GL_COLOR_ARRAY); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
161 | const GLenum glError = this->glGetError(); |
21 | 162 | if (glError != GL_NO_ERROR) |
163 | { | |
164 | const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError))); | |
165 | QMessageBox::critical( | |
166 | this, | |
167 | tr("Rendering error"), | |
168 | QString{"Failed to render: %1"}.arg(glErrorString)); | |
169 | } | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
170 | glDisable(GL_CULL_FACE); |
17 | 171 | } |
172 | ||
173 | static QPointF pointToPointF(const QPoint& point) | |
174 | { | |
175 | return {static_cast<qreal>(point.x()), static_cast<qreal>(point.y())}; | |
176 | } | |
177 | ||
178 | /* | |
179 | static QPoint pointFToPoint(const QPointF& point) | |
180 | { | |
181 | return {static_cast<int>(std::round(point.x())), static_cast<int>(std::round(point.y()))}; | |
182 | } | |
183 | */ | |
184 | ||
185 | void PartRenderer::mouseMoveEvent(QMouseEvent* event) | |
186 | { | |
187 | const bool left = event->buttons() & Qt::LeftButton; | |
188 | const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition; | |
189 | if (left and not move.isNull()) | |
190 | { | |
191 | const QQuaternion versor = QQuaternion::fromAxisAndAngle( | |
192 | 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
|
193 | 0.6f * static_cast<float>(std::hypot(move.x(), move.y())) |
17 | 194 | ); |
195 | this->rotation = versor * this->rotation; | |
196 | this->update(); | |
197 | } | |
198 | this->lastMousePosition = pointToPointF(event->pos()); | |
199 | } | |
18 | 200 | |
21 | 201 | void PartRenderer::setCompiler(gl::Compiler* compiler) |
202 | { | |
203 | this->compiler = compiler; | |
204 | } | |
205 | ||
18 | 206 | void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle) |
207 | { | |
208 | this->renderStyle = newStyle; | |
209 | this->update(); | |
210 | } | |
211 |