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