~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/opengl-grabber-glwidget-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
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">
 
5
<head>
 
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>
 
13
</head>
 
14
<body>
 
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
16
<tr>
 
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">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <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>&nbsp;   /****************************************************************************
 
21
    **
 
22
    ** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
23
    **
 
24
    ** This file is part of the documentation of the Qt Toolkit.
 
25
    **
 
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.
 
29
**
 
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.
 
34
**
 
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.
 
39
**
 
40
** Contact info@trolltech.com if any conditions of this licensing are
 
41
** not clear to you.
 
42
    **
 
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.
 
45
    **
 
46
    ****************************************************************************/
 
47
 
 
48
    #include &lt;QtGui&gt;
 
49
    #include &lt;QtOpenGL&gt;
 
50
 
 
51
    #include &lt;math.h&gt;
 
52
 
 
53
    #include &quot;glwidget.h&quot;
 
54
 
 
55
    GLWidget::GLWidget(QWidget *parent)
 
56
        : QGLWidget(parent)
 
57
    {
 
58
        gear1 = 0;
 
59
        gear2 = 0;
 
60
        gear3 = 0;
 
61
        xRot = 0;
 
62
        yRot = 0;
 
63
        zRot = 0;
 
64
        gear1Rot = 0;
 
65
 
 
66
        QTimer *timer = new QTimer(this);
 
67
        connect(timer, SIGNAL(timeout()), this, SLOT(advanceGears()));
 
68
        timer-&gt;start(20);
 
69
    }
 
70
 
 
71
    GLWidget::~GLWidget()
 
72
    {
 
73
        makeCurrent();
 
74
        glDeleteLists(gear1, 1);
 
75
        glDeleteLists(gear2, 1);
 
76
        glDeleteLists(gear3, 1);
 
77
    }
 
78
 
 
79
    void GLWidget::setXRotation(int angle)
 
80
    {
 
81
        normalizeAngle(&amp;angle);
 
82
        if (angle != xRot) {
 
83
            xRot = angle;
 
84
            emit xRotationChanged(angle);
 
85
            updateGL();
 
86
        }
 
87
    }
 
88
 
 
89
    void GLWidget::setYRotation(int angle)
 
90
    {
 
91
        normalizeAngle(&amp;angle);
 
92
        if (angle != yRot) {
 
93
            yRot = angle;
 
94
            emit yRotationChanged(angle);
 
95
            updateGL();
 
96
        }
 
97
    }
 
98
 
 
99
    void GLWidget::setZRotation(int angle)
 
100
    {
 
101
        normalizeAngle(&amp;angle);
 
102
        if (angle != zRot) {
 
103
            zRot = angle;
 
104
            emit zRotationChanged(angle);
 
105
            updateGL();
 
106
        }
 
107
    }
 
108
 
 
109
    void GLWidget::initializeGL()
 
110
    {
 
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 };
 
115
 
 
116
        glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
 
117
        glEnable(GL_LIGHTING);
 
118
        glEnable(GL_LIGHT0);
 
119
        glEnable(GL_DEPTH_TEST);
 
120
 
 
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);
 
124
 
 
125
        glEnable(GL_NORMALIZE);
 
126
    }
 
127
 
 
128
    void GLWidget::paintGL()
 
129
    {
 
130
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
131
 
 
132
        glPushMatrix();
 
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);
 
136
 
 
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);
 
139
 
 
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);
 
142
 
 
143
        glPopMatrix();
 
144
    }
 
145
 
 
146
    void GLWidget::resizeGL(int width, int height)
 
147
    {
 
148
        int side = qMin(width, height);
 
149
        glViewport((width - side) / 2, (height - side) / 2, side, side);
 
150
 
 
151
        glMatrixMode(GL_PROJECTION);
 
152
        glLoadIdentity();
 
153
        glFrustum(-1.0, +1.0, -1.0, 1.0, 5.0, 60.0);
 
154
        glMatrixMode(GL_MODELVIEW);
 
155
        glLoadIdentity();
 
156
        glTranslated(0.0, 0.0, -40.0);
 
157
    }
 
158
 
 
159
    void GLWidget::mousePressEvent(QMouseEvent *event)
 
160
    {
 
161
        lastPos = event-&gt;pos();
 
162
    }
 
