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
|
#include "Frustum.hpp"
Frustum::Frustum(const glm::mat4 &vpMat) {
planes[RIGHT] = glm::vec4(
vpMat[0][3] - vpMat[0][0],
vpMat[1][3] - vpMat[1][0],
vpMat[2][3] - vpMat[2][0],
vpMat[3][3] - vpMat[3][0]);
planes[LEFT] = glm::vec4(
vpMat[0][3] + vpMat[0][0],
vpMat[1][3] + vpMat[1][0],
vpMat[2][3] + vpMat[2][0],
vpMat[3][3] + vpMat[3][0]);
planes[BOTTOM] = glm::vec4(
vpMat[0][3] + vpMat[0][1],
vpMat[1][3] + vpMat[1][1],
vpMat[2][3] + vpMat[2][1],
vpMat[3][3] + vpMat[3][1]);
planes[TOP] = glm::vec4(
vpMat[0][3] - vpMat[0][1],
vpMat[1][3] - vpMat[1][1],
vpMat[2][3] - vpMat[2][1],
vpMat[3][3] - vpMat[3][1]);
planes[FAR] = glm::vec4(
vpMat[0][3] - vpMat[0][2],
vpMat[1][3] - vpMat[1][2],
vpMat[2][3] - vpMat[2][2],
vpMat[3][3] - vpMat[3][2]);
planes[NEAR] = glm::vec4(
vpMat[0][3] + vpMat[0][2],
vpMat[1][3] + vpMat[1][2],
vpMat[2][3] + vpMat[2][2],
vpMat[3][3] + vpMat[3][2]);
for (auto &plane : planes) {
float magnitude = sqrt(plane.x * plane.x + plane.y * plane.y + plane.z * plane.z);
plane.x /= magnitude;
plane.y /= magnitude;
plane.z /= magnitude;
plane.w /= magnitude;
}
}
bool Frustum::TestPoint(const glm::vec3 &pos) {
for (const auto &plane : planes) {
if (GetDistanceToPoint(plane, pos) < 0)
return false;
}
return true;
}
bool Frustum::TestSphere(const glm::vec3 &pos, float radius) {
for (const auto &plane : planes) {
if (GetDistanceToPoint(plane, pos) < -radius)
return false;
}
return true;
}
|