[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[556] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[556] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[556] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include "cube.h"
|
---|
| 42 | #include "glwidget.h"
|
---|
| 43 |
|
---|
| 44 | #include <QtGui/QImage>
|
---|
| 45 | #include <QtCore/QPropertyAnimation>
|
---|
| 46 |
|
---|
| 47 | static const qreal FACE_SIZE = 0.4;
|
---|
| 48 |
|
---|
| 49 | static const qreal speeds[] = { 1.8f, 2.4f, 3.6f };
|
---|
| 50 | static const qreal amplitudes[] = { 2.0f, 2.5f, 3.0f };
|
---|
| 51 |
|
---|
| 52 | static inline void qSetColor(float colorVec[], QColor c)
|
---|
| 53 | {
|
---|
| 54 | colorVec[0] = c.redF();
|
---|
| 55 | colorVec[1] = c.greenF();
|
---|
| 56 | colorVec[2] = c.blueF();
|
---|
| 57 | colorVec[3] = c.alphaF();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | int Geometry::append(const QVector3D &a, const QVector3D &n, const QVector2D &t)
|
---|
| 61 | {
|
---|
| 62 | int v = vertices.count();
|
---|
| 63 | vertices.append(a);
|
---|
| 64 | normals.append(n);
|
---|
| 65 | texCoords.append(t);
|
---|
| 66 | faces.append(v);
|
---|
| 67 | colors.append(QVector4D(0.6f, 0.6f, 0.6f, 1.0f));
|
---|
| 68 | return v;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | void Geometry::addQuad(const QVector3D &a, const QVector3D &b,
|
---|
| 72 | const QVector3D &c, const QVector3D &d,
|
---|
| 73 | const QVector<QVector2D> &tex)
|
---|
| 74 | {
|
---|
| 75 | QVector3D norm = QVector3D::normal(a, b, c);
|
---|
| 76 | // append first triangle
|
---|
| 77 | int aref = append(a, norm, tex[0]);
|
---|
| 78 | append(b, norm, tex[1]);
|
---|
| 79 | int cref = append(c, norm, tex[2]);
|
---|
| 80 | // append second triangle
|
---|
| 81 | faces.append(aref);
|
---|
| 82 | faces.append(cref);
|
---|
| 83 | append(d, norm, tex[3]);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | void Geometry::loadArrays() const
|
---|
| 87 | {
|
---|
| 88 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
| 89 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
| 90 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
| 91 | glEnableClientState(GL_COLOR_ARRAY);
|
---|
| 92 | glVertexPointer(3, GL_FLOAT, 0, vertices.constData());
|
---|
| 93 | glNormalPointer(GL_FLOAT, 0, normals.constData());
|
---|
| 94 | glTexCoordPointer(2, GL_FLOAT, 0, texCoords.constData());
|
---|
| 95 | glColorPointer(4, GL_FLOAT, 0, colors.constData());
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | void Geometry::setColors(int start, GLfloat colorArray[4][4])
|
---|
| 99 | {
|
---|
| 100 | int off = faces[start];
|
---|
| 101 | for (int i = 0; i < 4; ++i)
|
---|
| 102 | colors[i + off] = QVector4D(colorArray[i][0],
|
---|
| 103 | colorArray[i][1],
|
---|
| 104 | colorArray[i][2],
|
---|
| 105 | colorArray[i][3]);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | Tile::Tile(const QVector3D &loc)
|
---|
| 109 | : location(loc)
|
---|
| 110 | , start(0)
|
---|
| 111 | , count(0)
|
---|
| 112 | , useFlatColor(false)
|
---|
| 113 | , geom(0)
|
---|
| 114 | {
|
---|
| 115 | qSetColor(faceColor, QColor(Qt::darkGray));
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | void Tile::setColors(GLfloat colorArray[4][4])
|
---|
| 119 | {
|
---|
| 120 | useFlatColor = true;
|
---|
| 121 | geom->setColors(start, colorArray);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | static inline void qMultMatrix(const QMatrix4x4 &mat)
|
---|
| 125 | {
|
---|
| 126 | if (sizeof(qreal) == sizeof(GLfloat))
|
---|
| 127 | glMultMatrixf((GLfloat*)mat.constData());
|
---|
| 128 | #ifndef QT_OPENGL_ES
|
---|
| 129 | else if (sizeof(qreal) == sizeof(GLdouble))
|
---|
| 130 | glMultMatrixd((GLdouble*)mat.constData());
|
---|
| 131 | #endif
|
---|
| 132 | else
|
---|
| 133 | {
|
---|
| 134 | GLfloat fmat[16];
|
---|
| 135 | qreal const *r = mat.constData();
|
---|
| 136 | for (int i = 0; i < 16; ++i)
|
---|
| 137 | fmat[i] = r[i];
|
---|
| 138 | glMultMatrixf(fmat);
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | void Tile::draw() const
|
---|
| 143 | {
|
---|
| 144 | QMatrix4x4 mat;
|
---|
| 145 | mat.translate(location);
|
---|
| 146 | mat.rotate(orientation);
|
---|
| 147 | glMatrixMode(GL_MODELVIEW);
|
---|
| 148 | glPushMatrix();
|
---|
| 149 | qMultMatrix(mat);
|
---|
| 150 | glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, faceColor);
|
---|
| 151 | glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, geom->indices() + start);
|
---|
| 152 | glPopMatrix();
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | TileBuilder::TileBuilder(Geometry *g, qreal depth, qreal size)
|
---|
| 156 | : verts(4)
|
---|
| 157 | , tex(4)
|
---|
| 158 | , start(g->count())
|
---|
| 159 | , count(0)
|
---|
| 160 | , geom(g)
|
---|
| 161 | {
|
---|
| 162 | // front face - make a square with bottom-left at origin
|
---|
| 163 | verts[br].setX(size);
|
---|
| 164 | verts[tr].setX(size);
|
---|
| 165 | verts[tr].setY(size);
|
---|
| 166 | verts[tl].setY(size);
|
---|
| 167 |
|
---|
| 168 | // these vert numbers are good for the tex-coords
|
---|
| 169 | for (int i = 0; i < 4; ++i)
|
---|
| 170 | tex[i] = verts[i].toVector2D();
|
---|
| 171 |
|
---|
| 172 | // now move verts half cube width across so cube is centered on origin
|
---|
| 173 | for (int i = 0; i < 4; ++i)
|
---|
| 174 | verts[i] -= QVector3D(size / 2.0f, size / 2.0f, -depth);
|
---|
| 175 |
|
---|
| 176 | // add the front face
|
---|
| 177 | g->addQuad(verts[bl], verts[br], verts[tr], verts[tl], tex);
|
---|
| 178 |
|
---|
| 179 | count = g->count() - start;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | void TileBuilder::initialize(Tile *tile) const
|
---|
| 183 | {
|
---|
| 184 | tile->start = start;
|
---|
| 185 | tile->count = count;
|
---|
| 186 | tile->geom = geom;
|
---|
| 187 | qSetColor(tile->faceColor, color);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | Tile *TileBuilder::newTile(const QVector3D &loc) const
|
---|
| 191 | {
|
---|
| 192 | Tile *tile = new Tile(loc);
|
---|
| 193 | initialize(tile);
|
---|
| 194 | return tile;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | Cube::Cube(const QVector3D &loc)
|
---|
| 198 | : Tile(loc)
|
---|
| 199 | , rot(0.0f)
|
---|
| 200 | , r(0), a(0)
|
---|
| 201 | {
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | Cube::~Cube()
|
---|
| 205 | {
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | void Cube::setAltitude(qreal a)
|
---|
| 209 | {
|
---|
| 210 | if (location.y() != a)
|
---|
| 211 | {
|
---|
| 212 | location.setY(a);
|
---|
| 213 | emit changed();
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | void Cube::setRange(qreal r)
|
---|
| 218 | {
|
---|
| 219 | if (location.x() != r)
|
---|
| 220 | {
|
---|
| 221 | location.setX(r);
|
---|
| 222 | emit changed();
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | void Cube::setRotation(qreal r)
|
---|
| 227 | {
|
---|
| 228 | if (r != rot)
|
---|
| 229 | {
|
---|
| 230 | orientation = QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 1.0f, 1.0f), r);
|
---|
| 231 | emit changed();
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | void Cube::removeBounce()
|
---|
| 236 | {
|
---|
| 237 | delete a;
|
---|
| 238 | a = 0;
|
---|
| 239 | delete r;
|
---|
| 240 | r = 0;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | void Cube::startAnimation()
|
---|
| 244 | {
|
---|
| 245 | if (r)
|
---|
| 246 | {
|
---|
| 247 | r->start();
|
---|
| 248 | r->setCurrentTime(startx);
|
---|
| 249 | }
|
---|
| 250 | if (a)
|
---|
| 251 | a->start();
|
---|
| 252 | if (rtn)
|
---|
| 253 | rtn->start();
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | void Cube::setAnimationPaused(bool paused)
|
---|
| 257 | {
|
---|
| 258 | if (paused)
|
---|
| 259 | {
|
---|
| 260 | if (r)
|
---|
| 261 | r->pause();
|
---|
| 262 | if (a)
|
---|
| 263 | a->pause();
|
---|
| 264 | if (rtn)
|
---|
| 265 | rtn->pause();
|
---|
| 266 | }
|
---|
| 267 | else
|
---|
| 268 | {
|
---|
| 269 | if (r)
|
---|
| 270 | r->resume();
|
---|
| 271 | if (a)
|
---|
| 272 | a->resume();
|
---|
| 273 | if (rtn)
|
---|
| 274 | rtn->resume();
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | CubeBuilder::CubeBuilder(Geometry *g, qreal depth, qreal size)
|
---|
| 279 | : TileBuilder(g, depth)
|
---|
| 280 | , ix(0)
|
---|
| 281 | {
|
---|
| 282 | for (int i = 0; i < 4; ++i)
|
---|
| 283 | verts[i].setZ(size / 2.0f);
|
---|
| 284 | // back face - "extrude" verts down
|
---|
| 285 | QVector<QVector3D> back(verts);
|
---|
| 286 | for (int i = 0; i < 4; ++i)
|
---|
| 287 | back[i].setZ(-size / 2.0f);
|
---|
| 288 |
|
---|
| 289 | // add the back face
|
---|
| 290 | g->addQuad(back[br], back[bl], back[tl], back[tr], tex);
|
---|
| 291 |
|
---|
| 292 | // add the sides
|
---|
| 293 | g->addQuad(back[bl], back[br], verts[br], verts[bl], tex);
|
---|
| 294 | g->addQuad(back[br], back[tr], verts[tr], verts[br], tex);
|
---|
| 295 | g->addQuad(back[tr], back[tl], verts[tl], verts[tr], tex);
|
---|
| 296 | g->addQuad(back[tl], back[bl], verts[bl], verts[tl], tex);
|
---|
| 297 |
|
---|
| 298 | count = g->count() - start;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | Cube *CubeBuilder::newCube(const QVector3D &loc) const
|
---|
| 302 | {
|
---|
| 303 | Cube *c = new Cube(loc);
|
---|
| 304 | initialize(c);
|
---|
| 305 | qreal d = 4000.0f;
|
---|
| 306 | qreal d3 = d / 3.0f;
|
---|
| 307 | // Animate movement from left to right
|
---|
| 308 | c->r = new QPropertyAnimation(c, "range");
|
---|
| 309 | c->r->setStartValue(-1.3f);
|
---|
| 310 | c->r->setEndValue(1.3f);
|
---|
| 311 | c->startx = ix * d3 * 3.0f;
|
---|
| 312 | c->r->setDuration(d * 4.0f);
|
---|
| 313 | c->r->setLoopCount(-1);
|
---|
| 314 | c->r->setEasingCurve(QEasingCurve(QEasingCurve::CosineCurve));
|
---|
| 315 | // Animate movement from bottom to top
|
---|
| 316 | c->a = new QPropertyAnimation(c, "altitude");
|
---|
| 317 | c->a->setEndValue(loc.y());
|
---|
| 318 | c->a->setStartValue(loc.y() + amplitudes[ix]);
|
---|
| 319 | c->a->setDuration(d / speeds[ix]);
|
---|
| 320 | c->a->setLoopCount(-1);
|
---|
| 321 | c->a->setEasingCurve(QEasingCurve(QEasingCurve::CosineCurve));
|
---|
| 322 | // Animate rotation
|
---|
| 323 | c->rtn = new QPropertyAnimation(c, "rotation");
|
---|
| 324 | c->rtn->setStartValue(c->rot);
|
---|
| 325 | c->rtn->setEndValue(359.0f);
|
---|
| 326 | c->rtn->setDuration(d * 2.0f);
|
---|
| 327 | c->rtn->setLoopCount(-1);
|
---|
| 328 | c->rtn->setDuration(d / 2);
|
---|
| 329 | ix = (ix + 1) % 3;
|
---|
| 330 | return c;
|
---|
| 331 | }
|
---|