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 demonstration applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "roundedbox.h"
43 
44 //============================================================================//
45 //                                P3T2N3Vertex                                //
46 //============================================================================//
47 
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},
52 
53     {VertexDescription::Null, 0, 0, 0, 0},
54 };
55 
56 //============================================================================//
57 //                                GLRoundedBox                                //
58 //============================================================================//
59 
lerp(float a,float b,float t)60 float lerp(float a, float b, float t)
61 {
62     return a * (1.0f - t) + b * t;
63 }
64 
GLRoundedBox(float r,float scale,int n)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))
67 {
68     int vidx = 0, iidx = 0;
69     int vertexCountPerCorner = (n + 2) * (n + 3) / 2;
70 
71     P3T2N3Vertex *vp = m_vb.lock();
72     unsigned short *ip = m_ib.lock();
73 
74     if (!vp || !ip) {
75         qWarning("GLRoundedBox::GLRoundedBox: Failed to lock vertex buffer and/or index buffer.");
76         m_ib.unlock();
77         m_vb.unlock();
78         return;
79     }
80 
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;
89 
90         // Face polygons
91         if (winding) {
92             ip[iidx++] = vidx;
93             ip[iidx++] = vidx + offsX;
94             ip[iidx++] = vidx + offsY;
95 
96             ip[iidx++] = vidx + vertexCountPerCorner - n - 2;
97             ip[iidx++] = vidx + vertexCountPerCorner - n - 2 + offsY;
98             ip[iidx++] = vidx + vertexCountPerCorner - n - 2 + offsZ;
99 
100             ip[iidx++] = vidx + vertexCountPerCorner - 1;
101             ip[iidx++] = vidx + vertexCountPerCorner - 1 + offsZ;
102             ip[iidx++] = vidx + vertexCountPerCorner - 1 + offsX;
103         }
104 
105         for (int i = 0; i < n + 2; ++i) {
106 
107             // Edge polygons
108             if (winding && i < n + 1) {
109                 ip[iidx++] = vidx + i + 1;
110                 ip[iidx++] = vidx;
111                 ip[iidx++] = vidx + offsY + i + 1;
112                 ip[iidx++] = vidx + offsY;
113                 ip[iidx++] = vidx + offsY + i + 1;
114                 ip[iidx++] = vidx;
115 
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;
122 
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;
129             }
130 
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);
135 
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);
139 
140                 // Corner polygons
141                 if (i < n + 1) {
142                     ip[iidx++] = vidx;
143                     ip[iidx++] = vidx + i + 2 - winding;
144                     ip[iidx++] = vidx + i + 1 + winding;
145                 }
146                 if (i < n) {
147                     ip[iidx++] = vidx + i + 1 + winding;
148                     ip[iidx++] = vidx + i + 2 - winding;
149                     ip[iidx++] = vidx + 2 * i + 4;
150                 }
151 
152                 ++vidx;
153             }
154         }
155 
156     }
157 
158     m_ib.unlock();
159     m_vb.unlock();
160 }
161 
162