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