Wed, 01 Jan 2020 17:45:56 +0200
things
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(); | |
17 | if (this->glGetError() != GL_NO_ERROR) | |
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); |
17 | 26 | } |
27 | ||
28 | /* | |
29 | * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix. | |
30 | */ | |
31 | static QMatrix4x4 padMatrix(const QMatrix3x3& stub) | |
32 | { | |
33 | return { | |
34 | stub(0, 0), stub(0, 1), stub(0, 2), 0, | |
35 | stub(1, 0), stub(1, 1), stub(1, 2), 0, | |
36 | stub(2, 0), stub(2, 1), stub(2, 2), 0, | |
37 | 0, 0, 0, 1 | |
38 | }; | |
39 | } | |
40 | ||
41 | ||
42 | void PartRenderer::initializeLighting() | |
43 | { | |
44 | GLfloat materialShininess[] = {5.0}; | |
45 | GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0}; | |
46 | GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0}; | |
47 | glShadeModel(GL_SMOOTH); | |
48 | glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess); | |
49 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightingLevel); | |
50 | glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLightingLevel); | |
51 | glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); | |
52 | glEnable(GL_LIGHTING); | |
53 | glEnable(GL_LIGHT0); | |
54 | glEnable(GL_COLOR_MATERIAL); | |
55 | glEnable(GL_DEPTH_TEST); | |
56 | } | |
57 | ||
58 | void PartRenderer::resizeGL(int width, int height) | |
59 | { | |
60 | constexpr GLfloat near = 1.0f; | |
18 | 61 | constexpr GLfloat far = 1e+05f; |
17 | 62 | glViewport (0, 0, width, height); |
63 | glMatrixMode(GL_PROJECTION); | |
64 | glLoadIdentity(); | |
18 | 65 | gluPerspective(45.0f, static_cast<double>(width) / static_cast<double>(height), near, far); |
17 | 66 | glMatrixMode(GL_MODELVIEW); |
67 | } | |
68 | ||
21 | 69 | static int getGlTypeForVboClass(const gl::VboClass vboClass) |
70 | { | |
71 | switch (vboClass) | |
72 | { | |
73 | case gl::VboClass::Lines: | |
74 | case gl::VboClass::ConditionalLines: | |
75 | return GL_LINES; | |
76 | case gl::VboClass::Triangles: | |
77 | return GL_TRIANGLES; | |
78 | case gl::VboClass::Quads: | |
79 | return GL_QUADS; | |
80 | } | |
81 | throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"}; | |
82 | } | |
83 | ||
17 | 84 | // https://www.codemiles.com/c-opengl-examples/drawing-teapot-using-opengl-t9010.html?mobile=on |
21 | 85 | #include <QMessageBox> |
17 | 86 | void PartRenderer::paintGL() |
87 | { | |
18 | 88 | switch (this->renderStyle) |
89 | { | |
90 | case gl::RenderStyle::Normal: | |
91 | case gl::RenderStyle::BfcRedGreen: | |
92 | case gl::RenderStyle::RandomColors: | |
93 | break; | |
94 | case gl::RenderStyle::Wireframe: | |
95 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); | |
96 | break; | |
97 | } | |
17 | 98 | glMatrixMode(GL_MODELVIEW); |
99 | // clear the drawing buffer. | |
21 | 100 | glClearColor(1.0f, 1.0f, 1.0f, 1.0f); |
101 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
102 | glEnable(GL_DEPTH_TEST); | |
103 | glEnable(GL_LIGHTING); | |
17 | 104 | // clear the identity matrix. |
105 | glLoadIdentity(); | |
106 | // traslate the draw by z = -4.0 | |
107 | // Note this when you decrease z like -8.0 the drawing will looks far , or smaller. | |
108 | glTranslatef(0.0,0.0,-4.5); | |
109 | // Red color used to draw. | |
110 | glColor3f(0.8, 0.2, 0.1); | |
111 | glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData()); | |
21 | 112 | //glutSolidTeapot(1.0); |
113 | glEnableClientState(GL_VERTEX_ARRAY); | |
114 | glEnableClientState(GL_COLOR_ARRAY); | |
115 | for (const gl::VboClass vboClass : {gl::VboClass::Lines, gl::VboClass::Triangles, gl::VboClass::Quads}) | |
116 | { | |
117 | const int vboSurfaces = this->compiler->vbo({vboClass, gl::VboSubclass::Surfaces}); | |
118 | const int vboColors = this->compiler->vbo({vboClass, gl::VboSubclass::RegularColors}); | |
119 | const int vboNormals = this->compiler->vbo({vboClass, gl::VboSubclass::Normals}); | |
120 | const int count = this->compiler->vboSize({vboClass, gl::VboSubclass::Surfaces}) / 3; | |
121 | glBindBuffer(GL_ARRAY_BUFFER, vboSurfaces); | |
122 | glVertexPointer(3, GL_FLOAT, 0, nullptr); | |
123 | glBindBuffer(GL_ARRAY_BUFFER, vboColors); | |
124 | glColorPointer(4, GL_FLOAT, 0, nullptr); | |
125 | //glBindBuffer(GL_ARRAY_BUFFER, vboNormals); | |
126 | //glNormalPointer(GL_FLOAT, 0, nullptr); | |
127 | glDrawArrays(getGlTypeForVboClass(vboClass), 0, count); | |
128 | } | |
129 | glBindBuffer(GL_ARRAY_BUFFER, 0); | |
130 | glDisableClientState(GL_VERTEX_ARRAY); | |
131 | glDisableClientState(GL_COLOR_ARRAY); | |
132 | ||
133 | //glFlush(); | |
134 | const int glError = this->glGetError(); | |
135 | if (glError != GL_NO_ERROR) | |
136 | { | |
137 | const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError))); | |
138 | QMessageBox::critical( | |
139 | this, | |
140 | tr("Rendering error"), | |
141 | QString{"Failed to render: %1"}.arg(glErrorString)); | |
142 | } | |
17 | 143 | } |
144 | ||
145 | static QPointF pointToPointF(const QPoint& point) | |
146 | { | |
147 | return {static_cast<qreal>(point.x()), static_cast<qreal>(point.y())}; | |
148 | } | |
149 | ||
150 | /* | |
151 | static QPoint pointFToPoint(const QPointF& point) | |
152 | { | |
153 | return {static_cast<int>(std::round(point.x())), static_cast<int>(std::round(point.y()))}; | |
154 | } | |
155 | */ | |
156 | ||
157 | void PartRenderer::mouseMoveEvent(QMouseEvent* event) | |
158 | { | |
159 | const bool left = event->buttons() & Qt::LeftButton; | |
160 | const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition; | |
161 | if (left and not move.isNull()) | |
162 | { | |
163 | const QQuaternion versor = QQuaternion::fromAxisAndAngle( | |
164 | QVector3D{static_cast<float>(move.y()), static_cast<float>(move.x()), 0.0f}, | |
165 | 0.6 * std::hypot(move.x(), move.y()) | |
166 | ); | |
167 | this->rotation = versor * this->rotation; | |
168 | this->update(); | |
169 | } | |
170 | this->lastMousePosition = pointToPointF(event->pos()); | |
171 | } | |
18 | 172 | |
21 | 173 | void PartRenderer::setCompiler(gl::Compiler* compiler) |
174 | { | |
175 | this->compiler = compiler; | |
176 | } | |
177 | ||
18 | 178 | void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle) |
179 | { | |
180 | this->renderStyle = newStyle; | |
181 | this->update(); | |
182 | } | |
183 |