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