Sat, 14 Dec 2019 22:36:06 +0200
added missing files
17 | 1 | #include <QMouseEvent> |
2 | #include <GL/glut.h> // teapot | |
3 | #include "partrenderer.h" | |
4 | ||
5 | PartRenderer::PartRenderer(QWidget* parent) : | |
6 | QOpenGLWidget{parent} | |
7 | { | |
8 | this->setMouseTracking(true); | |
9 | } | |
10 | ||
11 | void PartRenderer::initializeGL() | |
12 | { | |
13 | this->initializeOpenGLFunctions(); | |
14 | if (this->glGetError() != GL_NO_ERROR) | |
15 | { | |
16 | abort(); | |
17 | } | |
18 | this->initializeLighting(); | |
19 | this->initialized = true; | |
20 | this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30); | |
21 | this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330); | |
22 | } | |
23 | ||
24 | /* | |
25 | * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix. | |
26 | */ | |
27 | static QMatrix4x4 padMatrix(const QMatrix3x3& stub) | |
28 | { | |
29 | return { | |
30 | stub(0, 0), stub(0, 1), stub(0, 2), 0, | |
31 | stub(1, 0), stub(1, 1), stub(1, 2), 0, | |
32 | stub(2, 0), stub(2, 1), stub(2, 2), 0, | |
33 | 0, 0, 0, 1 | |
34 | }; | |
35 | } | |
36 | ||
37 | ||
38 | void PartRenderer::initializeLighting() | |
39 | { | |
40 | GLfloat materialShininess[] = {5.0}; | |
41 | GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0}; | |
42 | GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0}; | |
43 | glShadeModel(GL_SMOOTH); | |
44 | glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess); | |
45 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightingLevel); | |
46 | glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLightingLevel); | |
47 | glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); | |
48 | glEnable(GL_LIGHTING); | |
49 | glEnable(GL_LIGHT0); | |
50 | glEnable(GL_COLOR_MATERIAL); | |
51 | glEnable(GL_DEPTH_TEST); | |
52 | } | |
53 | ||
54 | void PartRenderer::resizeGL(int width, int height) | |
55 | { | |
56 | constexpr GLfloat near = 1.0f; | |
18 | 57 | constexpr GLfloat far = 1e+05f; |
17 | 58 | glViewport (0, 0, width, height); |
59 | glMatrixMode(GL_PROJECTION); | |
60 | glLoadIdentity(); | |
18 | 61 | gluPerspective(45.0f, static_cast<double>(width) / static_cast<double>(height), near, far); |
17 | 62 | glMatrixMode(GL_MODELVIEW); |
63 | } | |
64 | ||
65 | // https://www.codemiles.com/c-opengl-examples/drawing-teapot-using-opengl-t9010.html?mobile=on | |
66 | void PartRenderer::paintGL() | |
67 | { | |
18 | 68 | switch (this->renderStyle) |
69 | { | |
70 | case gl::RenderStyle::Normal: | |
71 | case gl::RenderStyle::BfcRedGreen: | |
72 | case gl::RenderStyle::RandomColors: | |
73 | break; | |
74 | case gl::RenderStyle::Wireframe: | |
75 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); | |
76 | break; | |
77 | } | |
17 | 78 | glMatrixMode(GL_MODELVIEW); |
79 | // clear the drawing buffer. | |
80 | glClear(GL_COLOR_BUFFER_BIT); | |
81 | // clear the identity matrix. | |
82 | glLoadIdentity(); | |
83 | // traslate the draw by z = -4.0 | |
84 | // Note this when you decrease z like -8.0 the drawing will looks far , or smaller. | |
85 | glTranslatef(0.0,0.0,-4.5); | |
86 | // Red color used to draw. | |
87 | glColor3f(0.8, 0.2, 0.1); | |
88 | glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData()); | |
89 | glutSolidTeapot(1.0); | |
90 | glFlush(); | |
91 | } | |
92 | ||
93 | static QPointF pointToPointF(const QPoint& point) | |
94 | { | |
95 | return {static_cast<qreal>(point.x()), static_cast<qreal>(point.y())}; | |
96 | } | |
97 | ||
98 | /* | |
99 | static QPoint pointFToPoint(const QPointF& point) | |
100 | { | |
101 | return {static_cast<int>(std::round(point.x())), static_cast<int>(std::round(point.y()))}; | |
102 | } | |
103 | */ | |
104 | ||
105 | void PartRenderer::mouseMoveEvent(QMouseEvent* event) | |
106 | { | |
107 | const bool left = event->buttons() & Qt::LeftButton; | |
108 | const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition; | |
109 | if (left and not move.isNull()) | |
110 | { | |
111 | const QQuaternion versor = QQuaternion::fromAxisAndAngle( | |
112 | QVector3D{static_cast<float>(move.y()), static_cast<float>(move.x()), 0.0f}, | |
113 | 0.6 * std::hypot(move.x(), move.y()) | |
114 | ); | |
115 | this->rotation = versor * this->rotation; | |
116 | this->update(); | |
117 | } | |
118 | this->lastMousePosition = pointToPointF(event->pos()); | |
119 | } | |
18 | 120 | |
121 | void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle) | |
122 | { | |
123 | this->renderStyle = newStyle; | |
124 | this->update(); | |
125 | } | |
126 |