comparison examples/opengl/hellogl/glwidget.d @ 1:e78566595089

initial import
author mandel
date Mon, 11 May 2009 16:01:50 +0000
parents
children 0a29ce1ae854
comparison
equal deleted inserted replaced
0:36fb74dc547d 1:e78566595089
1 /****************************************************************************
2 **
3 ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
5 **
6 ** This file is part of the example classes of the Qt Toolkit.
7 **
8 ** Commercial Usage
9 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** accordance with the Qt Commercial License Agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and Nokia.
13 **
14 **
15 ** GNU General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU
17 ** General Public License versions 2.0 or 3.0 as published by the Free
18 ** Software Foundation and appearing in the file LICENSE.GPL included in
19 ** the packaging of this file. Please review the following information
20 ** to ensure GNU General Public Licensing requirements will be met:
21 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
22 ** http://www.gnu.org/copyleft/gpl.html. In addition, as a special
23 ** exception, Nokia gives you certain additional rights. These rights
24 ** are described in the Nokia Qt GPL Exception version 1.3, included in
25 ** the file GPL_EXCEPTION.txt in this package.
26 **
27 ** Qt for Windows(R) Licensees
28 ** As a special exception, Nokia, as the sole copyright holder for Qt
29 ** Designer, grants users of the Qt/Eclipse Integration plug-in the
30 ** right for the Qt/Eclipse Integration to link to functionality
31 ** provided by Qt Designer and its related libraries.
32 **
33 ** If you are unsure which license is appropriate for your use, please
34 ** contact the sales department at qt-sales@nokia.com.
35 **
36 ****************************************************************************/
37
38
39 import tango.math.Math;
40
41 import qt.core.QPoint;
42 import qt.gui.QMouseEvent;
43 import qt.opengl.QGLWidget;
44 import qt.gui.QColor;
45 import qt.core.QSize;
46
47 import qt.opengl.gl;
48 import qt.opengl.glu;
49
50 class GLWidget : QGLWidget
51 {
52 // Q_OBJECT
53
54 public:
55 this(QWidget parent = null)
56 {
57 super(parent);
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 ~this()
68 {
69 makeCurrent();
70 glDeleteLists(object, 1);
71 }
72
73 QSize minimumSizeHint()
74 {
75 return QSize(50, 50);
76 }
77
78 QSize sizeHint()
79 {
80 return QSize(400, 400);
81 }
82
83
84 public: // slots:
85 void setXRotation(int angle)
86 {
87 normalizeAngle(&angle);
88 if (angle != xRot) {
89 xRot = angle;
90 xRotationChanged.emit(angle);
91 updateGL();
92 }
93 }
94
95 void setYRotation(int angle)
96 {
97 normalizeAngle(&angle);
98 if (angle != yRot) {
99 yRot = angle;
100 yRotationChanged.emit(angle);
101 updateGL();
102 }
103 }
104
105 void setZRotation(int angle)
106 {
107 normalizeAngle(&angle);
108 if (angle != zRot) {
109 zRot = angle;
110 zRotationChanged.emit(angle);
111 updateGL();
112 }
113 }
114
115 mixin Signal!("xRotationChanged", int);
116 mixin Signal!("yRotationChanged", int);
117 mixin Signal!("zRotationChanged", int);
118
119
120 protected:
121 void initializeGL()
122 {
123 qglClearColor(trolltechPurple.darker());
124 object = makeObject();
125 glShadeModel(GL_FLAT);
126 glEnable(GL_DEPTH_TEST);
127 glEnable(GL_CULL_FACE);
128 }
129
130 void paintGL()
131 {
132 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
133 glLoadIdentity();
134 glTranslated(0.0, 0.0, -10.0);
135 glRotated(xRot / 16.0, 1.0, 0.0, 0.0);
136 glRotated(yRot / 16.0, 0.0, 1.0, 0.0);
137 glRotated(zRot / 16.0, 0.0, 0.0, 1.0);
138 glCallList(object);
139 }
140
141 void resizeGL(int width, int height)
142 {
143 int side = qMin(width, height);
144 glViewport((width - side) / 2, (height - side) / 2, side, side);
145
146 glMatrixMode(GL_PROJECTION);
147 glLoadIdentity();
148 glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
149 glMatrixMode(GL_MODELVIEW);
150 }
151
152 void mousePressEvent(QMouseEvent event)
153 {
154 lastPos = QPoint(event.pos.x, event.pos.y);
155 }
156
157 void mouseMoveEvent(QMouseEvent event)
158 {
159 int dx = event.x - lastPos.x;
160 int dy = event.y - lastPos.y;
161
162 if (event.buttons() & Qt.LeftButton) {
163 setXRotation(xRot + 8 * dy);
164 setYRotation(yRot + 8 * dx);
165 } else if (event.buttons() & Qt.RightButton) {
166 setXRotation(xRot + 8 * dy);
167 setZRotation(zRot + 8 * dx);
168 }
169 lastPos = QPoint(event.pos.x, event.pos.y);
170 }
171 private:
172 GLuint makeObject()
173 {
174 GLuint list = glGenLists(1);
175 glNewList(list, GL_COMPILE);
176
177 glBegin(GL_QUADS);
178
179 GLdouble x1 = +0.06;
180 GLdouble y1 = -0.14;
181 GLdouble x2 = +0.14;
182 GLdouble y2 = -0.06;
183 GLdouble x3 = +0.08;
184 GLdouble y3 = +0.00;
185 GLdouble x4 = +0.30;
186 GLdouble y4 = +0.22;
187
188 quad(x1, y1, x2, y2, y2, x2, y1, x1);
189 quad(x3, y3, x4, y4, y4, x4, y3, x3);
190
191 extrude(x1, y1, x2, y2);
192 extrude(x2, y2, y2, x2);
193 extrude(y2, x2, y1, x1);
194 extrude(y1, x1, x1, y1);
195 extrude(x3, y3, x4, y4);
196 extrude(x4, y4, y4, x4);
197 extrude(y4, x4, y3, x3);
198
199 const double Pi = 3.14159265358979323846;
200 const int NumSectors = 200;
201
202 for (int i = 0; i < NumSectors; ++i) {
203 double angle1 = (i * 2 * Pi) / NumSectors;
204 GLdouble x5 = 0.30 * sin(angle1);
205 GLdouble y5 = 0.30 * cos(angle1);
206 GLdouble x6 = 0.20 * sin(angle1);
207 GLdouble y6 = 0.20 * cos(angle1);
208
209 double angle2 = ((i + 1) * 2 * Pi) / NumSectors;
210 GLdouble x7 = 0.20 * sin(angle2);
211 GLdouble y7 = 0.20 * cos(angle2);
212 GLdouble x8 = 0.30 * sin(angle2);
213 GLdouble y8 = 0.30 * cos(angle2);
214
215 quad(x5, y5, x6, y6, x7, y7, x8, y8);
216
217 extrude(x6, y6, x7, y7);
218 extrude(x8, y8, x5, y5);
219 }
220
221 glEnd();
222
223 glEndList();
224 return list;
225 }
226
227 void quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2,
228 GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4)
229 {
230 qglColor(trolltechGreen);
231
232 glVertex3d(x1, y1, -0.05);
233 glVertex3d(x2, y2, -0.05);
234 glVertex3d(x3, y3, -0.05);
235 glVertex3d(x4, y4, -0.05);
236
237 glVertex3d(x4, y4, +0.05);
238 glVertex3d(x3, y3, +0.05);
239 glVertex3d(x2, y2, +0.05);
240 glVertex3d(x1, y1, +0.05);
241 }
242
243 void extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2)
244 {
245 qglColor(trolltechGreen.darker(rndint(250 + (100 * x1))));
246
247 glVertex3d(x1, y1, +0.05);
248 glVertex3d(x2, y2, +0.05);
249 glVertex3d(x2, y2, -0.05);
250 glVertex3d(x1, y1, -0.05);
251 }
252
253 void normalizeAngle(int *angle)
254 {
255 while (*angle < 0)
256 *angle += 360 * 16;
257 while (*angle > 360 * 16)
258 *angle -= 360 * 16;
259 }
260
261 GLuint object;
262 int xRot;
263 int yRot;
264 int zRot;
265 QPoint lastPos;
266 QColor trolltechGreen;
267 QColor trolltechPurple;
268 }