comparison examples/opengl/hellogl/glwidget_d1.d @ 216:06e7d3219464

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