-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathGeometry.h
327 lines (277 loc) · 14.7 KB
/
Geometry.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// VQE
// Copyright(C) 2020 - Volkan Ilbeyli
//
// This program is free software : you can redistribute it and / or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <http://www.gnu.org/licenses/>.
//
// Contact: [email protected]
#pragma once
#include "Mesh.h"
#include <type_traits>
namespace GeometryGenerator
{
template<class TVertex, class TIndex = unsigned>
struct GeometryData
{
static_assert(std::is_same<TIndex, unsigned>() || std::is_same<TIndex, unsigned short>()); // ensure UINT32 or UINT16 indices
std::vector<TVertex> Vertices;
std::vector<TIndex> Indices;
};
template<class TVertex, class TIndex = unsigned>
constexpr GeometryData<TVertex, TIndex> Triangle(float size);
template<class TVertex, class TIndex = unsigned>
constexpr GeometryData<TVertex, TIndex> Quad(float scale);
template<class TVertex, class TIndex = unsigned>
constexpr GeometryData<TVertex, TIndex> FullScreenQuad();
template<class TVertex, class TIndex = unsigned>
constexpr GeometryData<TVertex, TIndex> Cube();
template<class TVertex, class TIndex = unsigned>
constexpr GeometryData<TVertex, TIndex> Sphere(float radius, unsigned ringCount, unsigned sliceCount, int numLODLevels = 1);
template<class TVertex, class TIndex = unsigned>
constexpr GeometryData<TVertex, TIndex> Grid(float width, float depth, unsigned tilingX, unsigned tilingY, int numLODLevels = 1);
template<class TVertex, class TIndex = unsigned>
constexpr GeometryData<TVertex, TIndex> Cylinder(float height, float topRadius, float bottomRadius, unsigned sliceCount, unsigned stackCount, int numLODLevels = 1);
template<class TVertex, class TIndex = unsigned>
constexpr GeometryData<TVertex, TIndex> Cone(float height, float radius, unsigned sliceCount, int numLODLevels = 1);
// --------------------------------------------------------------------------------------------------------------------------------------------
// TEMPLATE DEFINITIONS
// --------------------------------------------------------------------------------------------------------------------------------------------
template<size_t LEN> // Helper function for setting values on a C-style array using an initializer list
void SetFVec(float* p, const std::initializer_list<float>& values)
{
static_assert(LEN > 0 && LEN <= 4); // assume [vec1-vec4]
for (size_t i = 0; i < LEN; ++i)
p[i] = values.begin()[i];
}
// Bitangent
//
// ^
// | V1 (uv1)
// | ^
// | / \
// | / \
// | / \
// | / \
// | / \
// | / \
// | / \
// | / \
// | / \
// | V0 ___________________ V2
// | (uv0) (uv2)
// -----------------------------------------> Tangent
template<class TVertex, class TIndex>
constexpr GeometryData<TVertex, TIndex> Triangle(float size)
{
constexpr bool bHasTangents = std::is_same<TVertex, FVertexWithNormalAndTangent>();
constexpr bool bHasNormals = std::is_same<TVertex, FVertexWithNormal>() || std::is_same<TVertex, FVertexWithNormalAndTangent>();
constexpr bool bHasColor = std::is_same<TVertex, FVertexWithColor>() || std::is_same<TVertex, FVertexWithColorAndAlpha>();
constexpr bool bHasAlpha = std::is_same<TVertex, FVertexWithColorAndAlpha>();
constexpr size_t NUM_VERTS = 3;
GeometryData<TVertex, TIndex> data;
std::vector<TVertex>& v = data.Vertices;
v.resize(NUM_VERTS);
// indices
data.Indices = { 0u, 1u, 2u };
// position
SetFVec<3>(v[0].position, { -size, -size, 0.0f });
SetFVec<3>(v[1].position, { 0.0f, size, 0.0f });
SetFVec<3>(v[2].position, { size, -size, 0.0f });
// uv
SetFVec<2>(v[0].uv, { 0.0f, 1.0f });
SetFVec<2>(v[1].uv, { 0.5f, 0.0f });
SetFVec<2>(v[2].uv, { 1.0f, 1.0f });
// normals
if constexpr (bHasNormals)
{
constexpr std::initializer_list<float> NormalVec = { 0, 0, -1 };
SetFVec<3>(v[0].normal, NormalVec);
SetFVec<3>(v[1].normal, NormalVec);
SetFVec<3>(v[2].normal, NormalVec);
}
// color
if constexpr (bHasColor)
{
SetFVec<3>(v[0].color, { 1.0f, 0.0f, 0.0f });
SetFVec<3>(v[1].color, { 0.0f, 1.0f, 0.0f });
SetFVec<3>(v[2].color, { 0.0f, 0.0f, 1.0f });
if constexpr (bHasAlpha)
{
v[0].color[3] = 1.0f;
v[1].color[3] = 1.0f;
v[2].color[3] = 1.0f;
}
}
// tangent
if constexpr (bHasTangents)
{
// todo: CalculateTangents(vector<T>& vertices, const vector<unsigned> indices)
}
return data;
}
// ASCII Cube art from: http://www.lonniebest.com/ASCII/Art/?ID=2
//
// 0 _________________________ 1 0, 1, 2, 0, 2, 3, // Top
// / _____________________ /| 4, 5, 6, 4, 6, 7, // Front
// / / ___________________/ / | 8, 9, 10, 8, 10, 11, // Right
// / / /| | / / | 12, 13, 14, 12, 14, 15, // Left
// / / / | | / / . | 16, 17, 18, 16, 18, 19, // Back
// / / /| | | / / /| | 20, 22, 21, 20, 23, 22, // Bottom
// / / / | | | / / / | |
// / / / | | | / / /| | | +Y
// / /_/__________________/ / / | | | | +Z
// 4,3 /________________________/5/ | | | | /
// | ______________________8|2| | | | | /
// | | | | | |_________| | |__| | | |/______+X
// | | | | |___________| | |____| |
// | | | / / ___________| | |_ / /
// | | | / / / | | |/ / /
// | | | / / / | | | / /
// | | |/ / / | | |/ /
// | | | / / | | ' /
// | | |/_/_______________| | /
// | |____________________| | /
// |________________________|/6
// 7
//
// vertices - CW
template<class TVertex, class TIndex>
constexpr GeometryData<TVertex, TIndex> Cube()
{
constexpr bool bHasTangents = std::is_same<TVertex, FVertexWithNormalAndTangent>();
constexpr bool bHasNormals = std::is_same<TVertex, FVertexWithNormal>() || std::is_same<TVertex, FVertexWithNormalAndTangent>();
constexpr bool bHasColor = std::is_same<TVertex, FVertexWithColor>() || std::is_same<TVertex, FVertexWithColorAndAlpha>();
constexpr bool bHasAlpha = std::is_same<TVertex, FVertexWithColorAndAlpha>();
constexpr int NUM_VERTS = 24;
GeometryData<TVertex, TIndex> data;
std::vector<TVertex>& v = data.Vertices;
v.resize(NUM_VERTS);
// indices
data.Indices = {
0, 1, 2, 0, 2, 3, // Top
4, 5, 6, 4, 6, 7, // back
8, 9, 10, 8, 10, 11, // Right
12, 13, 14, 12, 14, 15, // Back
16, 17, 18, 16, 18, 19, // Left
20, 22, 21, 20, 23, 22, // Bottom
};
// uv
SetFVec<2>(v[0] .uv, { +0.0f, +0.0f }); SetFVec<2>(v[3] .uv, { +0.0f, +1.0f });
SetFVec<2>(v[1] .uv, { +1.0f, +0.0f }); SetFVec<2>(v[4] .uv, { +0.0f, +0.0f });
SetFVec<2>(v[2] .uv, { +1.0f, +1.0f }); SetFVec<2>(v[5] .uv, { +1.0f, +0.0f });
SetFVec<2>(v[6] .uv, { +1.0f, +1.0f }); SetFVec<2>(v[9] .uv, { +1.0f, +0.0f });
SetFVec<2>(v[7] .uv, { +0.0f, +1.0f }); SetFVec<2>(v[10].uv, { +1.0f, +1.0f });
SetFVec<2>(v[8] .uv, { +0.0f, +0.0f }); SetFVec<2>(v[11].uv, { +0.0f, +1.0f });
SetFVec<2>(v[12].uv, { +0.0f, +0.0f }); SetFVec<2>(v[15].uv, { +0.0f, +1.0f });
SetFVec<2>(v[13].uv, { +1.0f, +0.0f }); SetFVec<2>(v[16].uv, { +0.0f, +0.0f });
SetFVec<2>(v[14].uv, { +1.0f, +1.0f }); SetFVec<2>(v[17].uv, { +1.0f, +0.0f });
SetFVec<2>(v[18].uv, { +1.0f, +1.0f }); SetFVec<2>(v[21].uv, { +0.0f, +0.0f });
SetFVec<2>(v[19].uv, { +0.0f, +1.0f }); SetFVec<2>(v[22].uv, { +0.0f, +1.0f });
SetFVec<2>(v[20].uv, { +1.0f, +0.0f }); SetFVec<2>(v[23].uv, { +1.0f, +1.0f });
// positions / normals / tangents
/* TOP */ SetFVec<3>(v[0].position , { -1.0f, +1.0f, +1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[0].normal , { +0.0f, +1.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[0].tangent , { +1.0f, +0.0f, +0.0f });
SetFVec<3>(v[1].position , { +1.0f, +1.0f, +1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[1].normal , { +0.0f, +1.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[1].tangent , { +1.0f, +0.0f, +0.0f });
SetFVec<3>(v[2].position , { +1.0f, +1.0f, -1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[2].normal , { +0.0f, +1.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[2].tangent , { +1.0f, +0.0f, +0.0f });
SetFVec<3>(v[3].position , { -1.0f, +1.0f, -1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[3].normal , { +0.0f, +1.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[3].tangent , { +1.0f, +0.0f, +0.0f });
/* FRONT */ SetFVec<3>(v[4].position , { -1.0f, +1.0f, -1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[4].normal , { +0.0f, +0.0f, -1.0f });
if constexpr (bHasTangents) SetFVec<3>(v[4].tangent , { +1.0f, +0.0f, +0.0f });
SetFVec<3>(v[5].position , { +1.0f, +1.0f, -1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[5].normal , { +0.0f, +0.0f, -1.0f });
if constexpr (bHasTangents) SetFVec<3>(v[5].tangent , { +1.0f, +0.0f, +0.0f });
SetFVec<3>(v[6].position , { +1.0f, -1.0f, -1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[6].normal , { +0.0f, +0.0f, -1.0f });
if constexpr (bHasTangents) SetFVec<3>(v[6].tangent , { +1.0f, +0.0f, +0.0f });
SetFVec<3>(v[7].position , { -1.0f, -1.0f, -1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[7].normal , { +0.0f, +0.0f, -1.0f });
if constexpr (bHasTangents) SetFVec<3>(v[7].tangent , { +1.0f, +0.0f, +0.0f });
/* RIGHT */ SetFVec<3>(v[8].position , { +1.0f, +1.0f, -1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[8].normal , { +1.0f, +0.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[8].tangent , { +0.0f, +0.0f, +1.0f });
SetFVec<3>(v[9].position , { +1.0f, +1.0f, +1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[9].normal , { +1.0f, +0.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[9].tangent , { +0.0f, +0.0f, +1.0f });
SetFVec<3>(v[10].position, { +1.0f, -1.0f, +1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[10].normal , { +1.0f, +0.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[10].tangent , { +0.0f, +0.0f, +1.0f });
SetFVec<3>(v[11].position, { +1.0f, -1.0f, -1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[11].normal , { +1.0f, +0.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[11].tangent , { +0.0f, +0.0f, +1.0f });
/* BACK */ SetFVec<3>(v[12].position, { +1.0f, +1.0f, +1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[12].normal , { +0.0f, +0.0f, +1.0f });
if constexpr (bHasTangents) SetFVec<3>(v[12].tangent , { +1.0f, +0.0f, +0.0f });
SetFVec<3>(v[13].position, { -1.0f, +1.0f, +1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[13].normal , { +0.0f, +0.0f, +1.0f });
if constexpr (bHasTangents) SetFVec<3>(v[13].tangent , { +1.0f, +0.0f, +0.0f });
SetFVec<3>(v[14].position, { -1.0f, -1.0f, +1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[14].normal , { +0.0f, +0.0f, +1.0f });
if constexpr (bHasTangents) SetFVec<3>(v[14].tangent , { +1.0f, +0.0f, +0.0f });
SetFVec<3>(v[15].position, { +1.0f, -1.0f, +1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[15].normal , { +0.0f, +0.0f, +1.0f });
if constexpr (bHasTangents) SetFVec<3>(v[15].tangent , { +1.0f, +0.0f, +0.0f });
/* LEFT */ SetFVec<3>(v[16].position, { -1.0f, +1.0f, +1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[16].normal , { -1.0f, +0.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[16].tangent , { +0.0f, +0.0f, -1.0f });
SetFVec<3>(v[17].position, { -1.0f, +1.0f, -1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[17].normal , { -1.0f, +0.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[17].tangent , { +0.0f, +0.0f, -1.0f });
SetFVec<3>(v[18].position, { -1.0f, -1.0f, -1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[18].normal , { -1.0f, +0.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[18].tangent , { +0.0f, +0.0f, -1.0f });
SetFVec<3>(v[19].position, { -1.0f, -1.0f, +1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[19].normal , { -1.0f, +0.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[19].tangent , { +0.0f, +0.0f, -1.0f });
/* BOTTOM */ SetFVec<3>(v[20].position, { +1.0f, -1.0f, -1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[20].normal , { +0.0f, -1.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[20].tangent , { +1.0f, +0.0f, +0.0f });
SetFVec<3>(v[21].position, { -1.0f, -1.0f, -1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[21].normal , { +0.0f, -1.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[21].tangent , { +1.0f, +0.0f, +0.0f });
SetFVec<3>(v[22].position, { -1.0f, -1.0f, +1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[22].normal , { +0.0f, -1.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[22].tangent , { +1.0f, +0.0f, +0.0f });
SetFVec<3>(v[23].position, { +1.0f, -1.0f, +1.0f });
if constexpr (bHasNormals) SetFVec<3>(v[23].normal , { +0.0f, -1.0f, +0.0f });
if constexpr (bHasTangents) SetFVec<3>(v[23].tangent , { +1.0f, +0.0f, +0.0f });
if constexpr (bHasColor)
{
for (int i = 0; i < (int)data.Indices.size(); i += 3)
{
TIndex Indices[3] =
{
data.Indices[i + 0]
, data.Indices[i + 1]
, data.Indices[i + 2]
};
SetFVec<3>(v[Indices[0]].color, { 1.0f, 0.0f, 0.0f });
SetFVec<3>(v[Indices[1]].color, { 0.0f, 1.0f, 0.0f });
SetFVec<3>(v[Indices[2]].color, { 0.0f, 0.0f, 1.0f });
if constexpr (bHasAlpha)
{
v[Indices[0]].color[3] = 1.0f;
v[Indices[1]].color[3] = 1.0f;
v[Indices[2]].color[3] = 1.0f;
}
}
}
return data;
}
};