-
Notifications
You must be signed in to change notification settings - Fork 0
/
frustum.h
179 lines (145 loc) · 3.53 KB
/
frustum.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
#pragma once
#include "plane.h"
#include "aabb.h"
struct frustum
{
enum
{
PLANE_LEFT,
PLANE_RIGHT,
PLANE_BOTTOM,
PLANE_TOP,
PLANE_NEAR,
PLANE_FAR
};
void calculate_points(const mat4& viewProjection, float minz = 0.0f)
{
mat4 m = viewProjection.inverse();
vec3 minp(-1.0, -1.0, minz);
vec3 maxp(1.0, 1.0, 1.0);
pt[0] = vec3(minp.x, minp.y, minp.z);
pt[1] = vec3(maxp.x, minp.y, minp.z);
pt[2] = vec3(minp.x, maxp.y, minp.z);
pt[3] = vec3(maxp.x, maxp.y, minp.z);
pt[4] = vec3(minp.x, minp.y, maxp.z);
pt[5] = vec3(maxp.x, minp.y, maxp.z);
pt[6] = vec3(minp.x, maxp.y, maxp.z);
pt[7] = vec3(maxp.x, maxp.y, maxp.z);
for(int i = 0; i < 8; i++)
pt[i] = m * pt[i];
}
void calculate_planes(const mat4& viewProjection)
{
mat4 m = viewProjection.transpose();
p[PLANE_LEFT].pl = m.row3() + m.row0();
p[PLANE_RIGHT].pl = m.row3() - m.row0();
p[PLANE_BOTTOM].pl = m.row3() + m.row1();
p[PLANE_TOP].pl = m.row3() - m.row1();
p[PLANE_NEAR].pl = m.row2();
p[PLANE_FAR].pl = m.row3() - m.row2();
for(int i = 0; i < 6; i++)
p[i].normalize();
}
// Visibility tests
bool point_inside(const vec3& pos)
{
for(int i = 0; i < 6; i++)
{
if(pos.x * p[i].pl.x + pos.y * p[i].pl.y + pos.z * p[i].pl.z + p[i].pl.w <= 0.0f)
return false;
}
return true;
}
bool point_inside(const vec4& pos)
{
for(int i = 0; i < 6; i++)
{
if(pos.x * p[i].pl.x + pos.y * p[i].pl.y + pos.z * p[i].pl.z + p[i].pl.w <= 0.0f)
return false;
}
return true;
}
unsigned point_outside_plane(const vec3& pos)
{
for(int i = 0; i < 6; i++)
{
if(pos.x * p[i].pl.x + pos.y * p[i].pl.y + pos.z * p[i].pl.z + p[i].pl.w <= 0.0f)
return i;
}
return ~0u;
}
unsigned point_outside_plane(const vec4& pos)
{
for(int i = 0; i < 6; i++)
{
if(pos.x * p[i].pl.x + pos.y * p[i].pl.y + pos.z * p[i].pl.z + p[i].pl.w <= 0.0f)
return i;
}
return ~0u;
}
bool sphere_inside(const vec3& pos, float radius)
{
for(int i = 0; i < 6; i++)
{
if(pos.x * p[i].pl.x + pos.y * p[i].pl.y + pos.z * p[i].pl.z + p[i].pl.w <= -radius)
return false;
}
return true;
}
bool sprite_inside(const vec3& p1, const vec3& p2, const vec3& p3, const vec3& p4)
{
for(int i = 0; i < 6; i++)
{
bool inside = false;
if(dot(p1, p[i].pl) > 0)
inside = true;
else if(dot(p2, p[i].pl) > 0)
inside = true;
else if(dot(p3, p[i].pl) > 0)
inside = true;
else if(dot(p4, p[i].pl) > 0)
inside = true;
if(!inside)
return false;
}
return true;
}
bool aabb_inside(const aabb& box)
{
float minX = box.center.x - box.size.x;
float minY = box.center.y - box.size.y;
float minZ = box.center.z - box.size.z;
float maxX = box.center.x + box.size.x;
float maxY = box.center.y + box.size.y;
float maxZ = box.center.z + box.size.z;
// Get aabb points
float points[][3] =
{
{ minX, minY, minZ },
{ maxX, minY, minZ },
{ maxX, maxY, minZ },
{ minX, maxY, minZ },
{ minX, minY, maxZ },
{ maxX, minY, maxZ },
{ maxX, maxY, maxZ },
{ minX, maxY, maxZ }
};
for(int i = 0; i < 6; i++)
{
bool inside = false;
for(int j = 0; j < 8; j++)
{
if(points[j][0] * p[i].pl.x + points[j][1] * p[i].pl.y + points[j][2] * p[i].pl.z + p[i].pl.w > 0)
{
inside = true;
break;
}
}
if(!inside)
return false;
}
return true;
}
vec3 pt[8];
plane p[6];
};