src/gl/partrenderer.cpp

Sat, 14 Dec 2019 22:36:06 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 14 Dec 2019 22:36:06 +0200
changeset 19
ed9685f44ab3
parent 18
918b6c0f8b5b
child 21
0133e565e072
permissions
-rw-r--r--

added missing files

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

mercurial