-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrustum.cpp
139 lines (114 loc) · 3.8 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
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
#include "Frustum.h"
Frustum::Frustum(void)
{
}
Frustum::~Frustum(void)
{
}
void Frustum::setFrustum(glm::mat4 &mat)
{
// Extracting with the Gribb/Hartman method: http://www.cs.otago.ac.nz/postgrads/alexis/planeExtraction.pdf
if(!&mat)
return;
//left
planes[0].a = mat[3][0] + mat[0][0];
planes[0].b = mat[3][1] + mat[0][1];
planes[0].c = mat[3][2] + mat[0][2];
planes[0].d = mat[3][3] + mat[0][3];
//right
planes[1].a = mat[3][0] - mat[0][0];
planes[1].b = mat[3][1] - mat[0][1];
planes[1].c = mat[3][2] - mat[0][2];
planes[1].d = mat[3][3] - mat[0][3];
//bottom
planes[2].a = mat[3][0] + mat[2][0];
planes[2].b = mat[3][1] + mat[2][1];
planes[2].c = mat[3][2] + mat[2][2];
planes[2].d = mat[3][3] + mat[2][3];
//top
planes[3].a = mat[3][0] - mat[2][0];
planes[3].b = mat[3][1] - mat[2][1];
planes[3].c = mat[3][2] - mat[2][2];
planes[3].d = mat[3][3] - mat[2][3];
//near
planes[4].a = mat[3][0] + mat[2][0];
planes[4].b = mat[3][1] + mat[2][1];
planes[4].c = mat[3][2] + mat[2][2];
planes[4].d = mat[3][3] + mat[2][3];
//far
planes[5].a = mat[3][0] - mat[0][0];
planes[5].b = mat[3][1] - mat[0][1];
planes[5].c = mat[3][2] - mat[0][2];
planes[5].d = mat[3][3] - mat[0][3];
/*//left
planes[0].a = mat[4][1] + mat[1][1];
planes[0].b = mat[4][2] + mat[1][2];
planes[0].c = mat[4][3] + mat[1][3];
planes[0].d = mat[4][4] + mat[1][4];
//right
planes[1].a = mat[4][1] - mat[1][1];
planes[1].b = mat[4][2] - mat[1][2];
planes[1].c = mat[4][3] - mat[1][3];
planes[1].d = mat[4][4] - mat[1][4];
//bottom
planes[2].a = mat[4][1] + mat[2][1];
planes[2].b = mat[4][2] + mat[2][2];
planes[2].c = mat[4][3] + mat[2][3];
planes[2].d = mat[4][4] + mat[2][4];
//top
planes[3].a = mat[4][1] - mat[2][1];
planes[3].b = mat[4][2] - mat[2][2];
planes[3].c = mat[4][3] - mat[2][3];
planes[3].d = mat[4][4] - mat[2][4];
//near
planes[4].a = mat[4][1] + mat[3][1];
planes[4].b = mat[4][2] + mat[3][2];
planes[4].c = mat[4][3] + mat[3][3];
planes[4].d = mat[4][4] + mat[3][4];
//far
planes[5].a = mat[4][1] - mat[1][1];
planes[5].b = mat[4][2] - mat[1][2];
planes[5].c = mat[4][3] - mat[1][3];
planes[5].d = mat[4][4] - mat[1][4];*/
}
bool Frustum::cubeInFrustum(glm::vec3 ¢er, float size)
{
// Add case for when the cube is larger than the frustum
glm::vec3 p1, p2, p3, p4, p5, p6, p7, p8;
bool hasPointIn = false;
p1 = glm::vec3(center.x + size, center.y + size, center.z + size);
p2 = glm::vec3(center.x - size, center.y + size, center.z + size);
p3 = glm::vec3(center.x + size, center.y - size, center.z + size);
p4 = glm::vec3(center.x - size, center.y - size, center.z + size);
p5 = glm::vec3(center.x + size, center.y + size, center.z - size);
p6 = glm::vec3(center.x - size, center.y + size, center.z - size);
p7 = glm::vec3(center.x + size, center.y - size, center.z - size);
p8 = glm::vec3(center.x - size, center.y - size, center.z - size);
hasPointIn &= pointInFrustum(p1);
hasPointIn &= pointInFrustum(p2);
hasPointIn &= pointInFrustum(p3);
hasPointIn &= pointInFrustum(p4);
hasPointIn &= pointInFrustum(p5);
hasPointIn &= pointInFrustum(p6);
hasPointIn &= pointInFrustum(p7);
hasPointIn &= pointInFrustum(p8);
return hasPointIn;
}
bool Frustum::pointInFrustum(glm::vec3 &point)
{
return planes[0].distanceTo(point) > 0 &&
planes[1].distanceTo(point) > 0 &&
planes[2].distanceTo(point) > 0 &&
planes[3].distanceTo(point) > 0 &&
planes[4].distanceTo(point) > 0 &&
planes[5].distanceTo(point) > 0;
}
bool Frustum::sphereInFrustum(glm::vec3 ¢er, float radius)
{
return planes[0].distanceTo(center) < radius ||
planes[1].distanceTo(center) > radius ||
planes[2].distanceTo(center) > radius ||
planes[3].distanceTo(center) > radius ||
planes[4].distanceTo(center) > radius ||
planes[5].distanceTo(center) > radius;
}