| 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 "qtlogo.h" |
| 52 |
|
| 53 |
#include <QGLWidget> |
| 54 |
#include <QMatrix4x4> |
| 55 |
#include <QVector3D> |
| 56 |
#include <QOpenGLFunctions_1_1> |
| 57 |
|
| 58 |
#include <qmath.h> |
| 59 |
|
| 60 |
static const qreal tee_height = 0.311126; |
| 61 |
static const qreal cross_width = 0.25; |
| 62 |
static const qreal bar_thickness = 0.113137; |
| 63 |
static const qreal logo_depth = 0.10; |
| 64 |
|
| 65 |
//! [0] |
| 66 |
struct Geometry |
| 67 |
{ |
| 68 |
QVector<GLushort> faces; |
| 69 |
QVector<QVector3D> vertices; |
| 70 |
QVector<QVector3D> normals; |
| 71 |
void appendSmooth(const QVector3D &a, const QVector3D &n, int from); |
| 72 |
void appendFaceted(const QVector3D &a, const QVector3D &n); |
| 73 |
void finalize(); |
| 74 |
void loadArrays(QOpenGLFunctions_1_1 *functions) const; |
| 75 |
}; |
| 76 |
//! [0] |
| 77 |
|
| 78 |
//! [1] |
| 79 |
class Patch |
| 80 |
{ |
| 81 |
public: |
| 82 |
enum Smoothing { Faceted, Smooth }; |
| 83 |
Patch(Geometry *); |
| 84 |
void setSmoothing(Smoothing s) { sm = s; } |
| 85 |
void translate(const QVector3D &t); |
| 86 |
void rotate(qreal deg, QVector3D axis); |
| 87 |
void draw(QOpenGLFunctions_1_1 *functions) const; |
| 88 |
void addTri(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &n); |
| 89 |
void addQuad(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &d); |
| 90 |
|
| 91 |
GLushort start; |
| 92 |
GLushort count; |
| 93 |
GLushort initv; |
| 94 |
|
| 95 |
GLfloat faceColor[4]; |
| 96 |
QMatrix4x4 mat; |
| 97 |
Smoothing sm; |
| 98 |
Geometry *geom; |
| 99 |
}; |
| 100 |
//! [1] |
| 101 |
|
| 102 |
static inline void qSetColor(float colorVec[], QColor c) |
| 103 |
{ |
| 104 |
colorVec[0] = c.redF(); |
| 105 |
colorVec[1] = c.greenF(); |
| 106 |
colorVec[2] = c.blueF(); |
| 107 |
colorVec[3] = c.alphaF(); |
| 108 |
} |
| 109 |
|
| 110 |
void Geometry::loadArrays(QOpenGLFunctions_1_1 *functions) const |
| 111 |
{ |
| 112 |
functions->glVertexPointer(3, GL_FLOAT, 0, vertices.constData()); |
| 113 |
functions->glNormalPointer(GL_FLOAT, 0, normals.constData()); |
| 114 |
} |
| 115 |
|
| 116 |
void Geometry::finalize() |
| 117 |
{ |
| 118 |
// TODO: add vertex buffer uploading here |
| 119 |
|
| 120 |
// Finish smoothing normals by ensuring accumulated normals are returned |
| 121 |
// to length 1.0. |
| 122 |
for (int i = 0; i < normals.count(); ++i) |
| 123 |
normals[i].normalize(); |
| 124 |
} |
| 125 |
|
| 126 |
void Geometry::appendSmooth(const QVector3D &a, const QVector3D &n, int from) |
| 127 |
{ |
| 128 |
// Smooth normals are achieved by averaging the normals for faces meeting |
| 129 |
// at a point. First find the point in geometry already generated |
| 130 |
// (working backwards, since most often the points shared are between faces |
| 131 |
// recently added). |
| 132 |
int v = vertices.count() - 1; |
| 133 |
for ( ; v >= from; --v) |
| 134 |
if (qFuzzyCompare(vertices[v], a)) |
| 135 |
break; |
| 136 |
|
| 137 |
if (v < from) { |
| 138 |
// The vertex was not found so add it as a new one, and initialize |
| 139 |
// its corresponding normal |
| 140 |
v = vertices.count(); |
| 141 |
vertices.append(a); |
| 142 |
normals.append(n); |
| 143 |
} else { |
| 144 |
// Vert found, accumulate normals into corresponding normal slot. |
| 145 |
// Must call finalize once finished accumulating normals |
| 146 |
normals[v] += n; |
| 147 |
} |
| 148 |
|
| 149 |
// In both cases (found or not) reference the vertex via its index |
| 150 |
faces.append(v); |
| 151 |
} |
| 152 |
|
| 153 |
void Geometry::appendFaceted(const QVector3D &a, const QVector3D &n) |
| 154 |
{ |
| 155 |
// Faceted normals are achieved by duplicating the vertex for every |
| 156 |
// normal, so that faces meeting at a vertex get a sharp edge. |
| 157 |
int v = vertices.count(); |
| 158 |
vertices.append(a); |
| 159 |
normals.append(n); |
| 160 |
faces.append(v); |
| 161 |
} |
| 162 |
|
| 163 |
Patch::Patch(Geometry *g) |
| 164 |
: start(g->faces.count()) |
| 165 |
, count(0) |
| 166 |
, initv(g->vertices.count()) |
| 167 |
, sm(Patch::Smooth) |
| 168 |
, geom(g) |
| 169 |
{ |
| 170 |
qSetColor(faceColor, QColor(Qt::darkGray)); |
| 171 |
} |
| 172 |
|
| 173 |
void Patch::rotate(qreal deg, QVector3D axis) |
| 174 |
{ |
| 175 |
mat.rotate(deg, axis); |
| 176 |
} |
| 177 |
|
| 178 |
void Patch::translate(const QVector3D &t) |
| 179 |
{ |
| 180 |
mat.translate(t); |
| 181 |
} |
| 182 |
|
| 183 |
//! [2] |
| 184 |
void Patch::draw(QOpenGLFunctions_1_1 *functions) const |
| 185 |
{ |
| 186 |
functions->glPushMatrix(); |
| 187 |
functions->glMultMatrixf(mat.constData()); |
| 188 |
functions->glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, faceColor); |
| 189 |
|
| 190 |
const GLushort *indices = geom->faces.constData(); |
| 191 |
functions->glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, indices + start); |
| 192 |
functions->glPopMatrix(); |
| 193 |
} |
| 194 |
//! [2] |
| 195 |
|
| 196 |
void Patch::addTri(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &n) |
| 197 |
{ |
| 198 |
QVector3D norm = n.isNull() ? QVector3D::normal(a, b, c) : n; |
| 199 |
|
| 200 |
if (sm == Smooth) { |
| 201 |
geom->appendSmooth(a, norm, initv); |
| 202 |
geom->appendSmooth(b, norm, initv); |
| 203 |
geom->appendSmooth(c, norm, initv); |
| 204 |
} else { |
| 205 |
geom->appendFaceted(a, norm); |
| 206 |
geom->appendFaceted(b, norm); |
| 207 |
geom->appendFaceted(c, norm); |
| 208 |
} |
| 209 |
|
| 210 |
count += 3; |
| 211 |
} |
| 212 |
|
| 213 |
void Patch::addQuad(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &d) |
| 214 |
{ |
| 215 |
QVector3D norm = QVector3D::normal(a, b, c); |
| 216 |
|
| 217 |
if (sm == Smooth) { |
| 218 |
addTri(a, b, c, norm); |
| 219 |
addTri(a, c, d, norm); |
| 220 |
} else { |
| 221 |
// If faceted share the two common vertices |
| 222 |
addTri(a, b, c, norm); |
| 223 |
int k = geom->vertices.count(); |
| 224 |
geom->appendSmooth(a, norm, k); |
| 225 |
geom->appendSmooth(c, norm, k); |
| 226 |
geom->appendFaceted(d, norm); |
| 227 |
count += 3; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
static inline QVector<QVector3D> extrude(const QVector<QVector3D> &vertices, qreal depth) |
| 232 |
{ |
| 233 |
QVector<QVector3D> extr = vertices; |
| 234 |
for (int v = 0; v < extr.count(); ++v) |
| 235 |
extr[v].setZ(extr[v].z() - depth); |
| 236 |
return extr; |
| 237 |
} |
| 238 |
|
| 239 |
class Rectoid |
| 240 |
{ |
| 241 |
public: |
| 242 |
void translate(const QVector3D &t) |
| 243 |
{ |
| 244 |
for (int i = 0; i < parts.count(); ++i) |
| 245 |
parts[i]->translate(t); |
| 246 |
} |
| 247 |
|
| 248 |
void rotate(qreal deg, QVector3D axis) |
| 249 |
{ |
| 250 |
for (int i = 0; i < parts.count(); ++i) |
| 251 |
parts[i]->rotate(deg, axis); |
| 252 |
} |
| 253 |
|
| 254 |
// No special Rectoid destructor - the parts are fetched out of this member |
| 255 |
// variable, and destroyed by the new owner |
| 256 |
QList<Patch *> parts; |
| 257 |
}; |
| 258 |
|
| 259 |
class RectPrism : public Rectoid |
| 260 |
{ |
| 261 |
public: |
| 262 |
RectPrism(Geometry *g, qreal width, qreal height, qreal depth); |
| 263 |
}; |
| 264 |
|
| 265 |
RectPrism::RectPrism(Geometry *g, qreal width, qreal height, qreal depth) |
| 266 |
{ |
| 267 |
enum { bl, br, tr, tl }; |
| 268 |
Patch *fb = new Patch(g); |
| 269 |
fb->setSmoothing(Patch::Faceted); |
| 270 |
|
| 271 |
// front face |
| 272 |
QVector<QVector3D> r(4); |
| 273 |
r[br].setX(width); |
| 274 |
r[tr].setX(width); |
| 275 |
r[tr].setY(height); |
| 276 |
r[tl].setY(height); |
| 277 |
QVector3D adjToCenter(-width / 2.0, -height / 2.0, depth / 2.0); |
| 278 |
for (int i = 0; i < 4; ++i) |
| 279 |
r[i] += adjToCenter; |
| 280 |
fb->addQuad(r[bl], r[br], r[tr], r[tl]); |
| 281 |
|
| 282 |
// back face |
| 283 |
QVector<QVector3D> s = extrude(r, depth); |
| 284 |
fb->addQuad(s[tl], s[tr], s[br], s[bl]); |
| 285 |
|
| 286 |
// side faces |
| 287 |
Patch *sides = new Patch(g); |
| 288 |
sides->setSmoothing(Patch::Faceted); |
| 289 |
sides->addQuad(s[bl], s[br], r[br], r[bl]); |
| 290 |
sides->addQuad(s[br], s[tr], r[tr], r[br]); |
| 291 |
sides->addQuad(s[tr], s[tl], r[tl], r[tr]); |
| 292 |
sides->addQuad(s[tl], s[bl], r[bl], r[tl]); |
| 293 |
|
| 294 |
parts << fb << sides; |
| 295 |
} |
| 296 |
|
| 297 |
class RectTorus : public Rectoid |
| 298 |
{ |
| 299 |
public: |
| 300 |
RectTorus(Geometry *g, qreal iRad, qreal oRad, qreal depth, int numSectors); |
| 301 |
}; |
| 302 |
|
| 303 |
RectTorus::RectTorus(Geometry *g, qreal iRad, qreal oRad, qreal depth, int k) |
| 304 |
{ |
| 305 |
QVector<QVector3D> inside; |
| 306 |
QVector<QVector3D> outside; |
| 307 |
for (int i = 0; i < k; ++i) { |
| 308 |
qreal angle = (i * 2 * M_PI) / k; |
| 309 |
inside << QVector3D(iRad * qSin(angle), iRad * qCos(angle), depth / 2.0); |
| 310 |
outside << QVector3D(oRad * qSin(angle), oRad * qCos(angle), depth / 2.0); |
| 311 |
} |
| 312 |
inside << QVector3D(0.0, iRad, 0.0); |
| 313 |
outside << QVector3D(0.0, oRad, 0.0); |
| 314 |
QVector<QVector3D> in_back = extrude(inside, depth); |
| 315 |
QVector<QVector3D> out_back = extrude(outside, depth); |
| 316 |
|
| 317 |
// Create front, back and sides as separate patches so that smooth normals |
| 318 |
// are generated for the curving sides, but a faceted edge is created between |
| 319 |
// sides and front/back |
| 320 |
Patch *front = new Patch(g); |
| 321 |
for (int i = 0; i < k; ++i) |
| 322 |
front->addQuad(outside[i], inside[i], |
| 323 |
inside[(i + 1) % k], outside[(i + 1) % k]); |
| 324 |
Patch *back = new Patch(g); |
| 325 |
for (int i = 0; i < k; ++i) |
| 326 |
back->addQuad(in_back[i], out_back[i], |
| 327 |
out_back[(i + 1) % k], in_back[(i + 1) % k]); |
| 328 |
Patch *is = new Patch(g); |
| 329 |
for (int i = 0; i < k; ++i) |
| 330 |
is->addQuad(in_back[i], in_back[(i + 1) % k], |
| 331 |
inside[(i + 1) % k], inside[i]); |
| 332 |
Patch *os = new Patch(g); |
| 333 |
for (int i = 0; i < k; ++i) |
| 334 |
os->addQuad(out_back[(i + 1) % k], out_back[i], |
| 335 |
outside[i], outside[(i + 1) % k]); |
| 336 |
parts << front << back << is << os; |
| 337 |
} |
| 338 |
|
| 339 |
QtLogo::QtLogo(QObject *parent, int divisions, qreal scale) |
| 340 |
: QObject(parent) |
| 341 |
, geom(new Geometry()) |
| 342 |
{ |
| 343 |
buildGeometry(divisions, scale); |
| 344 |
} |
| 345 |
|
| 346 |
QtLogo::~QtLogo() |
| 347 |
{ |
| 348 |
qDeleteAll(parts); |
| 349 |
delete geom; |
| 350 |
} |
| 351 |
|
| 352 |
void QtLogo::setColor(QColor c) |
| 353 |
{ |
| 354 |
for (int i = 0; i < parts.count(); ++i) |
| 355 |
qSetColor(parts[i]->faceColor, c); |
| 356 |
} |
| 357 |
|
| 358 |
//! [3] |
| 359 |
void QtLogo::buildGeometry(int divisions, qreal scale) |
| 360 |
{ |
| 361 |
qreal cw = cross_width * scale; |
| 362 |
qreal bt = bar_thickness * scale; |
| 363 |
qreal ld = logo_depth * scale; |
| 364 |
qreal th = tee_height *scale; |
| 365 |
|
| 366 |
RectPrism cross(geom, cw, bt, ld); |
| 367 |
RectPrism stem(geom, bt, th, ld); |
| 368 |
|
| 369 |
QVector3D z(0.0, 0.0, 1.0); |
| 370 |
cross.rotate(45.0, z); |
| 371 |
stem.rotate(45.0, z); |
| 372 |
|
| 373 |
qreal stem_downshift = (th + bt) / 2.0; |
| 374 |
stem.translate(QVector3D(0.0, -stem_downshift, 0.0)); |
| 375 |
|
| 376 |
RectTorus body(geom, 0.20, 0.30, 0.1, divisions); |
| 377 |
|
| 378 |
parts << stem.parts << cross.parts << body.parts; |
| 379 |
|
| 380 |
geom->finalize(); |
| 381 |
} |
| 382 |
//! [3] |
| 383 |
|
| 384 |
//! [4] |
| 385 |
void QtLogo::draw(QOpenGLFunctions_1_1 *functions) const |
| 386 |
{ |
| 387 |
geom->loadArrays(functions); |
| 388 |
|
| 389 |
functions->glEnableClientState(GL_VERTEX_ARRAY); |
| 390 |
functions->glEnableClientState(GL_NORMAL_ARRAY); |
| 391 |
|
| 392 |
for (int i = 0; i < parts.count(); ++i) |
| 393 |
parts[i]->draw(functions); |
| 394 |
|
| 395 |
functions->glDisableClientState(GL_VERTEX_ARRAY); |
| 396 |
functions->glDisableClientState(GL_NORMAL_ARRAY); |
| 397 |
} |
| 398 |
//! [4] |