1
|
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
|
207
|
38 version(Tango)
|
|
39 import tango.math.Math;
|
|
40 else
|
|
41 {
|
|
42 import std.math;
|
|
43 int rndint(real x)
|
|
44 {
|
|
45 return cast(int)rndtol(x);
|
|
46 }
|
|
47 }
|
1
|
48
|
|
49 import qt.core.QPoint;
|
|
50 import qt.gui.QMouseEvent;
|
|
51 import qt.opengl.QGLWidget;
|
|
52 import qt.gui.QColor;
|
|
53 import qt.core.QSize;
|
|
54
|
|
55 import qt.opengl.gl;
|
|
56 import qt.opengl.glu;
|
|
57
|
|
58 class GLWidget : QGLWidget
|
|
59 {
|
|
60 // Q_OBJECT
|
5
|
61
|
1
|
62 public:
|
|
63 this(QWidget parent = null)
|
|
64 {
|
|
65 super(parent);
|
|
66 object = 0;
|
|
67 xRot = 0;
|
|
68 yRot = 0;
|
|
69 zRot = 0;
|
5
|
70
|
1
|
71 trolltechGreen = QColor.fromCmykF(0.40, 0.0, 1.0, 0.0);
|
|
72 trolltechPurple = QColor.fromCmykF(0.39, 0.39, 0.0, 0.0);
|
|
73 }
|
5
|
74
|
1
|
75 ~this()
|
|
76 {
|
|
77 makeCurrent();
|
|
78 glDeleteLists(object, 1);
|
5
|
79 }
|
|
80
|
1
|
81 QSize minimumSizeHint()
|
|
82 {
|
|
83 return QSize(50, 50);
|
|
84 }
|
|
85
|
|
86 QSize sizeHint()
|
|
87 {
|
|
88 return QSize(400, 400);
|
|
89 }
|
|
90
|
|
91
|
|
92 public: // slots:
|
|
93 void setXRotation(int angle)
|
|
94 {
|
|
95 normalizeAngle(&angle);
|
|
96 if (angle != xRot) {
|
|
97 xRot = angle;
|
|
98 xRotationChanged.emit(angle);
|
|
99 updateGL();
|
|
100 }
|
|
101 }
|
5
|
102
|
1
|
103 void setYRotation(int angle)
|
|
104 {
|
|
105 normalizeAngle(&angle);
|
|
106 if (angle != yRot) {
|
|
107 yRot = angle;
|
|
108 yRotationChanged.emit(angle);
|
|
109 updateGL();
|
|
110 }
|
|
111 }
|
5
|
112
|
1
|
113 void setZRotation(int angle)
|
|
114 {
|
|
115 normalizeAngle(&angle);
|
|
116 if (angle != zRot) {
|
|
117 zRot = angle;
|
|
118 zRotationChanged.emit(angle);
|
|
119 updateGL();
|
|
120 }
|
|
121 }
|
5
|
122
|
1
|
123 mixin Signal!("xRotationChanged", int);
|
|
124 mixin Signal!("yRotationChanged", int);
|
|
125 mixin Signal!("zRotationChanged", int);
|
5
|
126
|
1
|
127
|
|
128 protected:
|
|
129 void initializeGL()
|
|
130 {
|
|
131 qglClearColor(trolltechPurple.darker());
|
|
132 object = makeObject();
|
|
133 glShadeModel(GL_FLAT);
|
|
134 glEnable(GL_DEPTH_TEST);
|
|
135 glEnable(GL_CULL_FACE);
|
|
136 }
|
|
137
|
|
138 void paintGL()
|
|
139 {
|
|
140 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
141 glLoadIdentity();
|
|
142 glTranslated(0.0, 0.0, -10.0);
|
|
143 glRotated(xRot / 16.0, 1.0, 0.0, 0.0);
|
|
144 glRotated(yRot / 16.0, 0.0, 1.0, 0.0);
|
|
145 glRotated(zRot / 16.0, 0.0, 0.0, 1.0);
|
|
146 glCallList(object);
|
|
147 }
|
|
148
|
|
149 void resizeGL(int width, int height)
|
|
150 {
|
|
151 int side = qMin(width, height);
|
|
152 glViewport((width - side) / 2, (height - side) / 2, side, side);
|
5
|
153
|
1
|
154 glMatrixMode(GL_PROJECTION);
|
|
155 glLoadIdentity();
|
|
156 glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
|
|
157 glMatrixMode(GL_MODELVIEW);
|
|
158 }
|
|
159
|
|
160 void mousePressEvent(QMouseEvent event)
|
|
161 {
|
|
162 lastPos = QPoint(event.pos.x, event.pos.y);
|
|
163 }
|
5
|
164
|
1
|
165 void mouseMoveEvent(QMouseEvent event)
|
|
166 {
|
|
167 int dx = event.x - lastPos.x;
|
|
168 int dy = event.y - lastPos.y;
|
5
|
169
|
1
|
170 if (event.buttons() & Qt.LeftButton) {
|
|
171 setXRotation(xRot + 8 * dy);
|
|
172 setYRotation(yRot + 8 * dx);
|
|
173 } else if (event.buttons() & Qt.RightButton) {
|
|
174 setXRotation(xRot + 8 * dy);
|
|
175 setZRotation(zRot + 8 * dx);
|
|
176 }
|
|
177 lastPos = QPoint(event.pos.x, event.pos.y);
|
|
178 }
|
|
179 private:
|
|
180 GLuint makeObject()
|
|
181 {
|
|
182 GLuint list = glGenLists(1);
|
|
183 glNewList(list, GL_COMPILE);
|
5
|
184
|
1
|
185 glBegin(GL_QUADS);
|
5
|
186
|
1
|
187 GLdouble x1 = +0.06;
|
|
188 GLdouble y1 = -0.14;
|
|
189 GLdouble x2 = +0.14;
|
|
190 GLdouble y2 = -0.06;
|
|
191 GLdouble x3 = +0.08;
|
|
192 GLdouble y3 = +0.00;
|
|
193 GLdouble x4 = +0.30;
|
|
194 GLdouble y4 = +0.22;
|
5
|
195
|
1
|
196 quad(x1, y1, x2, y2, y2, x2, y1, x1);
|
|
197 quad(x3, y3, x4, y4, y4, x4, y3, x3);
|
5
|
198
|
1
|
199 extrude(x1, y1, x2, y2);
|
|
200 extrude(x2, y2, y2, x2);
|
|
201 extrude(y2, x2, y1, x1);
|
|
202 extrude(y1, x1, x1, y1);
|
|
203 extrude(x3, y3, x4, y4);
|
|
204 extrude(x4, y4, y4, x4);
|
|
205 extrude(y4, x4, y3, x3);
|
5
|
206
|
1
|
207 const double Pi = 3.14159265358979323846;
|
|
208 const int NumSectors = 200;
|
5
|
209
|
1
|
210 for (int i = 0; i < NumSectors; ++i) {
|
|
211 double angle1 = (i * 2 * Pi) / NumSectors;
|
|
212 GLdouble x5 = 0.30 * sin(angle1);
|
|
213 GLdouble y5 = 0.30 * cos(angle1);
|
|
214 GLdouble x6 = 0.20 * sin(angle1);
|
|
215 GLdouble y6 = 0.20 * cos(angle1);
|
5
|
216
|
1
|
217 double angle2 = ((i + 1) * 2 * Pi) / NumSectors;
|
|
218 GLdouble x7 = 0.20 * sin(angle2);
|
|
219 GLdouble y7 = 0.20 * cos(angle2);
|
|
220 GLdouble x8 = 0.30 * sin(angle2);
|
|
221 GLdouble y8 = 0.30 * cos(angle2);
|
5
|
222
|
1
|
223 quad(x5, y5, x6, y6, x7, y7, x8, y8);
|
5
|
224
|
1
|
225 extrude(x6, y6, x7, y7);
|
|
226 extrude(x8, y8, x5, y5);
|
|
227 }
|
5
|
228
|
1
|
229 glEnd();
|
|
230
|
|
231 glEndList();
|
|
232 return list;
|
|
233 }
|
5
|
234
|
1
|
235 void quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2,
|
|
236 GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4)
|
|
237 {
|
|
238 qglColor(trolltechGreen);
|
5
|
239
|
1
|
240 glVertex3d(x1, y1, -0.05);
|
|
241 glVertex3d(x2, y2, -0.05);
|
|
242 glVertex3d(x3, y3, -0.05);
|
|
243 glVertex3d(x4, y4, -0.05);
|
5
|
244
|
1
|
245 glVertex3d(x4, y4, +0.05);
|
|
246 glVertex3d(x3, y3, +0.05);
|
|
247 glVertex3d(x2, y2, +0.05);
|
|
248 glVertex3d(x1, y1, +0.05);
|
|
249 }
|
5
|
250
|
1
|
251 void extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2)
|
|
252 {
|
|
253 qglColor(trolltechGreen.darker(rndint(250 + (100 * x1))));
|
5
|
254
|
1
|
255 glVertex3d(x1, y1, +0.05);
|
|
256 glVertex3d(x2, y2, +0.05);
|
|
257 glVertex3d(x2, y2, -0.05);
|
|
258 glVertex3d(x1, y1, -0.05);
|
|
259 }
|
5
|
260
|
1
|
261 void normalizeAngle(int *angle)
|
|
262 {
|
|
263 while (*angle < 0)
|
|
264 *angle += 360 * 16;
|
|
265 while (*angle > 360 * 16)
|
|
266 *angle -= 360 * 16;
|
|
267 }
|
5
|
268
|
1
|
269 GLuint object;
|
|
270 int xRot;
|
|
271 int yRot;
|
|
272 int zRot;
|
|
273 QPoint lastPos;
|
|
274 QColor trolltechGreen;
|
|
275 QColor trolltechPurple;
|
|
276 } |