1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
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.
|
---|
25 | **
|
---|
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."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QGLWidget>
|
---|
42 | #include <QMatrix4x4>
|
---|
43 | #include <QVector3D>
|
---|
44 |
|
---|
45 | #include <qmath.h>
|
---|
46 |
|
---|
47 | #include "qtlogo.h"
|
---|
48 |
|
---|
49 | static const qreal tee_height = 0.311126;
|
---|
50 | static const qreal cross_width = 0.25;
|
---|
51 | static const qreal bar_thickness = 0.113137;
|
---|
52 | static const qreal inside_diam = 0.20;
|
---|
53 | static const qreal outside_diam = 0.30;
|
---|
54 | static const qreal logo_depth = 0.10;
|
---|
55 | static const int num_divisions = 32;
|
---|
56 |
|
---|
57 | //! [0]
|
---|
58 | struct Geometry
|
---|
59 | {
|
---|
60 | QVector<GLushort> faces;
|
---|
61 | QVector<QVector3D> vertices;
|
---|
62 | QVector<QVector3D> normals;
|
---|
63 | void appendSmooth(const QVector3D &a, const QVector3D &n, int from);
|
---|
64 | void appendFaceted(const QVector3D &a, const QVector3D &n);
|
---|
65 | void finalize();
|
---|
66 | void loadArrays() const;
|
---|
67 | };
|
---|
68 | //! [0]
|
---|
69 |
|
---|
70 | //! [1]
|
---|
71 | class Patch
|
---|
72 | {
|
---|
73 | public:
|
---|
74 | enum Smoothing { Faceted, Smooth };
|
---|
75 | Patch(Geometry *);
|
---|
76 | void setSmoothing(Smoothing s) { sm = s; }
|
---|
77 | void translate(const QVector3D &t);
|
---|
78 | void rotate(qreal deg, QVector3D axis);
|
---|
79 | void draw() const;
|
---|
80 | void addTri(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &n);
|
---|
81 | void addQuad(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &d);
|
---|
82 |
|
---|
83 | GLushort start;
|
---|
84 | GLushort count;
|
---|
85 | GLushort initv;
|
---|
86 |
|
---|
87 | GLfloat faceColor[4];
|
---|
88 | QMatrix4x4 mat;
|
---|
89 | Smoothing sm;
|
---|
90 | Geometry *geom;
|
---|
91 | };
|
---|
92 | //! [1]
|
---|
93 |
|
---|
94 | static inline void qSetColor(float colorVec[], QColor c)
|
---|
95 | {
|
---|
96 | colorVec[0] = c.redF();
|
---|
97 | colorVec[1] = c.greenF();
|
---|
98 | colorVec[2] = c.blueF();
|
---|
99 | colorVec[3] = c.alphaF();
|
---|
100 | }
|
---|
101 |
|
---|
102 | void Geometry::loadArrays() const
|
---|
103 | {
|
---|
104 | glVertexPointer(3, GL_FLOAT, 0, vertices.constData());
|
---|
105 | glNormalPointer(GL_FLOAT, 0, normals.constData());
|
---|
106 | }
|
---|
107 |
|
---|
108 | void Geometry::finalize()
|
---|
109 | {
|
---|
110 | // TODO: add vertex buffer uploading here
|
---|
111 |
|
---|
112 | // Finish smoothing normals by ensuring accumulated normals are returned
|
---|
113 | // to length 1.0.
|
---|
114 | for (int i = 0; i < normals.count(); ++i)
|
---|
115 | normals[i].normalize();
|
---|
116 | }
|
---|
117 |
|
---|
118 | void Geometry::appendSmooth(const QVector3D &a, const QVector3D &n, int from)
|
---|
119 | {
|
---|
120 | // Smooth normals are acheived by averaging the normals for faces meeting
|
---|
121 | // at a point. First find the point in geometry already generated
|
---|
122 | // (working backwards, since most often the points shared are between faces
|
---|
123 | // recently added).
|
---|
124 | int v = vertices.count() - 1;
|
---|
125 | for ( ; v >= from; --v)
|
---|
126 | if (qFuzzyCompare(vertices[v], a))
|
---|
127 | break;
|
---|
128 | if (v < from)
|
---|
129 | {
|
---|
130 | // The vert was not found so add it as a new one, and initialize
|
---|
131 | // its corresponding normal
|
---|
132 | v = vertices.count();
|
---|
133 | vertices.append(a);
|
---|
134 | normals.append(n);
|
---|
135 | }
|
---|
136 | else
|
---|
137 | {
|
---|
138 | // Vert found, accumulate normals into corresponding normal slot.
|
---|
139 | // Must call finalize once finished accumulating normals
|
---|
140 | normals[v] += n;
|
---|
141 | }
|
---|
142 | // In both cases (found or not) reference the vert via its index
|
---|
143 | faces.append(v);
|
---|
144 | }
|
---|
145 |
|
---|
146 | void Geometry::appendFaceted(const QVector3D &a, const QVector3D &n)
|
---|
147 | {
|
---|
148 | // Faceted normals are achieved by duplicating the vert for every
|
---|
149 | // normal, so that faces meeting at a vert get a sharp edge.
|
---|
150 | int v = vertices.count();
|
---|
151 | vertices.append(a);
|
---|
152 | normals.append(n);
|
---|
153 | faces.append(v);
|
---|
154 | }
|
---|
155 |
|
---|
156 | Patch::Patch(Geometry *g)
|
---|
157 | : start(g->faces.count())
|
---|
158 | , count(0)
|
---|
159 | , initv(g->vertices.count())
|
---|
160 | , sm(Patch::Smooth)
|
---|
161 | , geom(g)
|
---|
162 | {
|
---|
163 | qSetColor(faceColor, QColor(Qt::darkGray));
|
---|
164 | }
|
---|
165 |
|
---|
166 | void Patch::rotate(qreal deg, QVector3D axis)
|
---|
167 | {
|
---|
168 | mat.rotate(deg, axis);
|
---|
169 | }
|
---|
170 |
|
---|
171 | void Patch::translate(const QVector3D &t)
|
---|
172 | {
|
---|
173 | mat.translate(t);
|
---|
174 | }
|
---|
175 |
|
---|
176 | static inline void qMultMatrix(const QMatrix4x4 &mat)
|
---|
177 | {
|
---|
178 | if (sizeof(qreal) == sizeof(GLfloat))
|
---|
179 | glMultMatrixf((GLfloat*)mat.constData());
|
---|
180 | #ifndef QT_OPENGL_ES
|
---|
181 | else if (sizeof(qreal) == sizeof(GLdouble))
|
---|
182 | glMultMatrixd((GLdouble*)mat.constData());
|
---|
183 | #endif
|
---|
184 | else
|
---|
185 | {
|
---|
186 | GLfloat fmat[16];
|
---|
187 | qreal const *r = mat.constData();
|
---|
188 | for (int i = 0; i < 16; ++i)
|
---|
189 | fmat[i] = r[i];
|
---|
190 | glMultMatrixf(fmat);
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | //! [2]
|
---|
195 | void Patch::draw() const
|
---|
196 | {
|
---|
197 | glPushMatrix();
|
---|
198 | qMultMatrix(mat);
|
---|
199 | glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, faceColor);
|
---|
200 |
|
---|
201 | const GLushort *indices = geom->faces.constData();
|
---|
202 | glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, indices + start);
|
---|
203 | glPopMatrix();
|
---|
204 | }
|
---|
205 | //! [2]
|
---|
206 |
|
---|
207 | void Patch::addTri(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &n)
|
---|
208 | {
|
---|
209 | QVector3D norm = n.isNull() ? QVector3D::normal(a, b, c) : n;
|
---|
210 | if (sm == Smooth)
|
---|
211 | {
|
---|
212 | geom->appendSmooth(a, norm, initv);
|
---|
213 | geom->appendSmooth(b, norm, initv);
|
---|
214 | geom->appendSmooth(c, norm, initv);
|
---|
215 | }
|
---|
216 | else
|
---|
217 | {
|
---|
218 | geom->appendFaceted(a, norm);
|
---|
219 | geom->appendFaceted(b, norm);
|
---|
220 | geom->appendFaceted(c, norm);
|
---|
221 | }
|
---|
222 | count += 3;
|
---|
223 | }
|
---|
224 |
|
---|
225 | void Patch::addQuad(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &d)
|
---|
226 | {
|
---|
227 | QVector3D norm = QVector3D::normal(a, b, c);
|
---|
228 | if (sm == Smooth)
|
---|
229 | {
|
---|
230 | addTri(a, b, c, norm);
|
---|
231 | addTri(a, c, d, norm);
|
---|
232 | }
|
---|
233 | else
|
---|
234 | {
|
---|
235 | // If faceted share the two common verts
|
---|
236 | addTri(a, b, c, norm);
|
---|
237 | int k = geom->vertices.count();
|
---|
238 | geom->appendSmooth(a, norm, k);
|
---|
239 | geom->appendSmooth(c, norm, k);
|
---|
240 | geom->appendFaceted(d, norm);
|
---|
241 | count += 3;
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | static inline QVector<QVector3D> extrude(const QVector<QVector3D> &verts, qreal depth)
|
---|
246 | {
|
---|
|
---|