-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglwidget.cpp
More file actions
115 lines (95 loc) · 2.44 KB
/
Copy pathglwidget.cpp
File metadata and controls
115 lines (95 loc) · 2.44 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
106
107
108
109
110
111
112
113
114
115
#include <QtGui>
#include <QtOpenGL>
#include <math.h>
#include "glwidget.h"
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
glt = new GLOperationThread(this);
xRot = 0;
yRot = 0;
zRot = 0;
setAutoBufferSwap(false);
}
GLWidget::~GLWidget()
{
}
GLOperationThread *GLWidget::getGLOperationThread()
{
return glt;
}
void GLWidget::showEvent ( QShowEvent * event )
{
if (glt->isRunning() == false)
{
glt->start();
connect(this, SIGNAL(paintRequested()), glt, SLOT(render()), Qt::BlockingQueuedConnection);
connect(this, SIGNAL(resizeRequested(QSize)), glt, SLOT(resizeViewport(QSize)), Qt::BlockingQueuedConnection);
connect(this, SIGNAL(xRotationChanged(int)), glt, SLOT(setXRotation(int)), Qt::BlockingQueuedConnection);
connect(this, SIGNAL(yRotationChanged(int)), glt, SLOT(setYRotation(int)), Qt::BlockingQueuedConnection);
connect(this, SIGNAL(zRotationChanged(int)), glt, SLOT(setZRotation(int)), Qt::BlockingQueuedConnection);
}
}
void GLWidget::resizeEvent(QResizeEvent *evt)
{
emit resizeRequested(evt->size());
}
void GLWidget::paintEvent(QPaintEvent *)
{
emit paintRequested();
}
void GLWidget::closeEvent(QCloseEvent *evt)
{
glt->quit();
glt->wait();
delete glt;
GLWidget::closeEvent(evt);
}
void GLWidget::mousePressEvent(QMouseEvent *event)
{
lastPos = event->pos();
}
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
int dx = event->x() - lastPos.x();
int dy = event->y() - lastPos.y();
if (event->buttons() & Qt::LeftButton) {
setXRotation(xRot + 3 * dy);
setYRotation(yRot + 3 * dx);
} else if (event->buttons() & Qt::RightButton) {
setXRotation(xRot + 3 * dy);
setZRotation(zRot + 3 * dx);
}
lastPos = event->pos();
}
static void qNormalizeAngle(int &angle)
{
while (angle < 0)
angle += 360 * 16;
while (angle > 360 * 16)
angle -= 360 * 16;
}
void GLWidget::setXRotation(int angle)
{
qNormalizeAngle(angle);
if (angle != xRot) {
xRot = angle;
emit xRotationChanged(angle);
}
}
void GLWidget::setYRotation(int angle)
{
qNormalizeAngle(angle);
if (angle != yRot) {
yRot = angle;
emit yRotationChanged(angle);
}
}
void GLWidget::setZRotation(int angle)
{
qNormalizeAngle(angle);
if (angle != zRot) {
zRot = angle;
emit zRotationChanged(angle);
}
}