-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenglwidget.cpp
executable file
·92 lines (68 loc) · 2.81 KB
/
openglwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "openglwidget.h"
OpenGLWidget::OpenGLWidget(QWidget *parent) :
QOpenGLWidget(parent),
outerRingAngle(0),
middleRingAngle(0),
innerRingAngle(0) {}
OpenGLWidget::~OpenGLWidget() {}
void OpenGLWidget::initializeGL()
{
// Load OpenGL functionality
Q_ASSERT(initializeOpenGLFunctions());
// Set scene's clear color to light gray
glClearColor(0.9f, 0.9f, 0.9f, 0.5f);
// Enable objects to be in front of / hide each other
glEnable(GL_DEPTH_TEST);
// Create ring models
const QString objectFilePath = ":/objects/ring.obj";
this->outerRingModel = new Model(objectFilePath, ":/fragshader/mint.frag");
this->middleRingModel = new Model(objectFilePath, ":/fragshader/storm.frag");
this->innerRingModel = new Model(objectFilePath, ":/fragshader/beige.frag");
// Mount camera
this->viewMatrix.lookAt(
QVector3D(0.0, 0.0, 10.0), // Eye
QVector3D(0.0, 0.0, 0.0), // Focal Point
QVector3D(0.0, 0.1, 0.0) // Up vector
);
}
void OpenGLWidget::paintGL()
{
// Clear screen //
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
// Calculate current projection matrix //
const float aspectRatio = float(this->width()) / float(this->height());
QMatrix4x4 projectionMatrix;
projectionMatrix.perspective(
45, // Vertical angle
aspectRatio, // Aspect ratio
0.1, // Near plane
1000.0 // Far plane
);
// Calculate ring rotations //
this->outerRingRotationMatrix = QMatrix4x4();
this->outerRingRotationMatrix.rotate(this->outerRingAngle, QVector3D(1,0,0));
this->middleRingRotationMatrix = QMatrix4x4();
this->middleRingRotationMatrix.rotate(this->middleRingAngle, QVector3D(0,1,0));
this->middleRingRotationMatrix = this->outerRingRotationMatrix * this->middleRingRotationMatrix;
this->innerRingRotationMatrix = QMatrix4x4();
this->innerRingRotationMatrix.rotate(this->innerRingAngle, QVector3D(1,0,0));
this->innerRingRotationMatrix = this->middleRingRotationMatrix * this->innerRingRotationMatrix;
// Draw to scene //
this->outerRingModel->draw(projectionMatrix, this->viewMatrix, this->outerRingRotationMatrix, 3.0);
this->middleRingModel->draw(projectionMatrix, this->viewMatrix, this->middleRingRotationMatrix, 1.9);
this->innerRingModel->draw(projectionMatrix, this->viewMatrix, this->innerRingRotationMatrix, 1.0);
this->update();
}
void OpenGLWidget::setOuterRingAngle(const int angle)
{
this->outerRingAngle = angle;
}
void OpenGLWidget::setMiddleRingAngle(const int angle)
{
this->middleRingAngle = angle;
}
void OpenGLWidget::setInnerRingAngle(const int angle)
{
this->innerRingAngle = angle;
}