| 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 |
#ifndef CUBE_H |
| 52 |
#define CUBE_H |
| 53 |
|
| 54 |
#include <QtOpenGL/qgl.h> |
| 55 |
#include <QtCore/qvector.h> |
| 56 |
#include <QtCore/qsequentialanimationgroup.h> |
| 57 |
#include <QtGui/qmatrix4x4.h> |
| 58 |
#include <QtGui/qvector3d.h> |
| 59 |
#include <QtGui/qvector2d.h> |
| 60 |
|
| 61 |
QT_BEGIN_NAMESPACE |
| 62 |
class QPropertyAnimation; |
| 63 |
QT_END_NAMESPACE |
| 64 |
|
| 65 |
class Geometry |
| 66 |
{ |
| 67 |
public: |
| 68 |
void loadArrays() const; |
| 69 |
void addQuad(const QVector3D &a, const QVector3D &b, |
| 70 |
const QVector3D &c, const QVector3D &d, |
| 71 |
const QVector<QVector2D> &tex); |
| 72 |
void setColors(int start, GLfloat colors[4][4]); |
| 73 |
const GLushort *indices() const { return faces.constData(); } |
| 74 |
int count() const { return faces.count(); } |
| 75 |
private: |
| 76 |
QVector<GLushort> faces; |
| 77 |
QVector<QVector3D> vertices; |
| 78 |
QVector<QVector3D> normals; |
| 79 |
QVector<QVector2D> texCoords; |
| 80 |
QVector<QVector4D> colors; |
| 81 |
int append(const QVector3D &a, const QVector3D &n, const QVector2D &t); |
| 82 |
void addTri(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &n); |
| 83 |
friend class Tile; |
| 84 |
}; |
| 85 |
|
| 86 |
class Tile |
| 87 |
{ |
| 88 |
public: |
| 89 |
void draw() const; |
| 90 |
void setColors(GLfloat[4][4]); |
| 91 |
protected: |
| 92 |
Tile(const QVector3D &loc = QVector3D()); |
| 93 |
QVector3D location; |
| 94 |
QQuaternion orientation; |
| 95 |
private: |
| 96 |
int start; |
| 97 |
int count; |
| 98 |
bool useFlatColor; |
| 99 |
GLfloat faceColor[4]; |
| 100 |
Geometry *geom; |
| 101 |
friend class TileBuilder; |
| 102 |
}; |
| 103 |
|
| 104 |
class TileBuilder |
| 105 |
{ |
| 106 |
public: |
| 107 |
enum { bl, br, tr, tl }; |
| 108 |
explicit TileBuilder(Geometry *, qreal depth = 0.0f, qreal size = 1.0f); |
| 109 |
Tile *newTile(const QVector3D &loc = QVector3D()) const; |
| 110 |
void setColor(QColor c) { color = c; } |
| 111 |
protected: |
| 112 |
void initialize(Tile *) const; |
| 113 |
QVector<QVector3D> verts; |
| 114 |
QVector<QVector2D> tex; |
| 115 |
int start; |
| 116 |
int count; |
| 117 |
Geometry *geom; |
| 118 |
QColor color; |
| 119 |
}; |
| 120 |
|
| 121 |
class Cube : public QObject, public Tile |
| 122 |
{ |
| 123 |
Q_OBJECT |
| 124 |
Q_PROPERTY(qreal range READ range WRITE setRange) |
| 125 |
Q_PROPERTY(qreal altitude READ altitude WRITE setAltitude) |
| 126 |
Q_PROPERTY(qreal rotation READ rotation WRITE setRotation) |
| 127 |
public: |
| 128 |
Cube(const QVector3D &loc = QVector3D()); |
| 129 |
~Cube(); |
| 130 |
qreal range() { return location.x(); } |
| 131 |
void setRange(qreal r); |
| 132 |
qreal altitude() { return location.y(); } |
| 133 |
void setAltitude(qreal a); |
| 134 |
qreal rotation() { return rot; } |
| 135 |
void setRotation(qreal r); |
| 136 |
void removeBounce(); |
| 137 |
void startAnimation(); |
| 138 |
void setAnimationPaused(bool paused); |
| 139 |
signals: |
| 140 |
void changed(); |
| 141 |
private: |
| 142 |
qreal rot; |
| 143 |
QPropertyAnimation *r; |
| 144 |
QPropertyAnimation *rtn; |
| 145 |
QSequentialAnimationGroup *animGroup; |
| 146 |
qreal startx; |
| 147 |
friend class CubeBuilder; |
| 148 |
}; |
| 149 |
|
| 150 |
class CubeBuilder : public TileBuilder |
| 151 |
{ |
| 152 |
public: |
| 153 |
explicit CubeBuilder(Geometry *, qreal depth = 0.0f, qreal size = 1.0f); |
| 154 |
Cube *newCube(const QVector3D &loc = QVector3D()) const; |
| 155 |
private: |
| 156 |
mutable int ix; |
| 157 |
}; |
| 158 |
|
| 159 |
#endif // CUBE_H |