-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimitive_builder.cpp
319 lines (260 loc) · 9.78 KB
/
primitive_builder.cpp
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
#include "primitive_builder.h"
#include <graphics/mesh.h>
#include <system/platform.h>
#include <graphics/primitive.h>
#include <maths/math_utils.h>
#include <vector>
#include <math.h>
//
// PrimitiveBuilder
//
PrimitiveBuilder::PrimitiveBuilder(gef::Platform& platform) :
platform_(platform),
default_cube_mesh_(NULL),
default_sphere_mesh_(NULL)
{
Init();
}
//
// ~PrimitiveBuilder
//
PrimitiveBuilder::~PrimitiveBuilder()
{
CleanUp();
}
//
// Init
//
void PrimitiveBuilder::Init()
{
// create helper geometry
default_cube_mesh_ = CreateBoxMesh(gef::Vector4(0.5f, 0.5f, 0.5f));
default_sphere_mesh_ = CreateSphereMesh(0.5f, 20, 20);
// create materials for basic colours
red_material_.set_colour(0xff0000ff);
blue_material_.set_colour(0xffff0000);
green_material_.set_colour(0xff00ff00);
}
//
// CleanUp
//
void PrimitiveBuilder::CleanUp()
{
delete default_sphere_mesh_;
default_sphere_mesh_ = NULL;
delete default_cube_mesh_;
default_cube_mesh_ = NULL;
}
//
// CreateBoxMesh
//
gef::Mesh* PrimitiveBuilder::CreateBoxMesh(const gef::Vector4& half_size, gef::Vector4 centre, gef::Material** materials)
{
gef::Mesh* mesh = gef::Mesh::Create(platform_);
//
// vertices
//
// create vertices, 4 for each face so we have all vertices in a single vertex share the same normal
const int kNumVertices = 4 * 6;
gef::Mesh::Vertex vertices[kNumVertices] =
{
// front
{ centre.x() - half_size.x(), centre.y() + half_size.y(), centre.z() + half_size.z(), 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
{ centre.x() + half_size.x(), centre.y() + half_size.y(), centre.z() + half_size.z(), 0.0f, 0.0f, 1.0f, 1.0f, 0.0f },
{ centre.x() - half_size.x(), centre.y() - half_size.y(), centre.z() + half_size.z(), 0.0f, 0.0f, 1.0f, 0.0f, 1.0f },
{ centre.x() + half_size.x(), centre.y() - half_size.y(), centre.z() + half_size.z(), 0.0f, 0.0f, 1.0f, 1.0f, 1.0f },
// back
{ centre.x() + half_size.x(), centre.y() + half_size.y(), centre.z() - half_size.z(), 0.0f, 0.0f, -1.0f, 0.0f, 0.0f },
{ centre.x() - half_size.x(), centre.y() + half_size.y(), centre.z() - half_size.z(), 0.0f, 0.0f, -1.0f, 1.0f, 0.0f },
{ centre.x() + half_size.x(), centre.y() - half_size.y(), centre.z() - half_size.z(), 0.0f, 0.0f, -1.0f, 0.0f, 1.0f },
{ centre.x() - half_size.x(), centre.y() - half_size.y(), centre.z() - half_size.z(), 0.0f, 0.0f, -1.0f, 1.0f, 1.0f },
// left
{ centre.x() - half_size.x(), centre.y() + half_size.y(), centre.z() - half_size.z(), -1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
{ centre.x() - half_size.x(), centre.y() + half_size.y(), centre.z() + half_size.z(), -1.0f, 0.0f, 0.0f, 1.0f, 0.0f },
{ centre.x() - half_size.x(), centre.y() - half_size.y(), centre.z() - half_size.z(), -1.0f, 0.0f, 0.0f, 0.0f, 1.0f },
{ centre.x() - half_size.x(), centre.y() - half_size.y(), centre.z() + half_size.z(), -1.0f, 0.0f, 0.0f, 1.0f, 1.0f },
// right
{ centre.x() + half_size.x(), centre.y() + half_size.y(), centre.z() + half_size.z(), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
{ centre.x() + half_size.x(), centre.y() + half_size.y(), centre.z() - half_size.z(), 1.0f, 0.0f, 0.0f, 1.0f, 0.0f },
{ centre.x() + half_size.x(), centre.y() - half_size.y(), centre.z() + half_size.z(), 1.0f, 0.0f, 0.0f, 0.0f, 1.0f },
{ centre.x() + half_size.x(), centre.y() - half_size.y(), centre.z() - half_size.z(), 1.0f, 0.0f, 0.0f, 1.0f, 1.0f },
// top
{ centre.x() - half_size.x(), centre.y() + half_size.y(), centre.z() - half_size.z(), 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
{ centre.x() + half_size.x(), centre.y() + half_size.y(), centre.z() - half_size.z(), 0.0f, 1.0f, 0.0f, 1.0f, 0.0f },
{ centre.x() - half_size.x(), centre.y() + half_size.y(), centre.z() + half_size.z(), 0.0f, 1.0f, 0.0f, 0.0f, 1.0f },
{ centre.x() + half_size.x(), centre.y() + half_size.y(), centre.z() + half_size.z(), 0.0f, 1.0f, 0.0f, 1.0f, 1.0f },
// bottom
{ centre.x() - half_size.x(), centre.y() - half_size.y(), centre.z() + half_size.z(), 0.0f, -1.0f, 0.0f, 0.0f, 0.0f },
{ centre.x() + half_size.x(), centre.y() - half_size.y(), centre.z() + half_size.z(), 0.0f, -1.0f, 0.0f, 1.0f, 0.0f },
{ centre.x() - half_size.x(), centre.y() - half_size.y(), centre.z() - half_size.z(), 0.0f, -1.0f, 0.0f, 0.0f, 1.0f },
{ centre.x() + half_size.x(), centre.y() - half_size.y(), centre.z() - half_size.z(), 0.0f, -1.0f, 0.0f, 1.0f, 1.0f },
};
const int kNumIndices = 6 * 6;
Int32 indices[kNumIndices] =
{
// front
0, 1, 2,
1, 3, 2,
// back
4, 5, 6,
5, 7, 6,
// left
8, 9, 10,
9, 11, 10,
// right
12, 13, 14,
13, 15, 14,
// top
16, 17, 18,
17, 19, 18,
// bottom
20, 21, 22,
21, 23, 22
};
// create the vertex buffer for the box vertices
mesh->InitVertexBuffer(platform_, vertices, kNumVertices, sizeof(gef::Mesh::Vertex));
// create a primitive per face so we can alter the material per face
const int num_faces = 6;
mesh->AllocatePrimitives(num_faces);
for (int primitive_num = 0; primitive_num < num_faces; ++primitive_num)
{
gef::Primitive* primitive = mesh->GetPrimitive(primitive_num);
primitive->InitIndexBuffer(platform_, &indices[primitive_num*6], 6, sizeof(Int32));
primitive->set_type(gef::TRIANGLE_LIST);
// if materials pointer is valid then assume we have an array of Material pointers
// with a size greater than 6 (one material per face)
if(materials)
primitive->set_material(materials[primitive_num]);
}
// set the bounds
// axis aligned bounding box
gef::Aabb aabb(centre - half_size, centre + half_size);
mesh->set_aabb(aabb);
// bounding sphere
gef::Sphere sphere(aabb);
mesh->set_bounding_sphere(sphere);
return mesh;
}
//
// CalculateSphereSurfaceNormal
//
gef::Vector4 CalculateSphereSurfaceNormal(const float radius, const gef::Vector4& origin, const gef::Vector4& point)
{
return gef::Vector4(
(point.x() - origin.x()) / radius,
(point.y() - origin.y()) / radius,
(point.z() - origin.z()) / radius);
}
//
// CreateSphereMesh
//
// adapted from
// http://www.visualizationlibrary.org/documentation/_geometry_primitives_8cpp_source.html#l00284
gef::Mesh* PrimitiveBuilder::CreateSphereMesh(const float radius, const int phi, const int theta, gef::Vector4 origin, gef::Material* material)
{
gef::Mesh* mesh = gef::Mesh::Create(platform_);
const int kNumVertices = theta*phi + 2;
std::vector<gef::Mesh::Vertex> vertices;
vertices.resize(kNumVertices);
int vert_idx = 0;
gef::Vector4 position, normal(0.0f, 0.0f, 1.0f);
gef::Vector2 uv(0.0f, 0.0f);
position = origin + gef::Vector4(0.0f, radius, 0.0f);
normal = CalculateSphereSurfaceNormal(radius, origin, position);
vertices[vert_idx].px = position.x();
vertices[vert_idx].py = position.y();
vertices[vert_idx].pz = position.z();
vertices[vert_idx].nx = normal.x();
vertices[vert_idx].ny = normal.y();
vertices[vert_idx].nz = normal.z();
vertices[vert_idx].u = uv.x;
vertices[vert_idx].v = uv.y;
vert_idx++;
for (int i = 0; i < theta; ++i)
{
for (int j = 0; j < phi; ++j)
{
gef::Vector4 v(0.0f, radius, 0.0f);
gef::Matrix44 rotz, roty;
rotz.RotationZ(gef::DegToRad(180.0f / (theta + 1)*(i + 1)));
roty.RotationY(gef::DegToRad(360.0f / phi*j));
v = v.Transform(rotz);
v = v.Transform(roty);
position = origin + v;
normal = CalculateSphereSurfaceNormal(radius, origin, position);
vertices[vert_idx].px = position.x();
vertices[vert_idx].py = position.y();
vertices[vert_idx].pz = position.z();
vertices[vert_idx].nx = normal.x();
vertices[vert_idx].ny = normal.y();
vertices[vert_idx].nz = normal.z();
vertices[vert_idx].u = uv.x;
vertices[vert_idx].v = uv.y;
vert_idx++;
}
}
position = origin + gef::Vector4(0.0f, -radius, 0.0f);
normal = CalculateSphereSurfaceNormal(radius, origin, position);
vertices[vert_idx].px = position.x();
vertices[vert_idx].py = position.y();
vertices[vert_idx].pz = position.z();
vertices[vert_idx].nx = normal.x();
vertices[vert_idx].ny = normal.y();
vertices[vert_idx].nz = normal.z();
vertices[vert_idx].u = uv.x;
vertices[vert_idx].v = uv.y;
vert_idx++;
mesh->InitVertexBuffer(platform_, &vertices[0], kNumVertices, sizeof(gef::Mesh::Vertex));
mesh->AllocatePrimitives(2);
// side quads
std::vector<Int32> index_buffer;
index_buffer.resize((theta - 1)*phi * 6);
int idx = 0;
for (int i = 0; i<theta - 1; ++i)
{
for (int j = 0; j<phi; ++j)
{
// 2 triangles per quad
index_buffer.at(idx++) = 1 + phi*(i + 0) + (j + 1) % phi;
index_buffer.at(idx++) = 1 + phi*(i + 1) + (j + 1) % phi;
index_buffer.at(idx++) = 1 + phi*(i + 1) + (j + 0) % phi;
index_buffer.at(idx++) = 1 + phi*(i + 0) + (j + 0) % phi;
index_buffer.at(idx++) = 1 + phi*(i + 0) + (j + 1) % phi;
index_buffer.at(idx++) = 1 + phi*(i + 1) + (j + 0) % phi;
}
}
gef::Primitive* primitive;
// setup primitive for side quads
primitive = mesh->GetPrimitive(0);
primitive->set_type(gef::TRIANGLE_LIST);
primitive->set_material(material);
primitive->InitIndexBuffer(platform_, &index_buffer[0], (UInt32)index_buffer.size(), sizeof(Int32));
// top/bottom triangles
index_buffer.resize(phi * 3 + phi * 3);
idx = 0;
// top fan
for (int j = 0; j<phi; ++j)
{
index_buffer.at(idx++) = 1 + (j + 1) % phi;
index_buffer.at(idx++) = 1 + (j + 0) % phi;
index_buffer.at(idx++) = 0;
}
// bottom fan
for (int j = 0; j<phi; ++j)
{
index_buffer.at(idx++) = 1 + phi*(theta - 1) + (j + 0) % phi;
index_buffer.at(idx++) = 1 + phi*(theta - 1) + (j + 1) % phi;
index_buffer.at(idx++) = (int)kNumVertices - 1;
}
// setup primitive for top and bottom fans
primitive = mesh->GetPrimitive(1);
primitive->set_type(gef::TRIANGLE_LIST);
primitive->set_material(material);
primitive->InitIndexBuffer(platform_, &index_buffer[0], (UInt32)index_buffer.size(), sizeof(Int32));
// bounds
gef::Aabb aabb(gef::Vector4(-radius, -radius, -radius) - origin, gef::Vector4(radius, radius, radius)+ origin);
mesh->set_aabb(aabb);
gef::Sphere sphere(origin, radius);
mesh->set_bounding_sphere(sphere);
return mesh;
}