src/gl/partrenderer.cpp

changeset 17
a5111f4e6412
child 18
918b6c0f8b5b
equal deleted inserted replaced
16:aeb5f203b3eb 17:a5111f4e6412
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 // https://stackoverflow.com/a/12943456
25 static void perspectiveGL(GLdouble fovY, GLdouble aspect, GLdouble zNear, GLdouble zFar)
26 {
27 //fH = tan( (fovY / 2) / 180 * pi ) * zNear;
28 const GLdouble fH = std::tan(fovY / 360 * pi) * zNear;
29 const GLdouble fW = fH * aspect;
30 glFrustum(-fW, fW, -fH, fH, zNear, zFar);
31 }
32
33 /*
34 * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix.
35 */
36 static QMatrix4x4 padMatrix(const QMatrix3x3& stub)
37 {
38 return {
39 stub(0, 0), stub(0, 1), stub(0, 2), 0,
40 stub(1, 0), stub(1, 1), stub(1, 2), 0,
41 stub(2, 0), stub(2, 1), stub(2, 2), 0,
42 0, 0, 0, 1
43 };
44 }
45
46
47 void PartRenderer::initializeLighting()
48 {
49 GLfloat materialShininess[] = {5.0};
50 GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0};
51 GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0};
52 glShadeModel(GL_SMOOTH);
53 glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess);
54 glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightingLevel);
55 glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLightingLevel);
56 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
57 glEnable(GL_LIGHTING);
58 glEnable(GL_LIGHT0);
59 glEnable(GL_COLOR_MATERIAL);
60 glEnable(GL_DEPTH_TEST);
61 }
62
63 void PartRenderer::resizeGL(int width, int height)
64 {
65 constexpr GLfloat near = 1.0f;
66 constexpr GLfloat far = 1.0e+05f;
67 glViewport (0, 0, width, height);
68 glMatrixMode(GL_PROJECTION);
69 glLoadIdentity();
70 ::perspectiveGL(45.0f, static_cast<double>(width) / static_cast<double>(height), near, far);
71 glMatrixMode(GL_MODELVIEW);
72 }
73
74 // https://www.codemiles.com/c-opengl-examples/drawing-teapot-using-opengl-t9010.html?mobile=on
75 void PartRenderer::paintGL()
76 {
77 glMatrixMode(GL_MODELVIEW);
78 // clear the drawing buffer.
79 glClear(GL_COLOR_BUFFER_BIT);
80 // clear the identity matrix.
81 glLoadIdentity();
82 // traslate the draw by z = -4.0
83 // Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
84 glTranslatef(0.0,0.0,-4.5);
85 // Red color used to draw.
86 glColor3f(0.8, 0.2, 0.1);
87 glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData());
88 glutSolidTeapot(1.0);
89 glFlush();
90 }
91
92 static QPointF pointToPointF(const QPoint& point)
93 {
94 return {static_cast<qreal>(point.x()), static_cast<qreal>(point.y())};
95 }
96
97 /*
98 static QPoint pointFToPoint(const QPointF& point)
99 {
100 return {static_cast<int>(std::round(point.x())), static_cast<int>(std::round(point.y()))};
101 }
102 */
103
104 void PartRenderer::mouseMoveEvent(QMouseEvent* event)
105 {
106 const bool left = event->buttons() & Qt::LeftButton;
107 const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition;
108 if (left and not move.isNull())
109 {
110 const QQuaternion versor = QQuaternion::fromAxisAndAngle(
111 QVector3D{static_cast<float>(move.y()), static_cast<float>(move.x()), 0.0f},
112 0.6 * std::hypot(move.x(), move.y())
113 );
114 this->rotation = versor * this->rotation;
115 this->update();
116 }
117 this->lastMousePosition = pointToPointF(event->pos());
118 }

mercurial