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

« back to all changes in this revision

Viewing changes to doc/html/opengl-hellogl-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/hellogl/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/hellogl/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
        object = 0;
 
59
        xRot = 0;
 
60
        yRot = 0;
 
61
        zRot = 0;
 
62
 
 
63
        trolltechGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
 
64
        trolltechPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
 
65
    }
 
66
 
 
67
    GLWidget::~GLWidget()
 
68
    {
 
69
        makeCurrent();
 
70
        glDeleteLists(object, 1);
 
71
    }
 
72
 
 
73
    QSize GLWidget::minimumSizeHint() const
 
74
    {
 
75
        return QSize(50, 50);
 
76
    }
 
77
 
 
78
    QSize GLWidget::sizeHint() const
 
79
    {
 
80
        return QSize(400, 400);
 
81
    }
 
82
 
 
83
    void GLWidget::setXRotation(int angle)
 
84
    {
 
85
        normalizeAngle(&amp;angle);
 
86
        if (angle != xRot) {
 
87
            xRot = angle;
 
88
            emit xRotationChanged(angle);
 
89
            updateGL();
 
90
        }
 
91
    }
 
92
 
 
93
    void GLWidget::setYRotation(int angle)
 
94
    {
 
95
        normalizeAngle(&amp;angle);
 
96
        if (angle != yRot) {
 
97
            yRot = angle;
 
98
            emit yRotationChanged(angle);
 
99
            updateGL();
 
100
        }
 
101
    }
 
102
 
 
103
    void GLWidget::setZRotation(int angle)
 
104
    {
 
105
        normalizeAngle(&amp;angle);
 
106
        if (angle != zRot) {
 
107
            zRot = angle;
 
108
            emit zRotationChanged(angle);
 
109
            updateGL();
 
110
        }
 
111
    }
 
112
 
 
113
    void GLWidget::initializeGL()
 
114
    {
 
115
        qglClearColor(trolltechPurple.dark());
 
116
        object = makeObject();
 
117
        glShadeModel(GL_FLAT);
 
118
        glEnable(GL_DEPTH_TEST);
 
119
        glEnable(GL_CULL_FACE);
 
120
    }
 
121
 
 
122
    void GLWidget::paintGL()
 
123
    {
 
124
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
125
        glLoadIdentity();
 
126
        glTranslated(0.0, 0.0, -10.0);
 
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
        glCallList(object);
 
131
    }
 
132
 
 
133
    void GLWidget::resizeGL(int width, int height)
 
134
    {
 
135
        int side = qMin(width, height);
 
136
        glViewport((width - side) / 2, (height - side) / 2, side, side);
 
137
 
 
138
        glMatrixMode(GL_PROJECTION);
 
139
        glLoadIdentity();
 
140
        glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
 
141
        glMatrixMode(GL_MODELVIEW);
 
142
    }
 
143
 
 
144
    void GLWidget::mousePressEvent(QMouseEvent *event)
 
145
    {
 
146
        lastPos = event-&gt;pos();
 
147
    }
 
148
 
 
149
    void GLWidget::mouseMoveEvent(QMouseEvent *event)
 
150
    {
 
151
        int dx = event-&gt;x() - lastPos.x();
 
152
        int dy = event-&gt;y() - lastPos.y();
 
153
 
 
154
        if (event-&gt;buttons() &amp; Qt::LeftButton) {
 
155
            setXRotation(xRot + 8 * dy);
 
156
            setYRotation(yRot + 8 * dx);
 
157
        } else if (event-&gt;buttons() &amp; Qt::RightButton) {
 
158
            setXRotation(xRot + 8 * dy);
 
159
            setZRotation(zRot + 8 * dx);
 
160
        }
 
161
        lastPos = event-&gt;pos();
 
162
    }
 
163
 
 
164
    GLuint GLWidget::makeObject()
 
