-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathglbox.cpp
More file actions
105 lines (93 loc) · 2.38 KB
/
Copy pathglbox.cpp
File metadata and controls
105 lines (93 loc) · 2.38 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "glbox.h"
#include <QTimerEvent>
GLBox::GLBox(QWidget *parent) :
QGLWidget (QGLFormat(QGL::SampleBuffers), parent),
xRotation(0),
yRotation(0),
zRotation(0),
timerUpdate(new QBasicTimer())
{
timerUpdate->start(33, this);
}
void GLBox::setRotation(double x, double y, double z)
{
xRotation = static_cast<int>(x * 16);
yRotation = static_cast<int>(y * 16);
zRotation = static_cast<int>(z * 16);
}
void GLBox::initializeGL()
{
qglClearColor(Qt::black);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
static GLfloat lightPosition[4] = {0, 0, 10, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
}
void GLBox::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
#ifdef QT_OPENGL_ES_1
glOrthof(-2, +2, -2, +2, 1.0, 15.0);
#else
glOrtho(-2, +2, -2, +2, 1.0, 15.0);
#endif
glMatrixMode(GL_MODELVIEW);
}
void GLBox::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -10.0);
glRotatef(xRotation / 16.0, 1.0, 0.0, 0.0);
glRotatef(yRotation / 16.0, 0.0, 1.0, 0.0);
glRotatef(zRotation / 16.0, 0.0, 0.0, 1.0);
draw();
}
void GLBox::draw()
{
qglColor(Qt::red);
glBegin(GL_QUADS);
glNormal3f(0,0,-1);
glVertex3f(-1,-1,0);
glVertex3f(-1,1,0);
glVertex3f(1,1,0);
glVertex3f(1,-1,0);
glEnd();
glBegin(GL_TRIANGLES);
glNormal3f(0,-1,0.707);
glVertex3f(-1,-1,0);
glVertex3f(1,-1,0);
glVertex3f(0,0,1.2);
glEnd();
glBegin(GL_TRIANGLES);
glNormal3f(1,0, 0.707);
glVertex3f(1,-1,0);
glVertex3f(1,1,0);
glVertex3f(0,0,1.2);
glEnd();
glBegin(GL_TRIANGLES);
glNormal3f(0,1,0.707);
glVertex3f(1,1,0);
glVertex3f(-1,1,0);
glVertex3f(0,0,1.2);
glEnd();
glBegin(GL_TRIANGLES);
glNormal3f(-1,0,0.707);
glVertex3f(-1,1,0);
glVertex3f(-1,-1,0);
glVertex3f(0,0,1.2);
glEnd();
}
void GLBox::timerEvent(QTimerEvent *event)
{
if (timerUpdate->timerId() == event->timerId()) {
updateGL();
} else
QObject::timerEvent(event);
}