-
Notifications
You must be signed in to change notification settings - Fork 3
/
Frustum.cpp
67 lines (54 loc) · 2.34 KB
/
Frustum.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
#include "Frustum.h"
//https://github.com/Hopson97/MineCraft-One-Week-Challenge/blob/a17cb344ebd61f0349f375a247d559e607d0dc06/Source/Maths/Frustum.cpp
void Open7Days::Frustum::Update(Matrix4f Matrix) {
Planes[FrustumPlanes::Left].Normal.x = Matrix[0][3] + Matrix[0][0];
Planes[FrustumPlanes::Left].Normal.y = Matrix[1][3] + Matrix[1][0];
Planes[FrustumPlanes::Left].Normal.z = Matrix[2][3] + Matrix[2][0];
Planes[FrustumPlanes::Left].DistToOrig = Matrix[3][3] + Matrix[3][0];
// right
Planes[FrustumPlanes::Right].Normal.x = Matrix[0][3] - Matrix[0][0];
Planes[FrustumPlanes::Right].Normal.y = Matrix[1][3] - Matrix[1][0];
Planes[FrustumPlanes::Right].Normal.z = Matrix[2][3] - Matrix[2][0];
Planes[FrustumPlanes::Right].DistToOrig = Matrix[3][3] - Matrix[3][0];
// bottom
Planes[FrustumPlanes::Bottom].Normal.x = Matrix[0][3] + Matrix[0][1];
Planes[FrustumPlanes::Bottom].Normal.y = Matrix[1][3] + Matrix[1][1];
Planes[FrustumPlanes::Bottom].Normal.z = Matrix[2][3] + Matrix[2][1];
Planes[FrustumPlanes::Bottom].DistToOrig = Matrix[3][3] + Matrix[3][1];
// top
Planes[FrustumPlanes::Top].Normal.x = Matrix[0][3] - Matrix[0][1];
Planes[FrustumPlanes::Top].Normal.y = Matrix[1][3] - Matrix[1][1];
Planes[FrustumPlanes::Top].Normal.z = Matrix[2][3] - Matrix[2][1];
Planes[FrustumPlanes::Top].DistToOrig = Matrix[3][3] - Matrix[3][1];
// near
Planes[FrustumPlanes::Near].Normal.x = Matrix[0][3] + Matrix[0][2];
Planes[FrustumPlanes::Near].Normal.y = Matrix[1][3] + Matrix[1][2];
Planes[FrustumPlanes::Near].Normal.z = Matrix[2][3] + Matrix[2][2];
Planes[FrustumPlanes::Near].DistToOrig = Matrix[3][3] + Matrix[3][2];
// far
Planes[FrustumPlanes::Far].Normal.x = Matrix[0][3] - Matrix[0][2];
Planes[FrustumPlanes::Far].Normal.y = Matrix[1][3] - Matrix[1][2];
Planes[FrustumPlanes::Far].Normal.z = Matrix[2][3] - Matrix[2][2];
Planes[FrustumPlanes::Far].DistToOrig = Matrix[3][3] - Matrix[3][2];
for (auto& plane : Planes)
{
float length = glm::length(plane.Normal);
plane.Normal /= length;
plane.DistToOrig /= length;
}
}
bool Open7Days::Frustum::InFrustum(FrustumAABB Box) {
bool result = true;
for (auto& plane : Planes)
{
if (plane.DistanceToPoint(Box.GetMaxPointFromNormal(plane.Normal)) < 0)
{
return false;
}
else if (plane.DistanceToPoint(Box.GetMinPointFromNormal(plane.Normal)) < 0)
{
result = true;
}
}
return result;
}