/
vertexbuilder.h
290 lines (251 loc) · 11.6 KB
/
vertexbuilder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/** @file vertexbuilder.h Utility for composing triangle strips.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef LIBGUI_VERTEXBUILDER_H
#define LIBGUI_VERTEXBUILDER_H
#include <QVector>
#include <de/Vector>
#include <de/Rectangle>
namespace de {
/**
* Utility for composing simple geometric constructs (using triangle strips).
*/
template <typename VertexType>
struct VertexBuilder
{
struct Vertices : public QVector<VertexType> {
Vertices &operator += (Vertices const &other) {
concatenate(other, *this);
return *this;
}
Vertices operator + (Vertices const &other) const {
Vertices v(*this);
return v += other;
}
Vertices &makeQuad(Rectanglef const &rect, Vector4f const &color, Vector2f const &uv) {
Vertices quad;
VertexType v;
v.rgba = color;
v.texCoord = uv;
v.pos = rect.topLeft; quad << v;
v.pos = rect.topRight(); quad << v;
v.pos = rect.bottomLeft(); quad << v;
v.pos = rect.bottomRight; quad << v;
return *this += quad;
}
Vertices &makeQuad(Rectanglef const &rect, Rectanglef const &uv) {
Vertices quad;
VertexType v;
v.pos = rect.topLeft; v.texCoord = uv.topLeft; quad << v;
v.pos = rect.topRight(); v.texCoord = uv.topRight(); quad << v;
v.pos = rect.bottomLeft(); v.texCoord = uv.bottomLeft(); quad << v;
v.pos = rect.bottomRight; v.texCoord = uv.bottomRight; quad << v;
return *this += quad;
}
Vertices &makeQuad(Rectanglef const &rect, Vector4f const &color, Rectanglef const &uv) {
Vertices quad;
VertexType v;
v.rgba = color;
v.pos = rect.topLeft; v.texCoord = uv.topLeft; quad << v;
v.pos = rect.topRight(); v.texCoord = uv.topRight(); quad << v;
v.pos = rect.bottomLeft(); v.texCoord = uv.bottomLeft(); quad << v;
v.pos = rect.bottomRight; v.texCoord = uv.bottomRight; quad << v;
return *this += quad;
}
/// Makes a 3D quad with indirect UV coords. The points p1...p4 are specified
/// with a clockwise winding.
Vertices &makeQuadIndirect(Vector3f const &p1, Vector3f const &p2,
Vector3f const &p3, Vector3f const &p4,
Vector4f const &color, Rectanglef const &uv,
Vector4f const &uvBounds) {
Vertices quad;
VertexType v;
v.rgba = color;
v.texBounds = uvBounds;
v.pos = p1; v.texCoord = uv.topLeft; quad << v;
v.pos = p2; v.texCoord = uv.topRight(); quad << v;
v.pos = p4; v.texCoord = uv.bottomLeft(); quad << v;
v.pos = p3; v.texCoord = uv.bottomRight; quad << v;
return *this += quad;
}
Vertices &makeRing(Vector2f const ¢er, float outerRadius, float innerRadius,
int divisions, Vector4f const &color, Rectanglef const &uv,
float innerTexRadius = -1) {
if(innerTexRadius < 0) innerTexRadius = innerRadius / outerRadius;
Vertices ring;
VertexType v;
v.rgba = color;
for(int i = 0; i <= divisions; ++i) {
float const ang = 2 * PI * i / divisions;
Vector2f r(cos(ang), sin(ang));
// Outer.
v.pos = center + r * outerRadius;
v.texCoord = uv.middle() + r * .5f * uv.size();
ring << v;
// Inner.
v.pos = center + r * innerRadius;
v.texCoord = uv.middle() + r * (.5f * innerTexRadius) * uv.size();
ring << v;
}
return *this += ring;
}
Vertices &makeCubeIndirect(Vector3f const &minPoint,
Vector3f const &maxPoint,
Rectanglef const &uv,
Vector4f const &uvBounds,
Vector4f const faceColors[6]) {
// Back.
makeQuadIndirect(minPoint,
Vector3f(maxPoint.x, minPoint.y, minPoint.z),
Vector3f(maxPoint.x, maxPoint.y, minPoint.z),
Vector3f(minPoint.x, maxPoint.y, minPoint.z),
faceColors[0], uv, uvBounds);
// Front.
makeQuadIndirect(Vector3f(minPoint.x, minPoint.y, maxPoint.z),
Vector3f(maxPoint.x, minPoint.y, maxPoint.z),
maxPoint,
Vector3f(minPoint.x, maxPoint.y, maxPoint.z),
faceColors[1], uv, uvBounds);
// Left.
makeQuadIndirect(Vector3f(minPoint.x, minPoint.y, maxPoint.z),
minPoint,
Vector3f(minPoint.x, maxPoint.y, minPoint.z),
Vector3f(minPoint.x, maxPoint.y, maxPoint.z),
faceColors[2], uv, uvBounds);
// Right.
makeQuadIndirect(Vector3f(maxPoint.x, minPoint.y, minPoint.z),
Vector3f(maxPoint.x, minPoint.y, maxPoint.z),
maxPoint,
Vector3f(maxPoint.x, maxPoint.y, minPoint.z),
faceColors[3], uv, uvBounds);
// Floor.
makeQuadIndirect(Vector3f(minPoint.x, maxPoint.y, minPoint.z),
Vector3f(maxPoint.x, maxPoint.y, minPoint.z),
maxPoint,
Vector3f(minPoint.x, maxPoint.y, maxPoint.z),
faceColors[4], uv, uvBounds);
// Ceiling.
makeQuadIndirect(Vector3f(minPoint.x, minPoint.y, maxPoint.z),
Vector3f(maxPoint.x, minPoint.y, maxPoint.z),
Vector3f(maxPoint.x, minPoint.y, minPoint.z),
minPoint,
faceColors[5], uv, uvBounds);
return *this;
}
Vertices &makeRing(Vector2f const ¢er, float outerRadius, float innerRadius,
int divisions, Vector4f const &color, Vector2f const &uv) {
return makeRing(center, outerRadius, innerRadius, divisions, color, Rectanglef(uv, uv));
}
Vertices &makeFlexibleFrame(Rectanglef const &rect, float cornerThickness,
Vector4f const &color, Rectanglef const &uv) {
Vector2f const uvOff = uv.size() / 2;
Vertices verts;
VertexType v;
v.rgba = color;
// Top left corner.
v.pos = rect.topLeft;
v.texCoord = uv.topLeft;
verts << v;
v.pos = rect.topLeft + Vector2f(0, cornerThickness);
v.texCoord = uv.topLeft + Vector2f(0, uvOff.y);
verts << v;
v.pos = rect.topLeft + Vector2f(cornerThickness, 0);
v.texCoord = uv.topLeft + Vector2f(uvOff.x, 0);
verts << v;
v.pos = rect.topLeft + Vector2f(cornerThickness, cornerThickness);
v.texCoord = uv.topLeft + uvOff;
verts << v;
// Top right corner.
v.pos = rect.topRight() + Vector2f(-cornerThickness, 0);
v.texCoord = uv.topRight() + Vector2f(-uvOff.x, 0);
verts << v;
v.pos = rect.topRight() + Vector2f(-cornerThickness, cornerThickness);
v.texCoord = uv.topRight() + Vector2f(-uvOff.x, uvOff.y);
verts << v;
v.pos = rect.topRight();
v.texCoord = uv.topRight();
verts << v;
v.pos = rect.topRight() + Vector2f(0, cornerThickness);
v.texCoord = uv.topRight() + Vector2f(0, uvOff.y);
verts << v;
// Discontinuity.
verts << v;
verts << v;
v.pos = rect.topRight() + Vector2f(-cornerThickness, cornerThickness);
v.texCoord = uv.topRight() + Vector2f(-uvOff.x, uvOff.y);
verts << v;
// Bottom right corner.
v.pos = rect.bottomRight + Vector2f(0, -cornerThickness);
v.texCoord = uv.bottomRight + Vector2f(0, -uvOff.y);
verts << v;
v.pos = rect.bottomRight + Vector2f(-cornerThickness, -cornerThickness);
v.texCoord = uv.bottomRight + Vector2f(-uvOff.x, -uvOff.y);
verts << v;
v.pos = rect.bottomRight;
v.texCoord = uv.bottomRight;
verts << v;
v.pos = rect.bottomRight + Vector2f(-cornerThickness, 0);
v.texCoord = uv.bottomRight + Vector2f(-uvOff.x, 0);
verts << v;
// Discontinuity.
verts << v;
verts << v;
v.pos = rect.bottomRight + Vector2f(-cornerThickness, -cornerThickness);
v.texCoord = uv.bottomRight + Vector2f(-uvOff.x, -uvOff.y);
verts << v;
// Bottom left corner.
v.pos = rect.bottomLeft() + Vector2f(cornerThickness, 0);
v.texCoord = uv.bottomLeft() + Vector2f(uvOff.x, 0);
verts << v;
v.pos = rect.bottomLeft() + Vector2f(cornerThickness, -cornerThickness);
v.texCoord = uv.bottomLeft() + Vector2f(uvOff.x, -uvOff.y);
verts << v;
v.pos = rect.bottomLeft();
v.texCoord = uv.bottomLeft();
verts << v;
v.pos = rect.bottomLeft() + Vector2f(0, -cornerThickness);
v.texCoord = uv.bottomLeft() + Vector2f(0, -uvOff.y);
verts << v;
// Discontinuity.
verts << v;
verts << v;
// Closing the loop.
v.pos = rect.bottomLeft() + Vector2f(cornerThickness, -cornerThickness);
v.texCoord = uv.bottomLeft() + Vector2f(uvOff.x, -uvOff.y);
verts << v;
v.pos = rect.topLeft + Vector2f(0, cornerThickness);
v.texCoord = uv.topLeft + Vector2f(0, uvOff.y);
verts << v;
v.pos = rect.topLeft + Vector2f(cornerThickness, cornerThickness);
v.texCoord = uv.topLeft + Vector2f(uvOff.x, uvOff.y);
verts << v;
return *this += verts;
}
};
static void concatenate(Vertices const &stripSequence, Vertices &destStrip)
{
if(!stripSequence.size()) return;
if(!destStrip.isEmpty())
{
destStrip << destStrip.back();
destStrip << stripSequence.front();
}
destStrip << stripSequence;
}
};
} // namespace de
#endif // LIBGUI_VERTEXBUILDER_H