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