1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the demonstration applications of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "roundedbox.h"
44 //============================================================================//
46 //============================================================================//
48 VertexDescription
P3T2N3Vertex::description
[] = {
49 {VertexDescription::Position
, GL_FLOAT
, SIZE_OF_MEMBER(P3T2N3Vertex
, position
) / sizeof(float), 0, 0},
50 {VertexDescription::TexCoord
, GL_FLOAT
, SIZE_OF_MEMBER(P3T2N3Vertex
, texCoord
) / sizeof(float), sizeof(QVector3D
), 0},
51 {VertexDescription::Normal
, GL_FLOAT
, SIZE_OF_MEMBER(P3T2N3Vertex
, normal
) / sizeof(float), sizeof(QVector3D
) + sizeof(QVector2D
), 0},
53 {VertexDescription::Null
, 0, 0, 0, 0},
56 //============================================================================//
58 //============================================================================//
60 float lerp(float a
, float b
, float t
)
62 return a
* (1.0f
- t
) + b
* t
;
65 GLRoundedBox::GLRoundedBox(float r
, float scale
, int n
)
66 : GLTriangleMesh
<P3T2N3Vertex
, unsigned short>((n
+2)*(n
+3)*4, (n
+1)*(n
+1)*24+36+72*(n
+1))
68 int vidx
= 0, iidx
= 0;
69 int vertexCountPerCorner
= (n
+ 2) * (n
+ 3) / 2;
71 P3T2N3Vertex
*vp
= m_vb
.lock();
72 unsigned short *ip
= m_ib
.lock();
75 qWarning("GLRoundedBox::GLRoundedBox: Failed to lock vertex buffer and/or index buffer.");
81 for (int corner
= 0; corner
< 8; ++corner
) {
82 QVector3D
centre(corner
& 1 ? 1.0f
: -1.0f
,
83 corner
& 2 ? 1.0f
: -1.0f
,
84 corner
& 4 ? 1.0f
: -1.0f
);
85 int winding
= (corner
& 1) ^ ((corner
>> 1) & 1) ^ (corner
>> 2);
86 int offsX
= ((corner
^ 1) - corner
) * vertexCountPerCorner
;
87 int offsY
= ((corner
^ 2) - corner
) * vertexCountPerCorner
;
88 int offsZ
= ((corner
^ 4) - corner
) * vertexCountPerCorner
;
93 ip
[iidx
++] = vidx
+ offsX
;
94 ip
[iidx
++] = vidx
+ offsY
;
96 ip
[iidx
++] = vidx
+ vertexCountPerCorner
- n
- 2;
97 ip
[iidx
++] = vidx
+ vertexCountPerCorner
- n
- 2 + offsY
;
98 ip
[iidx
++] = vidx
+ vertexCountPerCorner
- n
- 2 + offsZ
;
100 ip
[iidx
++] = vidx
+ vertexCountPerCorner
- 1;
101 ip
[iidx
++] = vidx
+ vertexCountPerCorner
- 1 + offsZ
;
102 ip
[iidx
++] = vidx
+ vertexCountPerCorner
- 1 + offsX
;
105 for (int i
= 0; i
< n
+ 2; ++i
) {
108 if (winding
&& i
< n
+ 1) {
109 ip
[iidx
++] = vidx
+ i
+ 1;
111 ip
[iidx
++] = vidx
+ offsY
+ i
+ 1;
112 ip
[iidx
++] = vidx
+ offsY
;
113 ip
[iidx
++] = vidx
+ offsY
+ i
+ 1;
116 ip
[iidx
++] = vidx
+ i
;
117 ip
[iidx
++] = vidx
+ 2 * i
+ 2;
118 ip
[iidx
++] = vidx
+ i
+ offsX
;
119 ip
[iidx
++] = vidx
+ 2 * i
+ offsX
+ 2;
120 ip
[iidx
++] = vidx
+ i
+ offsX
;
121 ip
[iidx
++] = vidx
+ 2 * i
+ 2;
123 ip
[iidx
++] = (corner
+ 1) * vertexCountPerCorner
- 1 - i
;
124 ip
[iidx
++] = (corner
+ 1) * vertexCountPerCorner
- 2 - i
;
125 ip
[iidx
++] = (corner
+ 1) * vertexCountPerCorner
- 1 - i
+ offsZ
;
126 ip
[iidx
++] = (corner
+ 1) * vertexCountPerCorner
- 2 - i
+ offsZ
;
127 ip
[iidx
++] = (corner
+ 1) * vertexCountPerCorner
- 1 - i
+ offsZ
;
128 ip
[iidx
++] = (corner
+ 1) * vertexCountPerCorner
- 2 - i
;
131 for (int j
= 0; j
<= i
; ++j
) {
132 QVector3D normal
= QVector3D(i
- j
, j
, n
+ 1 - i
).normalized();
133 QVector3D
offset(0.5f
- r
, 0.5f
- r
, 0.5f
- r
);
134 QVector3D pos
= centre
* (offset
+ r
* normal
);
136 vp
[vidx
].position
= scale
* pos
;
137 vp
[vidx
].normal
= centre
* normal
;
138 vp
[vidx
].texCoord
= QVector2D(pos
.x() + 0.5f
, pos
.y() + 0.5f
);
143 ip
[iidx
++] = vidx
+ i
+ 2 - winding
;
144 ip
[iidx
++] = vidx
+ i
+ 1 + winding
;
147 ip
[iidx
++] = vidx
+ i
+ 1 + winding
;
148 ip
[iidx
++] = vidx
+ i
+ 2 - winding
;
149 ip
[iidx
++] = vidx
+ 2 * i
+ 4;