author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
#include <QtOpenGL> |
|
43 |
||
44 |
#include <math.h> |
|
45 |
||
46 |
#include "glwidget.h" |
|
47 |
||
48 |
GLWidget::GLWidget(QWidget *parent) |
|
49 |
: QGLWidget(parent) |
|
50 |
{ |
|
51 |
gear1 = 0; |
|
52 |
gear2 = 0; |
|
53 |
gear3 = 0; |
|
54 |
xRot = 0; |
|
55 |
yRot = 0; |
|
56 |
zRot = 0; |
|
57 |
gear1Rot = 0; |
|
58 |
||
59 |
QTimer *timer = new QTimer(this); |
|
60 |
connect(timer, SIGNAL(timeout()), this, SLOT(advanceGears())); |
|
61 |
timer->start(20); |
|
62 |
} |
|
63 |
||
64 |
GLWidget::~GLWidget() |
|
65 |
{ |
|
66 |
makeCurrent(); |
|
67 |
glDeleteLists(gear1, 1); |
|
68 |
glDeleteLists(gear2, 1); |
|
69 |
glDeleteLists(gear3, 1); |
|
70 |
} |
|
71 |
||
72 |
void GLWidget::setXRotation(int angle) |
|
73 |
{ |
|
74 |
normalizeAngle(&angle); |
|
75 |
if (angle != xRot) { |
|
76 |
xRot = angle; |
|
77 |
emit xRotationChanged(angle); |
|
78 |
updateGL(); |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
void GLWidget::setYRotation(int angle) |
|
83 |
{ |
|
84 |
normalizeAngle(&angle); |
|
85 |
if (angle != yRot) { |
|
86 |
yRot = angle; |
|
87 |
emit yRotationChanged(angle); |
|
88 |
updateGL(); |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
void GLWidget::setZRotation(int angle) |
|
93 |
{ |
|
94 |
normalizeAngle(&angle); |
|
95 |
if (angle != zRot) { |
|
96 |
zRot = angle; |
|
97 |
emit zRotationChanged(angle); |
|
98 |
updateGL(); |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
void GLWidget::initializeGL() |
|
103 |
{ |
|
104 |
static const GLfloat lightPos[4] = { 5.0f, 5.0f, 10.0f, 1.0f }; |
|
105 |
static const GLfloat reflectance1[4] = { 0.8f, 0.1f, 0.0f, 1.0f }; |
|
106 |
static const GLfloat reflectance2[4] = { 0.0f, 0.8f, 0.2f, 1.0f }; |
|
107 |
static const GLfloat reflectance3[4] = { 0.2f, 0.2f, 1.0f, 1.0f }; |
|
108 |
||
109 |
glLightfv(GL_LIGHT0, GL_POSITION, lightPos); |
|
110 |
glEnable(GL_LIGHTING); |
|
111 |
glEnable(GL_LIGHT0); |
|
112 |
glEnable(GL_DEPTH_TEST); |
|
113 |
||
114 |
gear1 = makeGear(reflectance1, 1.0, 4.0, 1.0, 0.7, 20); |
|
115 |
gear2 = makeGear(reflectance2, 0.5, 2.0, 2.0, 0.7, 10); |
|
116 |
gear3 = makeGear(reflectance3, 1.3, 2.0, 0.5, 0.7, 10); |
|
117 |
||
118 |
glEnable(GL_NORMALIZE); |
|
119 |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
|
120 |
} |
|
121 |
||
122 |
void GLWidget::paintGL() |
|
123 |
{ |
|
124 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
125 |
||
126 |
glPushMatrix(); |
|
127 |
glRotated(xRot / 16.0, 1.0, 0.0, 0.0); |
|
128 |
glRotated(yRot / 16.0, 0.0, 1.0, 0.0); |
|
129 |
glRotated(zRot / 16.0, 0.0, 0.0, 1.0); |
|
130 |
||
131 |
drawGear(gear1, -3.0, -2.0, 0.0, gear1Rot / 16.0); |
|
132 |
drawGear(gear2, +3.1, -2.0, 0.0, -2.0 * (gear1Rot / 16.0) - 9.0); |
|
133 |
||
134 |
glRotated(+90.0, 1.0, 0.0, 0.0); |
|
135 |
drawGear(gear3, -3.1, -1.8, -2.2, +2.0 * (gear1Rot / 16.0) - 2.0); |
|
136 |
||
137 |
glPopMatrix(); |
|
138 |
} |
|
139 |
||
140 |
void GLWidget::resizeGL(int width, int height) |
|
141 |
{ |
|
142 |
int side = qMin(width, height); |
|
143 |
glViewport((width - side) / 2, (height - side) / 2, side, side); |
|
144 |
||
145 |
glMatrixMode(GL_PROJECTION); |
|
146 |
glLoadIdentity(); |
|
147 |
glFrustum(-1.0, +1.0, -1.0, 1.0, 5.0, 60.0); |
|
148 |
glMatrixMode(GL_MODELVIEW); |
|
149 |
glLoadIdentity(); |
|
150 |
glTranslated(0.0, 0.0, -40.0); |
|
151 |
} |
|
152 |
||
153 |
void GLWidget::mousePressEvent(QMouseEvent *event) |
|
154 |
{ |
|
155 |
lastPos = event->pos(); |
|
156 |
} |
|
157 |
||
158 |
void GLWidget::mouseMoveEvent(QMouseEvent *event) |
|
159 |
{ |
|
160 |
int dx = event->x() - lastPos.x(); |
|
161 |
int dy = event->y() - lastPos.y(); |
|
162 |
||
163 |
if (event->buttons() & Qt::LeftButton) { |
|
164 |
setXRotation(xRot + 8 * dy); |
|
165 |
setYRotation(yRot + 8 * dx); |
|
166 |
} else if (event->buttons() & Qt::RightButton) { |
|
167 |
setXRotation(xRot + 8 * dy); |
|
168 |
setZRotation(zRot + 8 * dx); |
|
169 |
} |
|
170 |
lastPos = event->pos(); |
|
171 |
} |
|
172 |
||
173 |
void GLWidget::advanceGears() |
|
174 |
{ |
|
175 |
gear1Rot += 2 * 16; |
|
176 |
updateGL(); |
|
177 |
} |
|
178 |
||
179 |
GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius, |
|
180 |
GLdouble outerRadius, GLdouble thickness, |
|
181 |
GLdouble toothSize, GLint toothCount) |
|
182 |
{ |
|
183 |
const double Pi = 3.14159265358979323846; |
|
184 |
||
185 |
GLuint list = glGenLists(1); |
|
186 |
glNewList(list, GL_COMPILE); |
|
187 |
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, reflectance); |
|
188 |
||
189 |
GLdouble r0 = innerRadius; |
|
190 |
GLdouble r1 = outerRadius - toothSize / 2.0; |
|
191 |
GLdouble r2 = outerRadius + toothSize / 2.0; |
|
192 |
GLdouble delta = (2.0 * Pi / toothCount) / 4.0; |
|
193 |
GLdouble z = thickness / 2.0; |
|
194 |
int i, j; |
|
195 |
||
196 |
glShadeModel(GL_FLAT); |
|
197 |
||
198 |
for (i = 0; i < 2; ++i) { |
|
199 |
GLdouble sign = (i == 0) ? +1.0 : -1.0; |
|
200 |
||
201 |
glNormal3d(0.0, 0.0, sign); |
|
202 |
||
203 |
glBegin(GL_QUAD_STRIP); |
|
204 |
for (j = 0; j <= toothCount; ++j) { |
|
205 |
GLdouble angle = 2.0 * Pi * j / toothCount; |
|
206 |
glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z); |
|
207 |
glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z); |
|
208 |
glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z); |
|
209 |
glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta), |
|
210 |
sign * z); |
|
211 |
} |
|
212 |
glEnd(); |
|
213 |
||
214 |
glBegin(GL_QUADS); |
|
215 |
for (j = 0; j < toothCount; ++j) { |
|
216 |
GLdouble angle = 2.0 * Pi * j / toothCount; |
|
217 |
glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z); |
|
218 |
glVertex3d(r2 * cos(angle + delta), r2 * sin(angle + delta), |
|
219 |
sign * z); |
|
220 |
glVertex3d(r2 * cos(angle + 2 * delta), r2 * sin(angle + 2 * delta), |
|
221 |
sign * z); |
|
222 |
glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta), |
|
223 |
sign * z); |
|
224 |
} |
|
225 |
glEnd(); |
|
226 |
} |
|
227 |
||
228 |
glBegin(GL_QUAD_STRIP); |
|
229 |
for (i = 0; i < toothCount; ++i) { |
|
230 |
for (j = 0; j < 2; ++j) { |
|
231 |
GLdouble angle = 2.0 * Pi * (i + (j / 2.0)) / toothCount; |
|
232 |
GLdouble s1 = r1; |
|
233 |
GLdouble s2 = r2; |
|
234 |
if (j == 1) |
|
235 |
qSwap(s1, s2); |
|
236 |
||
237 |
glNormal3d(cos(angle), sin(angle), 0.0); |
|
238 |
glVertex3d(s1 * cos(angle), s1 * sin(angle), +z); |
|
239 |
glVertex3d(s1 * cos(angle), s1 * sin(angle), -z); |
|
240 |
||
241 |
glNormal3d(s2 * sin(angle + delta) - s1 * sin(angle), |
|
242 |
s1 * cos(angle) - s2 * cos(angle + delta), 0.0); |
|
243 |
glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), +z); |
|
244 |
glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), -z); |
|
245 |
} |
|
246 |
} |
|
247 |
glVertex3d(r1, 0.0, +z); |
|
248 |
glVertex3d(r1, 0.0, -z); |
|
249 |
glEnd(); |
|
250 |
||
251 |
glShadeModel(GL_SMOOTH); |
|
252 |
||
253 |
glBegin(GL_QUAD_STRIP); |
|
254 |
for (i = 0; i <= toothCount; ++i) { |
|
255 |
GLdouble angle = i * 2.0 * Pi / toothCount; |
|
256 |
glNormal3d(-cos(angle), -sin(angle), 0.0); |
|
257 |
glVertex3d(r0 * cos(angle), r0 * sin(angle), +z); |
|
258 |
glVertex3d(r0 * cos(angle), r0 * sin(angle), -z); |
|
259 |
} |
|
260 |
glEnd(); |
|
261 |
||
262 |
glEndList(); |
|
263 |
||
264 |
return list; |
|
265 |
} |
|
266 |
||
267 |
void GLWidget::drawGear(GLuint gear, GLdouble dx, GLdouble dy, GLdouble dz, |
|
268 |
GLdouble angle) |
|
269 |
{ |
|
270 |
glPushMatrix(); |
|
271 |
glTranslated(dx, dy, dz); |
|
272 |
glRotated(angle, 0.0, 0.0, 1.0); |
|
273 |
glCallList(gear); |
|
274 |
glPopMatrix(); |
|
275 |
} |
|
276 |
||
277 |
void GLWidget::normalizeAngle(int *angle) |
|
278 |
{ |
|
279 |
while (*angle < 0) |
|
280 |
*angle += 360 * 16; |
|
281 |
while (*angle > 360 * 16) |
|
282 |
*angle -= 360 * 16; |
|
283 |
} |