1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.0: glwidget.cpp Example File (opengl/grabber/glwidget.cpp)</title>
7
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
8
a:link { color: #004faf; text-decoration: none }
9
a:visited { color: #672967; text-decoration: none }
10
td.postheader { font-family: sans-serif }
11
tr.address { font-family: sans-serif }
12
body { background: #ffffff; color: black; }</style>
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
18
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">glwidget.cpp Example File<br /><small><small>opengl/grabber/glwidget.cpp</small></small></h1>
20
<pre> /****************************************************************************
22
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
24
** This file is part of the documentation of the Qt Toolkit.
26
** This file may be distributed under the terms of the Q Public License
27
** as defined by Trolltech AS of Norway and appearing in the file
28
** LICENSE.QPL included in the packaging of this file.
30
** This file may be distributed and/or modified under the terms of the
31
** GNU General Public License version 2 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file.
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
36
** information about Qt Commercial License Agreements.
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
40
** Contact info@trolltech.com if any conditions of this licensing are
43
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
44
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46
****************************************************************************/
48
#include <QtGui>
49
#include <QtOpenGL>
51
#include <math.h>
53
#include "glwidget.h"
55
GLWidget::GLWidget(QWidget *parent)
66
QTimer *timer = new QTimer(this);
67
connect(timer, SIGNAL(timeout()), this, SLOT(advanceGears()));
74
glDeleteLists(gear1, 1);
75
glDeleteLists(gear2, 1);
76
glDeleteLists(gear3, 1);
79
void GLWidget::setXRotation(int angle)
81
normalizeAngle(&angle);
84
emit xRotationChanged(angle);
89
void GLWidget::setYRotation(int angle)
91
normalizeAngle(&angle);
94
emit yRotationChanged(angle);
99
void GLWidget::setZRotation(int angle)
101
normalizeAngle(&angle);
104
emit zRotationChanged(angle);
109
void GLWidget::initializeGL()
111
static const GLfloat lightPos[4] = { 5.0f, 5.0f, 10.0f, 1.0f };
112
static const GLfloat reflectance1[4] = { 0.8f, 0.1f, 0.0f, 1.0f };
113
static const GLfloat reflectance2[4] = { 0.0f, 0.8f, 0.2f, 1.0f };
114
static const GLfloat reflectance3[4] = { 0.2f, 0.2f, 1.0f, 1.0f };
116
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
117
glEnable(GL_LIGHTING);
119
glEnable(GL_DEPTH_TEST);
121
gear1 = makeGear(reflectance1, 1.0, 4.0, 1.0, 0.7, 20);
122
gear2 = makeGear(reflectance2, 0.5, 2.0, 2.0, 0.7, 10);
123
gear3 = makeGear(reflectance3, 1.3, 2.0, 0.5, 0.7, 10);
125
glEnable(GL_NORMALIZE);
128
void GLWidget::paintGL()
130
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
133
glRotated(xRot / 16.0, 1.0, 0.0, 0.0);
134
glRotated(yRot / 16.0, 0.0, 1.0, 0.0);
135
glRotated(zRot / 16.0, 0.0, 0.0, 1.0);
137
drawGear(gear1, -3.0, -2.0, 0.0, gear1Rot / 16.0);
138
drawGear(gear2, +3.1, -2.0, 0.0, -2.0 * (gear1Rot / 16.0) - 9.0);
140
glRotated(+90.0, 1.0, 0.0, 0.0);
141
drawGear(gear3, -3.1, -1.8, -2.2, +2.0 * (gear1Rot / 16.0) - 2.0);
146
void GLWidget::resizeGL(int width, int height)
148
int side = qMin(width, height);
149
glViewport((width - side) / 2, (height - side) / 2, side, side);
151
glMatrixMode(GL_PROJECTION);
153
glFrustum(-1.0, +1.0, -1.0, 1.0, 5.0, 60.0);
154
glMatrixMode(GL_MODELVIEW);
156
glTranslated(0.0, 0.0, -40.0);
159
void GLWidget::mousePressEvent(QMouseEvent *event)
161
lastPos = event->pos();
164
void GLWidget::mouseMoveEvent(QMouseEvent *event)
166
int dx = event->x() - lastPos.x();
167
int dy = event->y() - lastPos.y();
169
if (event->buttons() & Qt::LeftButton) {
170
setXRotation(xRot + 8 * dy);
171
setYRotation(yRot + 8 * dx);
172
} else if (event->buttons() & Qt::RightButton) {
173
setXRotation(xRot + 8 * dy);
174
setZRotation(zRot + 8 * dx);
176
lastPos = event->pos();
179
void GLWidget::advanceGears()
185
GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius,
186
GLdouble outerRadius, GLdouble thickness,
187
GLdouble toothSize, GLint toothCount)
189
const double Pi = 3.14159265358979323846;
191
GLuint list = glGenLists(1);
192
glNewList(list, GL_COMPILE);
193
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, reflectance);
195
GLdouble r0 = innerRadius;
196
GLdouble r1 = outerRadius - toothSize / 2.0;
197
GLdouble r2 = outerRadius + toothSize / 2.0;
198
GLdouble delta = (2.0 * Pi / toothCount) / 4.0;
199
GLdouble z = thickness / 2.0;
202
glShadeModel(GL_FLAT);
204
for (i = 0; i < 2; ++i) {
205
GLdouble sign = (i == 0) ? +1.0 : -1.0;
207
glNormal3d(0.0, 0.0, sign);
209
glBegin(GL_QUAD_STRIP);
210
for (j = 0; j <= toothCount; ++j) {
211
GLdouble angle = 2.0 * Pi * j / toothCount;
212
glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z);
213
glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z);
214
glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z);
215
glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta),
221
for (j = 0; j < toothCount; ++j) {
222
GLdouble angle = 2.0 * Pi * j / toothCount;
223
glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z);
224
glVertex3d(r2 * cos(angle + delta), r2 * sin(angle + delta),
226
glVertex3d(r2 * cos(angle + 2 * delta), r2 * sin(angle + 2 * delta),
228
glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta),
234
glBegin(GL_QUAD_STRIP);
235
for (i = 0; i < toothCount; ++i) {
236
for (j = 0; j < 2; ++j) {
237
GLdouble angle = 2.0 * Pi * (i + (j / 2.0)) / toothCount;
243
glNormal3d(cos(angle), sin(angle), 0.0);
244
glVertex3d(s1 * cos(angle), s1 * sin(angle), +z);
245
glVertex3d(s1 * cos(angle), s1 * sin(angle), -z);
247
glNormal3d(s2 * sin(angle + delta) - s1 * sin(angle),
248
s1 * cos(angle) - s2 * cos(angle + delta), 0.0);
249
glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), +z);
250
glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), -z);
253
glVertex3d(r1, 0.0, +z);
254
glVertex3d(r1, 0.0, -z);
257
glShadeModel(GL_SMOOTH);
259
glBegin(GL_QUAD_STRIP);
260
for (i = 0; i <= toothCount; ++i) {
261
GLdouble angle = i * 2.0 * Pi / toothCount;
262
glNormal3d(-cos(angle), -sin(angle), 0.0);
263
glVertex3d(r0 * cos(angle), r0 * sin(angle), +z);
264
glVertex3d(r0 * cos(angle), r0 * sin(angle), -z);
273
void GLWidget::drawGear(GLuint gear, GLdouble dx, GLdouble dy, GLdouble dz,
277
glTranslated(dx, dy, dz);
278
glRotated(angle, 0.0, 0.0, 1.0);
283
void GLWidget::normalizeAngle(int *angle)
285
while (*angle < 0)
287
while (*angle > 360 * 16)
290
<p /><address><hr /><div align="center">
291
<table width="100%" cellspacing="0" border="0"><tr class="address">
292
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
293
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
294
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
295
</tr></table></div></address></body>