1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #ifndef CUBE_H
42 #define CUBE_H
43 
44 #include <QtOpenGL/qgl.h>
45 #include <QtCore/qvector.h>
46 #include <QtCore/qsequentialanimationgroup.h>
47 #include <QtGui/qmatrix4x4.h>
48 #include <QtGui/qvector3d.h>
49 #include <QtGui/qvector2d.h>
50 
51 QT_BEGIN_NAMESPACE
52 class QPropertyAnimation;
53 QT_END_NAMESPACE
54 
55 class Geometry
56 {
57 public:
58     void loadArrays() const;
59     void addQuad(const QVector3D &a, const QVector3D &b,
60                  const QVector3D &c, const QVector3D &d,
61                  const QVector<QVector2D> &tex);
62     void setColors(int start, GLfloat colors[4][4]);
indices()63     const GLushort *indices() const { return faces.constData(); }
count()64     int count() const { return faces.count(); }
65 private:
66     QVector<GLushort> faces;
67     QVector<QVector3D> vertices;
68     QVector<QVector3D> normals;
69     QVector<QVector2D> texCoords;
70     QVector<QVector4D> colors;
71     int append(const QVector3D &a, const QVector3D &n, const QVector2D &t);
72     void addTri(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &n);
73     friend class Tile;
74 };
75 
76 class Tile
77 {
78 public:
79     void draw() const;
80     void setColors(GLfloat[4][4]);
81 protected:
82     Tile(const QVector3D &loc = QVector3D());
83     QVector3D location;
84     QQuaternion orientation;
85 private:
86     int start;
87     int count;
88     bool useFlatColor;
89     GLfloat faceColor[4];
90     Geometry *geom;
91     friend class TileBuilder;
92 };
93 
94 class TileBuilder
95 {
96 public:
97     enum { bl, br, tr, tl };
98     TileBuilder(Geometry *, qreal depth = 0.0f, qreal size = 1.0f);
99     Tile *newTile(const QVector3D &loc = QVector3D()) const;
setColor(QColor c)100     void setColor(QColor c) { color = c; }
101 protected:
102     void initialize(Tile *) const;
103     QVector<QVector3D> verts;
104     QVector<QVector2D> tex;
105     int start;
106     int count;
107     Geometry *geom;
108     QColor color;
109 };
110 
111 class Cube : public QObject, public Tile
112 {
113     Q_OBJECT
114     Q_PROPERTY(qreal range READ range WRITE setRange)
115     Q_PROPERTY(qreal altitude READ altitude WRITE setAltitude)
116     Q_PROPERTY(qreal rotation READ rotation WRITE setRotation)
117 public:
118     Cube(const QVector3D &loc = QVector3D());
119     ~Cube();
range()120     qreal range() { return location.x(); }
121     void setRange(qreal r);
altitude()122     qreal altitude() { return location.y(); }
123     void setAltitude(qreal a);
rotation()124     qreal rotation() { return rot; }
125     void setRotation(qreal r);
126     void removeBounce();
127     void startAnimation();
128     void setAnimationPaused(bool paused);
129 signals:
130     void changed();
131 private:
132     qreal rot;
133     QPropertyAnimation *r;
134     QPropertyAnimation *rtn;
135     QSequentialAnimationGroup *animGroup;
136     qreal startx;
137     friend class CubeBuilder;
138 };
139 
140 class CubeBuilder : public TileBuilder
141 {
142 public:
143     CubeBuilder(Geometry *, qreal depth = 0.0f, qreal size = 1.0f);
144     Cube *newCube(const QVector3D &loc = QVector3D()) const;
145 private:
146     mutable int ix;
147 };
148 
149 #endif // CUBE_H
150