163
 
 
164
    void GLWidget::mouseMoveEvent(QMouseEvent *event)
 
165
    {
 
166
        int dx = event-&gt;x() - lastPos.x();
 
167
        int dy = event-&gt;y() - lastPos.y();
 
168
 
 
169
        if (event-&gt;buttons() &amp; Qt::LeftButton) {
 
170
            setXRotation(xRot + 8 * dy);
 
171
            setYRotation(yRot + 8 * dx);
 
172
        } else if (event-&gt;buttons() &amp; Qt::RightButton) {
 
173
            setXRotation(xRot + 8 * dy);
 
174
            setZRotation(zRot + 8 * dx);
 
175
        }
 
176
        lastPos = event-&gt;pos();
 
177
    }
 
178
 
 
179
    void GLWidget::advanceGears()
 
180
    {
 
181
        gear1Rot += 2 * 16;
 
182
        updateGL();
 
183
    }
 
184
 
 
185
    GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius,
 
186
                              GLdouble outerRadius, GLdouble thickness,
 
187
                              GLdouble toothSize, GLint toothCount)
 
188
    {
 
189
        const double Pi = 3.14159265358979323846;
 
190
 
 
191
        GLuint list = glGenLists(1);
 
192
        glNewList(list, GL_COMPILE);
 
193
        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, reflectance);
 
194
 
 
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;
 
200
        int i, j;
 
201
 
 
202
        glShadeModel(GL_FLAT);
 
203
 
 
204
        for (i = 0; i &lt; 2; ++i) {
 
205
            GLdouble sign = (i == 0) ? +1.0 : -1.0;
 
206
 
 
207
            glNormal3d(0.0, 0.0, sign);
 
208
 
 
209
            glBegin(GL_QUAD_STRIP);
 
210
            for (j = 0; j &lt;= 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),
 
216
                           sign * z);
 
217
            }
 
218
            glEnd();
 
219
 
 
220
            glBegin(GL_QUADS);
 
221
            for (j = 0; j &lt; 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),
 
225
                           sign * z);
 
226
                glVertex3d(r2 * cos(angle + 2 * delta), r2 * sin(angle + 2 * delta),
 
227
                           sign * z);
 
228
                glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta),
 
229
                           sign * z);
 
230
            }
 
231
            glEnd();
 
232
        }
 
233
 
 
234
        glBegin(GL_QUAD_STRIP);
 
235
        for (i = 0; i &lt; toothCount; ++i) {
 
236
            for (j = 0; j &lt; 2; ++j) {
 
237
                GLdouble angle = 2.0 * Pi * (i + (j / 2.0)) / toothCount;
 
238
                GLdouble s1 = r1;
 
239
                GLdouble s2 = r2;
 
240
                if (j == 1)
 
241
                    qSwap(s1, s2);
 
242
 
 
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);
 
246
 
 
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);
 
251
            }
 
252
        }
 
253
        glVertex3d(r1, 0.0, +z);
 
254
        glVertex3d(r1, 0.0, -z);
 
255
        glEnd();
 
256
 
 
257
        glShadeModel(GL_SMOOTH);
 
258
 
 
259
        glBegin(GL_QUAD_STRIP);
 
260
        for (i = 0; i &lt;= 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);
 
265
        }
 
266
        glEnd();
 
267
 
 
268
        glEndList();
 
269
 
 
270
        return list;
 
271
    }
 
272
 
 
273
    void GLWidget::drawGear(GLuint gear, GLdouble dx, GLdouble dy, GLdouble dz,
 
274
                            GLdouble angle)
 
275
    {
 
276
        glPushMatrix();
 
277
        glTranslated(dx, dy, dz);
 
278
        glRotated(angle, 0.0, 0.0, 1.0);
 
279
        glCallList(gear);
 
280
        glPopMatrix();
 
281
    }
 
282
 
 
283
    void GLWidget::normalizeAngle(int *angle)
 
284
    {
 
285
        while (*angle &lt; 0)
 
286
            *angle += 360 * 16;
 
287
        while (*angle &gt; 360 * 16)
 
288
            *angle -= 360 * 16;
 
289
    }</pre>
 
290
<p /><address><hr /><div align="center">
 
291
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
292
<td width="30%">Copyright &copy; 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>
 
296
</html>