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/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>
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/hellogl/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)
63
trolltechGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
64
trolltechPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
70
glDeleteLists(object, 1);
73
QSize GLWidget::minimumSizeHint() const
78
QSize GLWidget::sizeHint() const
80
return QSize(400, 400);
83
void GLWidget::setXRotation(int angle)
85
normalizeAngle(&angle);
88
emit xRotationChanged(angle);
93
void GLWidget::setYRotation(int angle)
95
normalizeAngle(&angle);
98
emit yRotationChanged(angle);
103
void GLWidget::setZRotation(int angle)
105
normalizeAngle(&angle);
108
emit zRotationChanged(angle);
113
void GLWidget::initializeGL()
115
qglClearColor(trolltechPurple.dark());
116
object = makeObject();
117
glShadeModel(GL_FLAT);
118
glEnable(GL_DEPTH_TEST);
119
glEnable(GL_CULL_FACE);
122
void GLWidget::paintGL()
124
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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);
133
void GLWidget::resizeGL(int width, int height)
135
int side = qMin(width, height);
136
glViewport((width - side) / 2, (height - side) / 2, side, side);
138
glMatrixMode(GL_PROJECTION);
140
glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
141
glMatrixMode(GL_MODELVIEW);
144
void GLWidget::mousePressEvent(QMouseEvent *event)
146
lastPos = event->pos();
149
void GLWidget::mouseMoveEvent(QMouseEvent *event)
151
int dx = event->x() - lastPos.x();
152
int dy = event->y() - lastPos.y();
154
if (event->buttons() & Qt::LeftButton) {
155
setXRotation(xRot + 8 * dy);
156
setYRotation(yRot + 8 * dx);
157
} else if (event->buttons() & Qt::RightButton) {
158
setXRotation(xRot + 8 * dy);
159
setZRotation(zRot + 8 * dx);
161
lastPos = event->pos();
164
GLuint GLWidget::makeObject()
166
GLuint list = glGenLists(1);
167
glNewList(list, GL_COMPILE);
180
quad(x1, y1, x2, y2, y2, x2, y1, x1);
181
quad(x3, y3, x4, y4, y4, x4, y3, x3);
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);
191
const double Pi = 3.14159265358979323846;
192
const int NumSectors = 200;
194
for (int i = 0; i < 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);
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);
207
quad(x5, y5, x6, y6, x7, y7, x8, y8);
209
extrude(x6, y6, x7, y7);
210
extrude(x8, y8, x5, y5);
219
void GLWidget::quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2,
220
GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4)
222
qglColor(trolltechGreen);
224
glVertex3d(x1, y1, -0.05);
225
glVertex3d(x2, y2, -0.05);
226
glVertex3d(x3, y3, -0.05);
227
glVertex3d(x4, y4, -0.05);
229
glVertex3d(x4, y4, +0.05);
230
glVertex3d(x3, y3, +0.05);
231
glVertex3d(x2, y2, +0.05);
232
glVertex3d(x1, y1, +0.05);
235
void GLWidget::extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2)
237
qglColor(trolltechGreen.dark(250 + int(100 * x1)));
239
glVertex3d(x1, y1, +0.05);
240
glVertex3d(x2, y2, +0.05);
241
glVertex3d(x2, y2, -0.05);
242
glVertex3d(x1, y1, -0.05);
245
void GLWidget::normalizeAngle(int *angle)
247
while (*angle < 0)
249
while (*angle > 360 * 16)
252
<p /><address><hr /><div align="center">
253
<table width="100%" cellspacing="0" border="0"><tr class="address">
254
<td width="30%">Copyright © 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>