Sun, 19 Jan 2020 02:54:48 +0200
commit work on GL rendering
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 | } | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
113 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
17 | 114 | glMatrixMode(GL_MODELVIEW); |
115 | // clear the drawing buffer. | |
21 | 116 | glClearColor(1.0f, 1.0f, 1.0f, 1.0f); |
117 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
118 | glEnable(GL_DEPTH_TEST); | |
119 | glEnable(GL_LIGHTING); | |
17 | 120 | // clear the identity matrix. |
121 | glLoadIdentity(); | |
122 | // traslate the draw by z = -4.0 | |
123 | // Note this when you decrease z like -8.0 the drawing will looks far , or smaller. | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
124 | glTranslatef(0.0, 0.0, -4.5 * this->compiler->modelDistance()); |
17 | 125 | glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData()); |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
126 | xyz(glTranslatef, -this->compiler->modelCenter()); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
127 | glEnableClientState(GL_NORMAL_ARRAY); |
21 | 128 | glEnableClientState(GL_VERTEX_ARRAY); |
129 | glEnableClientState(GL_COLOR_ARRAY); | |
130 | for (const gl::VboClass vboClass : {gl::VboClass::Lines, gl::VboClass::Triangles, gl::VboClass::Quads}) | |
131 | { | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
132 | 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
|
133 | 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
|
134 | 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
|
135 | const std::size_t count = this->compiler->vboSize({vboClass, gl::VboSubclass::Surfaces}) / 3_z; |
21 | 136 | glBindBuffer(GL_ARRAY_BUFFER, vboSurfaces); |
137 | glVertexPointer(3, GL_FLOAT, 0, nullptr); | |
138 | glBindBuffer(GL_ARRAY_BUFFER, vboColors); | |
139 | glColorPointer(4, GL_FLOAT, 0, nullptr); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
140 | glBindBuffer(GL_ARRAY_BUFFER, vboNormals); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
141 | glNormalPointer(GL_FLOAT, 0, nullptr); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
142 | glDrawArrays(getGlTypeForVboClass(vboClass), 0, static_cast<GLsizei>(count)); |
21 | 143 | } |
144 | glBindBuffer(GL_ARRAY_BUFFER, 0); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
145 | glDisableClientState(GL_NORMAL_ARRAY); |
21 | 146 | glDisableClientState(GL_VERTEX_ARRAY); |
147 | glDisableClientState(GL_COLOR_ARRAY); | |
148 | //glFlush(); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
149 | const GLenum glError = this->glGetError(); |
21 | 150 | if (glError != GL_NO_ERROR) |
151 | { | |
152 | const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError))); | |
153 | QMessageBox::critical( | |
154 | this, | |
155 | tr("Rendering error"), | |
156 | QString{"Failed to render: %1"}.arg(glErrorString)); | |
157 | } | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
158 | glDisable(GL_CULL_FACE); |
17 | 159 | } |
160 | ||
161 | static QPointF pointToPointF(const QPoint& point) | |
162 | { | |
163 | return {static_cast<qreal>(point.x()), static_cast<qreal>(point.y())}; | |
164 | } | |
165 | ||
166 | /* | |
167 | static QPoint pointFToPoint(const QPointF& point) | |
168 | { | |
169 | return {static_cast<int>(std::round(point.x())), static_cast<int>(std::round(point.y()))}; | |
170 | } | |
171 | */ | |
172 | ||
173 | void PartRenderer::mouseMoveEvent(QMouseEvent* event) | |
174 | { | |
175 | const bool left = event->buttons() & Qt::LeftButton; | |
176 | const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition; | |
177 | if (left and not move.isNull()) | |
178 | { | |
179 | const QQuaternion versor = QQuaternion::fromAxisAndAngle( | |
180 | 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
|
181 | 0.6f * static_cast<float>(std::hypot(move.x(), move.y())) |
17 | 182 | ); |
183 | this->rotation = versor * this->rotation; | |
184 | this->update(); | |
185 | } | |
186 | this->lastMousePosition = pointToPointF(event->pos()); | |
187 | } | |
18 | 188 | |
21 | 189 | void PartRenderer::setCompiler(gl::Compiler* compiler) |
190 | { | |
191 | this->compiler = compiler; | |
192 | } | |
193 | ||
18 | 194 | void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle) |
195 | { | |
196 | this->renderStyle = newStyle; | |
197 | this->update(); | |
198 | } | |
199 |