source: trunk/examples/opengl/pbuffers/cube.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 8.9 KB
Line 
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 "cube.h"
42#include "glwidget.h"
43
44#include <QtGui/QImage>
45#include <QtCore/QPropertyAnimation>
46
47static const qreal FACE_SIZE = 0.4;
48
49static const qreal speeds[] = { 1.8f, 2.4f, 3.6f };
50static const qreal amplitudes[] = { 2.0f, 2.5f, 3.0f };
51
52static inline void qSetColor(float colorVec[], QColor c)
53{
54 colorVec[0] = c.redF();
55 colorVec[1] = c.greenF();
56 colorVec[2] = c.blueF();
57 colorVec[3] = c.alphaF();
58}
59
60int Geometry::append(const QVector3D &a, const QVector3D &n, const QVector2D &t)
61{
62 int v = vertices.count();
63 vertices.append(a);
64 normals.append(n);
65 texCoords.append(t);
66 faces.append(v);
67 colors.append(QVector4D(0.6f, 0.6f, 0.6f, 1.0f));
68 return v;
69}
70
71void Geometry::addQuad(const QVector3D &a, const QVector3D &b,
72 const QVector3D &c, const QVector3D &d,
73 const QVector<QVector2D> &tex)
74{
75 QVector3D norm = QVector3D::normal(a, b, c);
76 // append first triangle
77 int aref = append(a, norm, tex[0]);
78 append(b, norm, tex[1]);
79 int cref = append(c, norm, tex[2]);
80 // append second triangle
81 faces.append(aref);
82 faces.append(cref);
83 append(d, norm, tex[3]);
84}
85
86void Geometry::loadArrays() const
87{
88 glEnableClientState(GL_VERTEX_ARRAY);
89 glEnableClientState(GL_NORMAL_ARRAY);
90 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
91 glEnableClientState(GL_COLOR_ARRAY);
92 glVertexPointer(3, GL_FLOAT, 0, vertices.constData());
93 glNormalPointer(GL_FLOAT, 0, normals.constData());
94 glTexCoordPointer(2, GL_FLOAT, 0, texCoords.constData());
95 glColorPointer(4, GL_FLOAT, 0, colors.constData());
96}
97
98void Geometry::setColors(int start, GLfloat colorArray[4][4])
99{
100 int off = faces[start];
101 for (int i = 0; i < 4; ++i)
102 colors[i + off] = QVector4D(colorArray[i][0],
103 colorArray[i][1],
104 colorArray[i][2],