165
    {
 
166
        GLuint list = glGenLists(1);
 
167
        glNewList(list, GL_COMPILE);
 
168
 
 
169
        glBegin(GL_QUADS);
 
170
 
 
171
        GLdouble x1 = +0.06;
 
172
        GLdouble y1 = -0.14;
 
173
        GLdouble x2 = +0.14;
 
174
        GLdouble y2 = -0.06;
 
175
        GLdouble x3 = +0.08;
 
176
        GLdouble y3 = +0.00;
 
177
        GLdouble x4 = +0.30;
 
178
        GLdouble y4 = +0.22;
 
179
 
 
180
        quad(x1, y1, x2, y2, y2, x2, y1, x1);
 
181
        quad(x3, y3, x4, y4, y4, x4, y3, x3);
 
182
 
 
183
        extrude(x1, y1, x2, y2);
 
184
        extrude(x2, y2, y2, x2);
 
185
        extrude(y2, x2, y1, x1);
 
186
        extrude(y1, x1, x1, y1);
 
187
        extrude(x3, y3, x4, y4);
 
188
        extrude(x4, y4, y4, x4);
 
189
        extrude(y4, x4, y3, x3);
 
190
 
 
191
        const double Pi = 3.14159265358979323846;
 
192
        const int NumSectors = 200;
 
193
 
 
194
        for (int i = 0; i &lt; NumSectors; ++i) {
 
195
            double angle1 = (i * 2 * Pi) / NumSectors;
 
196
            GLdouble x5 = 0.30 * sin(angle1);
 
197
            GLdouble y5 = 0.30 * cos(angle1);
 
198
            GLdouble x6 = 0.20 * sin(angle1);
 
199
            GLdouble y6 = 0.20 * cos(angle1);
 
200
 
 
201
            double angle2 = ((i + 1) * 2 * Pi) / NumSectors;
 
202
            GLdouble x7 = 0.20 * sin(angle2);
 
203
            GLdouble y7 = 0.20 * cos(angle2);
 
204
            GLdouble x8 = 0.30 * sin(angle2);
 
205
            GLdouble y8 = 0.30 * cos(angle2);
 
206
 
 
207
            quad(x5, y5, x6, y6, x7, y7, x8, y8);
 
208
 
 
209
            extrude(x6, y6, x7, y7);
 
210
            extrude(x8, y8, x5, y5);
 
211
        }
 
212
 
 
213
        glEnd();
 
214
 
 
215
        glEndList();
 
216
        return list;
 
217
    }
 
218
 
 
219
    void GLWidget::quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2,
 
220
                        GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4)
 
221
    {
 
222
        qglColor(trolltechGreen);
 
223
 
 
224
        glVertex3d(x1, y1, -0.05);
 
225
        glVertex3d(x2, y2, -0.05);
 
226
        glVertex3d(x3, y3, -0.05);
 
227
        glVertex3d(x4, y4, -0.05);
 
228
 
 
229
        glVertex3d(x4, y4, +0.05);
 
230
        glVertex3d(x3, y3, +0.05);
 
231
        glVertex3d(x2, y2, +0.05);
 
232
        glVertex3d(x1, y1, +0.05);
 
233
    }
 
234
 
 
235
    void GLWidget::extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2)
 
236
    {
 
237
        qglColor(trolltechGreen.dark(250 + int(100 * x1)));
 
238
 
 
239
        glVertex3d(x1, y1, +0.05);
 
240
        glVertex3d(x2, y2, +0.05);
 
241
        glVertex3d(x2, y2, -0.05);
 
242
        glVertex3d(x1, y1, -0.05);
 
243
    }
 
244
 
 
245
    void GLWidget::normalizeAngle(int *angle)
 
246
    {
 
247
        while (*angle &lt; 0)
 
248
            *angle += 360 * 16;
 
249
        while (*angle &gt; 360 * 16)
 
250
            *angle -= 360 * 16;
 
251
    }</pre>
 
252
<p /><address><hr /><div align="center">
 
253
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
254
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
255
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
256
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
257
</tr></table></div></address></body>
 
258
</html>