1 | #include <QtGui> |
---|
2 | #include <QtOpenGL> |
---|
3 | |
---|
4 | #include <math.h> |
---|
5 | |
---|
6 | #include "glwidget.h" |
---|
7 | |
---|
8 | GLWidget::GLWidget(QWidget *parent) |
---|
9 | : QGLWidget() |
---|
10 | { |
---|
11 | printf("GLWidget::GLWidget \n"); |
---|
12 | object = 0; |
---|
13 | xRot = 0; |
---|
14 | yRot = 0; |
---|
15 | zRot = 0; |
---|
16 | |
---|
17 | trolltechGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0); |
---|
18 | trolltechPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0); |
---|
19 | printf("GLWidget::GLWidget END\n"); |
---|
20 | } |
---|
21 | |
---|
22 | void GLWidget::initialize() { |
---|
23 | printf("GLWidget::Initialise \n"); |
---|
24 | } |
---|
25 | |
---|
26 | GLWidget::~GLWidget() |
---|
27 | { |
---|
28 | printf("GLWidget::~GLWidget \n"); |
---|
29 | makeCurrent(); |
---|
30 | glDeleteLists(object, 1); |
---|
31 | printf("GLWidget::~GLWidget END\n"); |
---|
32 | } |
---|
33 | |
---|
34 | // QSize GLWidget::minimumSizeHint() const |
---|
35 | // { |
---|
36 | // printf("GLWidget::minimumSizeHint \n"); |
---|
37 | // return QSize(50, 50); |
---|
38 | // } |
---|
39 | |
---|
40 | // QSize GLWidget::sizeHint() const |
---|
41 | // { |
---|
42 | // printf("GLWidget::SizeHint \n"); |
---|
43 | // return QSize(400, 400); |
---|
44 | // } |
---|
45 | |
---|
46 | void GLWidget::setXRotation(int angle) |
---|
47 | { |
---|
48 | normalizeAngle(&angle); |
---|
49 | if (angle != xRot) { |
---|
50 | xRot = angle; |
---|
51 | // emit xRotationChanged(angle); |
---|
52 | updateGL(); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | void GLWidget::setYRotation(int angle) |
---|
57 | { |
---|
58 | normalizeAngle(&angle); |
---|
59 | if (angle != yRot) { |
---|
60 | yRot = angle; |
---|
61 | // emit yRotationChanged(angle); |
---|
62 | updateGL(); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | void GLWidget::setZRotation(int angle) |
---|
67 | { |
---|
68 | normalizeAngle(&angle); |
---|
69 | if (angle != zRot) { |
---|
70 | zRot = angle; |
---|
71 | // emit zRotationChanged(angle); |
---|
72 | updateGL(); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | void GLWidget::initializeGL() |
---|
77 | { |
---|
78 | printf("GLWidget::initializeGL \n"); |
---|
79 | qglClearColor(trolltechPurple.dark()); |
---|
80 | object = makeObject(); |
---|
81 | glShadeModel(GL_FLAT); |
---|
82 | glEnable(GL_DEPTH_TEST); |
---|
83 | glEnable(GL_CULL_FACE); |
---|
84 | printf("GLWidget::initializeGL END\n"); |
---|
85 | } |
---|
86 | |
---|
87 | void GLWidget::paintGL() |
---|
88 | { |
---|
89 | printf("GLWidget::paintGL\n"); |
---|
90 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
91 | glLoadIdentity(); |
---|
92 | glTranslated(0.0, 0.0, -10.0); |
---|
93 | glRotated(xRot / 16, 1.0, 0.0, 0.0); |
---|
94 | glRotated(yRot / 16, 0.0, 1.0, 0.0); |
---|
95 | glRotated(zRot / 16, 0.0, 0.0, 1.0); |
---|
96 | glCallList(object); |
---|
97 | printf("GLWidget::paintGL END\n"); |
---|
98 | } |
---|
99 | |
---|
100 | void GLWidget::resizeGL(int width, int height) |
---|
101 | { |
---|
102 | printf("GLWidget::resizeGL\n"); |
---|
103 | int side = qMin(width, height); |
---|
104 | glViewport((width - side) / 2, (height - side) / 2, side, side); |
---|
105 | |
---|
106 | glMatrixMode(GL_PROJECTION); |
---|
107 | glLoadIdentity(); |
---|
108 | glOrtho(-1., +1., -1., +1., -10., 15.0); |
---|
109 | glMatrixMode(GL_MODELVIEW); |
---|
110 | printf("GLWidget::resizeGL END\n"); |
---|
111 | } |
---|
112 | |
---|
113 | void GLWidget::mousePressEvent(QMouseEvent *event) |
---|
114 | { |
---|
115 | printf("GLWidget::mousePress \n"); |
---|
116 | lastPos = event->pos(); |
---|
117 | printf("GLWidget::mousePress END\n"); |
---|
118 | } |
---|
119 | |
---|
120 | void GLWidget::mouseMoveEvent(QMouseEvent *event) |
---|
121 | { |
---|
122 | printf("GLWidget::mouseEvent \n"); |
---|
123 | int dx = event->x() - lastPos.x(); |
---|
124 | int dy = event->y() - lastPos.y(); |
---|
125 | |
---|
126 | if (event->buttons() & Qt::LeftButton) { |
---|
127 | printf("GLWidget::mouseEvent Left 1\n"); |
---|
128 | setXRotation(xRot + 8 * dy); |
---|
129 | printf("GLWidget::mouseEvent Left 2\n"); |
---|
130 | setYRotation(yRot + 8 * dx); |
---|
131 | printf("GLWidget::mouseEvent Left 3\n"); |
---|
132 | } else if (event->buttons() & Qt::RightButton) { |
---|
133 | printf("GLWidget::mouseEvent Right 1\n"); |
---|
134 | setXRotation(xRot + 8 * dy); |
---|
135 | printf("GLWidget::mouseEvent Right 2\n"); |
---|
136 | setZRotation(zRot + 8 * dx); |
---|
137 | printf("GLWidget::mouseEvent Right 3\n"); |
---|
138 | } |
---|
139 | lastPos = event->pos(); |
---|
140 | printf("GLWidget::mouseEvent END\n"); |
---|
141 | } |
---|
142 | |
---|
143 | GLuint GLWidget::makeObject() |
---|
144 | { |
---|
145 | GLuint list = glGenLists(1); |
---|
146 | glNewList(list, GL_COMPILE); |
---|
147 | |
---|
148 | glBegin(GL_QUADS); |
---|
149 | |
---|
150 | GLdouble x1 = +0.06; |
---|
151 | GLdouble y1 = -0.14; |
---|
152 | GLdouble x2 = +0.14; |
---|
153 | GLdouble y2 = -0.06; |
---|
154 | GLdouble x3 = +0.08; |
---|
155 | GLdouble y3 = +0.00; |
---|
156 | GLdouble x4 = +0.30; |
---|
157 | GLdouble y4 = +0.22; |
---|
158 | |
---|
159 | quad(x1, y1, x2, y2, y2, x2, y1, x1); |
---|
160 | quad(x3, y3, x4, y4, y4, x4, y3, x3); |
---|
161 | |
---|
162 | extrude(x1, y1, x2, y2); |
---|
163 | extrude(x2, y2, y2, x2); |
---|
164 | extrude(y2, x2, y1, x1); |
---|
165 | extrude(y1, x1, x1, y1); |
---|
166 | extrude(x3, y3, x4, y4); |
---|
167 | extrude(x4, y4, y4, x4); |
---|
168 | extrude(y4, x4, y3, x3); |
---|
169 | |
---|
170 | const double Pi = 3.14159265358979323846; |
---|
171 | const int NumSectors = 200; |
---|
172 | |
---|
173 | for (int i = 0; i < NumSectors; ++i) { |
---|
174 | double angle1 = (i * 2 * Pi) / NumSectors; |
---|
175 | GLdouble x5 = 0.30 * sin(angle1); |
---|
176 | GLdouble y5 = 0.30 * cos(angle1); |
---|
177 | GLdouble x6 = 0.20 * sin(angle1); |
---|
178 | GLdouble y6 = 0.20 * cos(angle1); |
---|
179 | |
---|
180 | double angle2 = ((i + 1) * 2 * Pi) / NumSectors; |
---|
181 | GLdouble x7 = 0.20 * sin(angle2); |
---|
182 | GLdouble y7 = 0.20 * cos(angle2); |
---|
183 | GLdouble x8 = 0.30 * sin(angle2); |
---|
184 | GLdouble y8 = 0.30 * cos(angle2); |
---|
185 | |
---|
186 | quad(x5, y5, x6, y6, x7, y7, x8, y8); |
---|
187 | |
---|
188 | extrude(x6, y6, x7, y7); |
---|
189 | extrude(x8, y8, x5, y5); |
---|
190 | } |
---|
191 | |
---|
192 | glEnd(); |
---|
193 | |
---|
194 | glEndList(); |
---|
195 | return list; |
---|
196 | } |
---|
197 | |
---|
198 | void GLWidget::quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2, |
---|
199 | GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4) |
---|
200 | { |
---|
201 | qglColor(trolltechGreen); |
---|
202 | |
---|
203 | glVertex3d(x1, y1, -0.05); |
---|
204 | glVertex3d(x2, y2, -0.05); |
---|
205 | glVertex3d(x3, y3, -0.05); |
---|
206 | glVertex3d(x4, y4, -0.05); |
---|
207 | |
---|
208 | glVertex3d(x4, y4, +0.05); |
---|
209 | glVertex3d(x3, y3, +0.05); |
---|
210 | glVertex3d(x2, y2, +0.05); |
---|
211 | glVertex3d(x1, y1, +0.05); |
---|
212 | } |
---|
213 | |
---|
214 | void GLWidget::extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2) |
---|
215 | { |
---|
216 | qglColor(trolltechGreen.dark(250 + int(100 * x1))); |
---|
217 | |
---|
218 | glVertex3d(x1, y1, +0.05); |
---|
219 | glVertex3d(x2, y2, +0.05); |
---|
220 | glVertex3d(x2, y2, -0.05); |
---|
221 | glVertex3d(x1, y1, -0.05); |
---|
222 | } |
---|
223 | |
---|
224 | void GLWidget::normalizeAngle(int *angle) |
---|
225 | { |
---|
226 | while (*angle < 0) |
---|
227 | *angle += 360 * 16; |
---|
228 | while (*angle > 360 * 16) |
---|
229 | *angle -= 360 * 16; |
---|
230 | } |
---|