-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
entity.h
485 lines (394 loc) · 14.6 KB
/
entity.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#ifndef ENTITY_H
#define ENTITY_H
#include <glm/glm.hpp> //glm::mat4
#include <list> //std::list
#include <array> //std::array
#include <memory> //std::unique_ptr
class Transform
{
protected:
//Local space information
glm::vec3 m_pos = { 0.0f, 0.0f, 0.0f };
glm::vec3 m_eulerRot = { 0.0f, 0.0f, 0.0f }; //In degrees
glm::vec3 m_scale = { 1.0f, 1.0f, 1.0f };
//Global space information concatenate in matrix
glm::mat4 m_modelMatrix = glm::mat4(1.0f);
//Dirty flag
bool m_isDirty = true;
protected:
glm::mat4 getLocalModelMatrix()
{
const glm::mat4 transformX = glm::rotate(glm::mat4(1.0f), glm::radians(m_eulerRot.x), glm::vec3(1.0f, 0.0f, 0.0f));
const glm::mat4 transformY = glm::rotate(glm::mat4(1.0f), glm::radians(m_eulerRot.y), glm::vec3(0.0f, 1.0f, 0.0f));
const glm::mat4 transformZ = glm::rotate(glm::mat4(1.0f), glm::radians(m_eulerRot.z), glm::vec3(0.0f, 0.0f, 1.0f));
// Y * X * Z
const glm::mat4 rotationMatrix = transformY * transformX * transformZ;
// translation * rotation * scale (also know as TRS matrix)
return glm::translate(glm::mat4(1.0f), m_pos) * rotationMatrix * glm::scale(glm::mat4(1.0f), m_scale);
}
public:
void computeModelMatrix()
{
m_modelMatrix = getLocalModelMatrix();
m_isDirty = false;
}
void computeModelMatrix(const glm::mat4& parentGlobalModelMatrix)
{
m_modelMatrix = parentGlobalModelMatrix * getLocalModelMatrix();
m_isDirty = false;
}
void setLocalPosition(const glm::vec3& newPosition)
{
m_pos = newPosition;
m_isDirty = true;
}
void setLocalRotation(const glm::vec3& newRotation)
{
m_eulerRot = newRotation;
m_isDirty = true;
}
void setLocalScale(const glm::vec3& newScale)
{
m_scale = newScale;
m_isDirty = true;
}
const glm::vec3& getGlobalPosition() const
{
return m_modelMatrix[3];
}
const glm::vec3& getLocalPosition() const
{
return m_pos;
}
const glm::vec3& getLocalRotation() const
{
return m_eulerRot;
}
const glm::vec3& getLocalScale() const
{
return m_scale;
}
const glm::mat4& getModelMatrix() const
{
return m_modelMatrix;
}
glm::vec3 getRight() const
{
return m_modelMatrix[0];
}
glm::vec3 getUp() const
{
return m_modelMatrix[1];
}
glm::vec3 getBackward() const
{
return m_modelMatrix[2];
}
glm::vec3 getForward() const
{
return -m_modelMatrix[2];
}
glm::vec3 getGlobalScale() const
{
return { glm::length(getRight()), glm::length(getUp()), glm::length(getBackward()) };
}
bool isDirty() const
{
return m_isDirty;
}
};
struct Plane
{
glm::vec3 normal = { 0.f, 1.f, 0.f }; // unit vector
float distance = 0.f; // Distance with origin
Plane() = default;
Plane(const glm::vec3& p1, const glm::vec3& norm)
: normal(glm::normalize(norm)),
distance(glm::dot(normal, p1))
{}
float getSignedDistanceToPlane(const glm::vec3& point) const
{
return glm::dot(normal, point) - distance;
}
};
struct Frustum
{
Plane topFace;
Plane bottomFace;
Plane rightFace;
Plane leftFace;
Plane farFace;
Plane nearFace;
};
struct BoundingVolume
{
virtual bool isOnFrustum(const Frustum& camFrustum, const Transform& transform) const = 0;
virtual bool isOnOrForwardPlane(const Plane& plane) const = 0;
bool isOnFrustum(const Frustum& camFrustum) const
{
return (isOnOrForwardPlane(camFrustum.leftFace) &&
isOnOrForwardPlane(camFrustum.rightFace) &&
isOnOrForwardPlane(camFrustum.topFace) &&
isOnOrForwardPlane(camFrustum.bottomFace) &&
isOnOrForwardPlane(camFrustum.nearFace) &&
isOnOrForwardPlane(camFrustum.farFace));
};
};
struct Sphere : public BoundingVolume
{
glm::vec3 center{ 0.f, 0.f, 0.f };
float radius{ 0.f };
Sphere(const glm::vec3& inCenter, float inRadius)
: BoundingVolume{}, center{ inCenter }, radius{ inRadius }
{}
bool isOnOrForwardPlane(const Plane& plane) const final
{
return plane.getSignedDistanceToPlane(center) > -radius;
}
bool isOnFrustum(const Frustum& camFrustum, const Transform& transform) const final
{
//Get global scale thanks to our transform
const glm::vec3 globalScale = transform.getGlobalScale();
//Get our global center with process it with the global model matrix of our transform
const glm::vec3 globalCenter{ transform.getModelMatrix() * glm::vec4(center, 1.f) };
//To wrap correctly our shape, we need the maximum scale scalar.
const float maxScale = std::max(std::max(globalScale.x, globalScale.y), globalScale.z);
//Max scale is assuming for the diameter. So, we need the half to apply it to our radius
Sphere globalSphere(globalCenter, radius * (maxScale * 0.5f));
//Check Firstly the result that have the most chance to failure to avoid to call all functions.
return (globalSphere.isOnOrForwardPlane(camFrustum.leftFace) &&
globalSphere.isOnOrForwardPlane(camFrustum.rightFace) &&
globalSphere.isOnOrForwardPlane(camFrustum.farFace) &&
globalSphere.isOnOrForwardPlane(camFrustum.nearFace) &&
globalSphere.isOnOrForwardPlane(camFrustum.topFace) &&
globalSphere.isOnOrForwardPlane(camFrustum.bottomFace));
};
};
struct SquareAABB : public BoundingVolume
{
glm::vec3 center{ 0.f, 0.f, 0.f };
float extent{ 0.f };
SquareAABB(const glm::vec3& inCenter, float inExtent)
: BoundingVolume{}, center{ inCenter }, extent{ inExtent }
{}
bool isOnOrForwardPlane(const Plane& plane) const final
{
// Compute the projection interval radius of b onto L(t) = b.c + t * p.n
const float r = extent * (std::abs(plane.normal.x) + std::abs(plane.normal.y) + std::abs(plane.normal.z));
return -r <= plane.getSignedDistanceToPlane(center);
}
bool isOnFrustum(const Frustum& camFrustum, const Transform& transform) const final
{
//Get global scale thanks to our transform
const glm::vec3 globalCenter{ transform.getModelMatrix() * glm::vec4(center, 1.f) };
// Scaled orientation
const glm::vec3 right = transform.getRight() * extent;
const glm::vec3 up = transform.getUp() * extent;
const glm::vec3 forward = transform.getForward() * extent;
const float newIi = std::abs(glm::dot(glm::vec3{ 1.f, 0.f, 0.f }, right)) +
std::abs(glm::dot(glm::vec3{ 1.f, 0.f, 0.f }, up)) +
std::abs(glm::dot(glm::vec3{ 1.f, 0.f, 0.f }, forward));
const float newIj = std::abs(glm::dot(glm::vec3{ 0.f, 1.f, 0.f }, right)) +
std::abs(glm::dot(glm::vec3{ 0.f, 1.f, 0.f }, up)) +
std::abs(glm::dot(glm::vec3{ 0.f, 1.f, 0.f }, forward));
const float newIk = std::abs(glm::dot(glm::vec3{ 0.f, 0.f, 1.f }, right)) +
std::abs(glm::dot(glm::vec3{ 0.f, 0.f, 1.f }, up)) +
std::abs(glm::dot(glm::vec3{ 0.f, 0.f, 1.f }, forward));
const SquareAABB globalAABB(globalCenter, std::max(std::max(newIi, newIj), newIk));
return (globalAABB.isOnOrForwardPlane(camFrustum.leftFace) &&
globalAABB.isOnOrForwardPlane(camFrustum.rightFace) &&
globalAABB.isOnOrForwardPlane(camFrustum.topFace) &&
globalAABB.isOnOrForwardPlane(camFrustum.bottomFace) &&
globalAABB.isOnOrForwardPlane(camFrustum.nearFace) &&
globalAABB.isOnOrForwardPlane(camFrustum.farFace));
};
};
struct AABB : public BoundingVolume
{
glm::vec3 center{ 0.f, 0.f, 0.f };
glm::vec3 extents{ 0.f, 0.f, 0.f };
AABB(const glm::vec3& min, const glm::vec3& max)
: BoundingVolume{}, center{ (max + min) * 0.5f }, extents{ max.x - center.x, max.y - center.y, max.z - center.z }
{}
AABB(const glm::vec3& inCenter, float iI, float iJ, float iK)
: BoundingVolume{}, center{ inCenter }, extents{ iI, iJ, iK }
{}
std::array<glm::vec3, 8> getVertice() const
{
std::array<glm::vec3, 8> vertice;
vertice[0] = { center.x - extents.x, center.y - extents.y, center.z - extents.z };
vertice[1] = { center.x + extents.x, center.y - extents.y, center.z - extents.z };
vertice[2] = { center.x - extents.x, center.y + extents.y, center.z - extents.z };
vertice[3] = { center.x + extents.x, center.y + extents.y, center.z - extents.z };
vertice[4] = { center.x - extents.x, center.y - extents.y, center.z + extents.z };
vertice[5] = { center.x + extents.x, center.y - extents.y, center.z + extents.z };
vertice[6] = { center.x - extents.x, center.y + extents.y, center.z + extents.z };
vertice[7] = { center.x + extents.x, center.y + extents.y, center.z + extents.z };
return vertice;
}
//see https://gdbooks.gitbooks.io/3dcollisions/content/Chapter2/static_aabb_plane.html
bool isOnOrForwardPlane(const Plane& plane) const final
{
// Compute the projection interval radius of b onto L(t) = b.c + t * p.n
const float r = extents.x * std::abs(plane.normal.x) + extents.y * std::abs(plane.normal.y) +
extents.z * std::abs(plane.normal.z);
return -r <= plane.getSignedDistanceToPlane(center);
}
bool isOnFrustum(const Frustum& camFrustum, const Transform& transform) const final
{
//Get global scale thanks to our transform
const glm::vec3 globalCenter{ transform.getModelMatrix() * glm::vec4(center, 1.f) };
// Scaled orientation
const glm::vec3 right = transform.getRight() * extents.x;
const glm::vec3 up = transform.getUp() * extents.y;
const glm::vec3 forward = transform.getForward() * extents.z;
const float newIi = std::abs(glm::dot(glm::vec3{ 1.f, 0.f, 0.f }, right)) +
std::abs(glm::dot(glm::vec3{ 1.f, 0.f, 0.f }, up)) +
std::abs(glm::dot(glm::vec3{ 1.f, 0.f, 0.f }, forward));
const float newIj = std::abs(glm::dot(glm::vec3{ 0.f, 1.f, 0.f }, right)) +
std::abs(glm::dot(glm::vec3{ 0.f, 1.f, 0.f }, up)) +
std::abs(glm::dot(glm::vec3{ 0.f, 1.f, 0.f }, forward));
const float newIk = std::abs(glm::dot(glm::vec3{ 0.f, 0.f, 1.f }, right)) +
std::abs(glm::dot(glm::vec3{ 0.f, 0.f, 1.f }, up)) +
std::abs(glm::dot(glm::vec3{ 0.f, 0.f, 1.f }, forward));
const AABB globalAABB(globalCenter, newIi, newIj, newIk);
return (globalAABB.isOnOrForwardPlane(camFrustum.leftFace) &&
globalAABB.isOnOrForwardPlane(camFrustum.rightFace) &&
globalAABB.isOnOrForwardPlane(camFrustum.topFace) &&
globalAABB.isOnOrForwardPlane(camFrustum.bottomFace) &&
globalAABB.isOnOrForwardPlane(camFrustum.nearFace) &&
globalAABB.isOnOrForwardPlane(camFrustum.farFace));
};
};
Frustum createFrustumFromCamera(const Camera& cam, float aspect, float fovY, float zNear, float zFar)
{
Frustum frustum;
const float halfVSide = zFar * tanf(fovY * .5f);
const float halfHSide = halfVSide * aspect;
const glm::vec3 frontMultFar = zFar * cam.Front;
frustum.nearFace = { cam.Position + zNear * cam.Front, cam.Front };
frustum.farFace = { cam.Position + frontMultFar, -cam.Front };
frustum.rightFace = { cam.Position, glm::cross(frontMultFar - cam.Right * halfHSide, cam.Up) };
frustum.leftFace = { cam.Position, glm::cross(cam.Up, frontMultFar + cam.Right * halfHSide) };
frustum.topFace = { cam.Position, glm::cross(cam.Right, frontMultFar - cam.Up * halfVSide) };
frustum.bottomFace = { cam.Position, glm::cross(frontMultFar + cam.Up * halfVSide, cam.Right) };
return frustum;
}
AABB generateAABB(const Model& model)
{
glm::vec3 minAABB = glm::vec3(std::numeric_limits<float>::max());
glm::vec3 maxAABB = glm::vec3(std::numeric_limits<float>::min());
for (auto&& mesh : model.meshes)
{
for (auto&& vertex : mesh.vertices)
{
minAABB.x = std::min(minAABB.x, vertex.Position.x);
minAABB.y = std::min(minAABB.y, vertex.Position.y);
minAABB.z = std::min(minAABB.z, vertex.Position.z);
maxAABB.x = std::max(maxAABB.x, vertex.Position.x);
maxAABB.y = std::max(maxAABB.y, vertex.Position.y);
maxAABB.z = std::max(maxAABB.z, vertex.Position.z);
}
}
return AABB(minAABB, maxAABB);
}
Sphere generateSphereBV(const Model& model)
{
glm::vec3 minAABB = glm::vec3(std::numeric_limits<float>::max());
glm::vec3 maxAABB = glm::vec3(std::numeric_limits<float>::min());
for (auto&& mesh : model.meshes)
{
for (auto&& vertex : mesh.vertices)
{
minAABB.x = std::min(minAABB.x, vertex.Position.x);
minAABB.y = std::min(minAABB.y, vertex.Position.y);
minAABB.z = std::min(minAABB.z, vertex.Position.z);
maxAABB.x = std::max(maxAABB.x, vertex.Position.x);
maxAABB.y = std::max(maxAABB.y, vertex.Position.y);
maxAABB.z = std::max(maxAABB.z, vertex.Position.z);
}
}
return Sphere((maxAABB + minAABB) * 0.5f, glm::length(minAABB - maxAABB));
}
class Entity
{
public:
//Scene graph
std::list<std::unique_ptr<Entity>> children;
Entity* parent = nullptr;
//Space information
Transform transform;
Model* pModel = nullptr;
std::unique_ptr<AABB> boundingVolume;
// constructor, expects a filepath to a 3D model.
Entity(Model& model) : pModel{ &model }
{
boundingVolume = std::make_unique<AABB>(generateAABB(model));
//boundingVolume = std::make_unique<Sphere>(generateSphereBV(model));
}
AABB getGlobalAABB()
{
//Get global scale thanks to our transform
const glm::vec3 globalCenter{ transform.getModelMatrix() * glm::vec4(boundingVolume->center, 1.f) };
// Scaled orientation
const glm::vec3 right = transform.getRight() * boundingVolume->extents.x;
const glm::vec3 up = transform.getUp() * boundingVolume->extents.y;
const glm::vec3 forward = transform.getForward() * boundingVolume->extents.z;
const float newIi = std::abs(glm::dot(glm::vec3{ 1.f, 0.f, 0.f }, right)) +
std::abs(glm::dot(glm::vec3{ 1.f, 0.f, 0.f }, up)) +
std::abs(glm::dot(glm::vec3{ 1.f, 0.f, 0.f }, forward));
const float newIj = std::abs(glm::dot(glm::vec3{ 0.f, 1.f, 0.f }, right)) +
std::abs(glm::dot(glm::vec3{ 0.f, 1.f, 0.f }, up)) +
std::abs(glm::dot(glm::vec3{ 0.f, 1.f, 0.f }, forward));
const float newIk = std::abs(glm::dot(glm::vec3{ 0.f, 0.f, 1.f }, right)) +
std::abs(glm::dot(glm::vec3{ 0.f, 0.f, 1.f }, up)) +
std::abs(glm::dot(glm::vec3{ 0.f, 0.f, 1.f }, forward));
return AABB(globalCenter, newIi, newIj, newIk);
}
//Add child. Argument input is argument of any constructor that you create. By default you can use the default constructor and don't put argument input.
template<typename... TArgs>
void addChild(TArgs&... args)
{
children.emplace_back(std::make_unique<Entity>(args...));
children.back()->parent = this;
}
//Update transform if it was changed
void updateSelfAndChild()
{
if (transform.isDirty()) {
forceUpdateSelfAndChild();
return;
}
for (auto&& child : children)
{
child->updateSelfAndChild();
}
}
//Force update of transform even if local space don't change
void forceUpdateSelfAndChild()
{
if (parent)
transform.computeModelMatrix(parent->transform.getModelMatrix());
else
transform.computeModelMatrix();
for (auto&& child : children)
{
child->forceUpdateSelfAndChild();
}
}
void drawSelfAndChild(const Frustum& frustum, Shader& ourShader, unsigned int& display, unsigned int& total)
{
if (boundingVolume->isOnFrustum(frustum, transform))
{
ourShader.setMat4("model", transform.getModelMatrix());
pModel->Draw(ourShader);
display++;
}
total++;
for (auto&& child : children)
{
child->drawSelfAndChild(frustum, ourShader, display, total);
}
}
};
#endif