source: trunk/examples/opengl/grabber/glwidget.cpp@ 983

Last change on this file since 983 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 8.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42#include <QtOpenGL>
43
44#include <math.h>
45
46#include "glwidget.h"
47
48GLWidget::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
64GLWidget::~GLWidget()
65{
66 makeCurrent();
67 glDeleteLists(gear1, 1);
68 glDeleteLists(gear2, 1);
69 glDeleteLists(gear3, 1);
70}
71
72void GLWidget::setXRotation(int angle)
73{
74 normalizeAngle(&angle);
75 if (angle != xRot) {
76 xRot = angle;
77 emit xRotationChanged(angle);
78 updateGL();
79 }
80}
81
82void GLWidget::setYRotation(int angle)
83{
84 normalizeAngle(&angle);
85 if (angle != yRot) {
86 yRot = angle;
87 emit yRotationChanged(angle);
88 updateGL();
89 }
90}
91
92void GLWidget::setZRotation(int angle)
93{
94 normalizeAngle(&angle);
95 if (angle != zRot) {
96 zRot = angle;
97 emit zRotationChanged(angle);
98 updateGL();
99 }
100}
101
102void 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
122void 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
140void 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
153void GLWidget::mousePressEvent(QMouseEvent *event)
154{
155 lastPos = event->pos();
156}
157
158void 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
173void GLWidget::advanceGears()
174{
175 gear1Rot += 2 * 16;
176 updateGL();
177}
178
179GLuint 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
267void 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
277void GLWidget::normalizeAngle(int *angle)
278{
279 while (*angle < 0)
280 *angle += 360 * 16;
281 while (*angle > 360 * 16)
282 *angle -= 360 * 16;
283}
Note: See TracBrowser for help on using the repository browser.