|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "cube.h" |
|
43 #include "glwidget.h" |
|
44 |
|
45 #include <QtGui/QImage> |
|
46 #include <QtCore/QPropertyAnimation> |
|
47 |
|
48 static const qreal FACE_SIZE = 0.4; |
|
49 |
|
50 static const qreal speeds[] = { 1.8f, 2.4f, 3.6f }; |
|
51 static const qreal amplitudes[] = { 2.0f, 2.5f, 3.0f }; |
|
52 |
|
53 static inline void qSetColor(float colorVec[], QColor c) |
|
54 { |
|
55 colorVec[0] = c.redF(); |
|
56 colorVec[1] = c.greenF(); |
|
57 colorVec[2] = c.blueF(); |
|
58 colorVec[3] = c.alphaF(); |
|
59 } |
|
60 |
|
61 int Geometry::append(const QVector3D &a, const QVector3D &n, const QVector2D &t) |
|
62 { |
|
63 int v = vertices.count(); |
|
64 vertices.append(a); |
|
65 normals.append(n); |
|
66 texCoords.append(t); |
|
67 faces.append(v); |
|
68 colors.append(QVector4D(0.6f, 0.6f, 0.6f, 1.0f)); |
|
69 return v; |
|
70 } |
|
71 |
|
72 void Geometry::addQuad(const QVector3D &a, const QVector3D &b, |
|
73 const QVector3D &c, const QVector3D &d, |
|
74 const QVector<QVector2D> &tex) |
|
75 { |
|
76 QVector3D norm = QVector3D::normal(a, b, c); |
|
77 // append first triangle |
|
78 int aref = append(a, norm, tex[0]); |
|
79 append(b, norm, tex[1]); |
|
80 int cref = append(c, norm, tex[2]); |
|
81 // append second triangle |
|
82 faces.append(aref); |
|
83 faces.append(cref); |
|
84 append(d, norm, tex[3]); |
|
85 } |
|
86 |
|
87 void Geometry::loadArrays() const |
|
88 { |
|
89 glEnableClientState(GL_VERTEX_ARRAY); |
|
90 glEnableClientState(GL_NORMAL_ARRAY); |
|
91 glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
|
92 glEnableClientState(GL_COLOR_ARRAY); |
|
93 glVertexPointer(3, GL_FLOAT, 0, vertices.constData()); |
|
94 glNormalPointer(GL_FLOAT, 0, normals.constData()); |
|
95 glTexCoordPointer(2, GL_FLOAT, 0, texCoords.constData()); |
|
96 glColorPointer(4, GL_FLOAT, 0, colors.constData()); |
|
97 } |
|
98 |
|
99 void Geometry::setColors(int start, GLfloat colorArray[4][4]) |
|
100 { |
|
101 int off = faces[start]; |
|
102 for (int i = 0; i < 4; ++i) |
|
103 colors[i + off] = QVector4D(colorArray[i][0], |
|
104 colorArray[i][1], |
|
105 colorArray[i][2], |
|
106 colorArray[i][3]); |
|
107 } |
|
108 |
|
109 Tile::Tile(const QVector3D &loc) |
|
110 : location(loc) |
|
111 , start(0) |
|
112 , count(0) |
|
113 , useFlatColor(false) |
|
114 , geom(0) |
|
115 { |
|
116 qSetColor(faceColor, QColor(Qt::darkGray)); |
|
117 } |
|
118 |
|
119 void Tile::setColors(GLfloat colorArray[4][4]) |
|
120 { |
|
121 useFlatColor = true; |
|
122 geom->setColors(start, colorArray); |
|
123 } |
|
124 |
|
125 static inline void qMultMatrix(const QMatrix4x4 &mat) |
|
126 { |
|
127 if (sizeof(qreal) == sizeof(GLfloat)) |
|
128 glMultMatrixf((GLfloat*)mat.constData()); |
|
129 #ifndef QT_OPENGL_ES |
|
130 else if (sizeof(qreal) == sizeof(GLdouble)) |
|
131 glMultMatrixd((GLdouble*)mat.constData()); |
|
132 #endif |
|
133 else |
|
134 { |
|
135 GLfloat fmat[16]; |
|
136 qreal const *r = mat.constData(); |
|
137 for (int i = 0; i < 16; ++i) |
|
138 fmat[i] = r[i]; |
|
139 glMultMatrixf(fmat); |
|
140 } |
|
141 } |
|
142 |
|
143 void Tile::draw() const |
|
144 { |
|
145 QMatrix4x4 mat; |
|
146 mat.translate(location); |
|
147 mat.rotate(orientation); |
|
148 glMatrixMode(GL_MODELVIEW); |
|
149 glPushMatrix(); |
|
150 qMultMatrix(mat); |
|
151 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, faceColor); |
|
152 glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, geom->indices() + start); |
|
153 glPopMatrix(); |
|
154 } |
|
155 |
|
156 TileBuilder::TileBuilder(Geometry *g, qreal depth, qreal size) |
|
157 : verts(4) |
|
158 , tex(4) |
|
159 , start(g->count()) |
|
160 , count(0) |
|
161 , geom(g) |
|
162 { |
|
163 // front face - make a square with bottom-left at origin |
|
164 verts[br].setX(size); |
|
165 verts[tr].setX(size); |
|
166 verts[tr].setY(size); |
|
167 verts[tl].setY(size); |
|
168 |
|
169 // these vert numbers are good for the tex-coords |
|
170 for (int i = 0; i < 4; ++i) |
|
171 tex[i] = verts[i].toVector2D(); |
|
172 |
|
173 // now move verts half cube width across so cube is centered on origin |
|
174 for (int i = 0; i < 4; ++i) |
|
175 verts[i] -= QVector3D(size / 2.0f, size / 2.0f, -depth); |
|
176 |
|
177 // add the front face |
|
178 g->addQuad(verts[bl], verts[br], verts[tr], verts[tl], tex); |
|
179 |
|
180 count = g->count() - start; |
|
181 } |
|
182 |
|
183 void TileBuilder::initialize(Tile *tile) const |
|
184 { |
|
185 tile->start = start; |
|
186 tile->count = count; |
|
187 tile->geom = geom; |
|
188 qSetColor(tile->faceColor, color); |
|
189 } |
|
190 |
|
191 Tile *TileBuilder::newTile(const QVector3D &loc) const |
|
192 { |
|
193 Tile *tile = new Tile(loc); |
|
194 initialize(tile); |
|
195 return tile; |
|
196 } |
|
197 |
|
198 Cube::Cube(const QVector3D &loc) |
|
199 : Tile(loc) |
|
200 , rot(0.0f) |
|
201 , r(0), a(0) |
|
202 { |
|
203 } |
|
204 |
|
205 Cube::~Cube() |
|
206 { |
|
207 } |
|
208 |
|
209 void Cube::setAltitude(qreal a) |
|
210 { |
|
211 if (location.y() != a) |
|
212 { |
|
213 location.setY(a); |
|
214 emit changed(); |
|
215 } |
|
216 } |
|
217 |
|
218 void Cube::setRange(qreal r) |
|
219 { |
|
220 if (location.x() != r) |
|
221 { |
|
222 location.setX(r); |
|
223 emit changed(); |
|
224 } |
|
225 } |
|
226 |
|
227 void Cube::setRotation(qreal r) |
|
228 { |
|
229 if (r != rot) |
|
230 { |
|
231 orientation = QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 1.0f, 1.0f), r); |
|
232 emit changed(); |
|
233 } |
|
234 } |
|
235 |
|
236 void Cube::removeBounce() |
|
237 { |
|
238 delete a; |
|
239 a = 0; |
|
240 delete r; |
|
241 r = 0; |
|
242 } |
|
243 |
|
244 void Cube::startAnimation() |
|
245 { |
|
246 if (r) |
|
247 { |
|
248 r->start(); |
|
249 r->setCurrentTime(startx); |
|
250 } |
|
251 if (a) |
|
252 a->start(); |
|
253 if (rtn) |
|
254 rtn->start(); |
|
255 } |
|
256 |
|
257 void Cube::setAnimationPaused(bool paused) |
|
258 { |
|
259 if (paused) |
|
260 { |
|
261 if (r) |
|
262 r->pause(); |
|
263 if (a) |
|
264 a->pause(); |
|
265 if (rtn) |
|
266 rtn->pause(); |
|
267 } |
|
268 else |
|
269 { |
|
270 if (r) |
|
271 r->resume(); |
|
272 if (a) |
|
273 a->resume(); |
|
274 if (rtn) |
|
275 rtn->resume(); |
|
276 } |
|
277 } |
|
278 |
|
279 CubeBuilder::CubeBuilder(Geometry *g, qreal depth, qreal size) |
|
280 : TileBuilder(g, depth) |
|
281 , ix(0) |
|
282 { |
|
283 for (int i = 0; i < 4; ++i) |
|
284 verts[i].setZ(size / 2.0f); |
|
285 // back face - "extrude" verts down |
|
286 QVector<QVector3D> back(verts); |
|
287 for (int i = 0; i < 4; ++i) |
|
288 back[i].setZ(-size / 2.0f); |
|
289 |
|
290 // add the back face |
|
291 g->addQuad(back[br], back[bl], back[tl], back[tr], tex); |
|
292 |
|
293 // add the sides |
|
294 g->addQuad(back[bl], back[br], verts[br], verts[bl], tex); |
|
295 g->addQuad(back[br], back[tr], verts[tr], verts[br], tex); |
|
296 g->addQuad(back[tr], back[tl], verts[tl], verts[tr], tex); |
|
297 g->addQuad(back[tl], back[bl], verts[bl], verts[tl], tex); |
|
298 |
|
299 count = g->count() - start; |
|
300 } |
|
301 |
|
302 Cube *CubeBuilder::newCube(const QVector3D &loc) const |
|
303 { |
|
304 Cube *c = new Cube(loc); |
|
305 initialize(c); |
|
306 qreal d = 4000.0f; |
|
307 qreal d3 = d / 3.0f; |
|
308 // Animate movement from left to right |
|
309 c->r = new QPropertyAnimation(c, "range"); |
|
310 c->r->setStartValue(-1.3f); |
|
311 c->r->setEndValue(1.3f); |
|
312 c->startx = ix * d3 * 3.0f; |
|
313 c->r->setDuration(d * 4.0f); |
|
314 c->r->setLoopCount(-1); |
|
315 c->r->setEasingCurve(QEasingCurve(QEasingCurve::CosineCurve)); |
|
316 // Animate movement from bottom to top |
|
317 c->a = new QPropertyAnimation(c, "altitude"); |
|
318 c->a->setEndValue(loc.y()); |
|
319 c->a->setStartValue(loc.y() + amplitudes[ix]); |
|
320 c->a->setDuration(d / speeds[ix]); |
|
321 c->a->setLoopCount(-1); |
|
322 c->a->setEasingCurve(QEasingCurve(QEasingCurve::CosineCurve)); |
|
323 // Animate rotation |
|
324 c->rtn = new QPropertyAnimation(c, "rotation"); |
|
325 c->rtn->setStartValue(c->rot); |
|
326 c->rtn->setEndValue(359.0f); |
|
327 c->rtn->setDuration(d * 2.0f); |
|
328 c->rtn->setLoopCount(-1); |
|
329 c->rtn->setDuration(d / 2); |
|
330 ix = (ix + 1) % 3; |
|
331 return c; |
|
332 } |