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
#[allow(unused)]
#[derive(Debug)]
pub struct Frustum {
planes: [[f32; 4]; 6],
}
#[allow(unused)]
impl Frustum {
#[inline]
pub fn from_modelview_projection(clip: &[f32; 16]) -> Frustum {
let rdx: f32 = clip[3] - clip[0];
let rdy: f32 = clip[7] - clip[4];
let rdz: f32 = clip[11] - clip[8];
let rdw: f32 = clip[15] - clip[12];
let rd: f32 = (rdx.powi(2) + rdy.powi(2) + rdz.powi(2)).sqrt();
debug_assert!(rd > 0.0f32, "Invalid modelview projection matrix: right plane divisor cannot be 0");
let ldx: f32 = clip[3] + clip[0];
let ldy: f32 = clip[7] + clip[4];
let ldz: f32 = clip[11] + clip[8];
let ldw: f32 = clip[15] + clip[12];
let ld: f32 = (ldx.powi(2) + ldy.powi(2) + ldz.powi(2)).sqrt();
debug_assert!(rd > 0.0f32, "Invalid modelview projection matrix: left plane divisor cannot be 0");
let bdx: f32 = clip[3] + clip[1];
let bdy: f32 = clip[7] + clip[5];
let bdz: f32 = clip[11] + clip[9];
let bdw: f32 = clip[15] + clip[13];
let bd: f32 = (bdx.powi(2) + bdy.powi(2) + bdz.powi(2)).sqrt();
debug_assert!(rd > 0.0f32, "Invalid modelview projection matrix: bottom plane divisor cannot be 0");
let tdx: f32 = clip[3] - clip[1];
let tdy: f32 = clip[7] - clip[5];
let tdz: f32 = clip[11] - clip[9];
let tdw: f32 = clip[15] - clip[13];
let td: f32 = (tdx.powi(2) + tdy.powi(2) + tdz.powi(2)).sqrt();
debug_assert!(rd > 0.0f32, "Invalid modelview projection matrix: top plane divisor cannot be 0");
let fdx: f32 = clip[3] - clip[2];
let fdy: f32 = clip[7] - clip[6];
let fdz: f32 = clip[11] - clip[10];
let fdw: f32 = clip[15] - clip[14];
let fd: f32 = (fdx.powi(2) + fdy.powi(2) + fdz.powi(2)).sqrt();
debug_assert!(rd > 0.0f32, "Invalid modelview projection matrix: far plane divisor cannot be 0");
let ndx: f32 = clip[3] + clip[2];
let ndy: f32 = clip[7] + clip[6];
let ndz: f32 = clip[11] + clip[10];
let ndw: f32 = clip[15] + clip[14];
let nd: f32 = (ndx.powi(2) + ndy.powi(2) + ndz.powi(2)).sqrt();
debug_assert!(rd > 0.0f32, "Invalid modelview projection matrix: near plane divisor cannot be 0");
Frustum {
planes: [[rdx / rd, rdy / rd, rdz / rd, rdw / rd],
[ldx / ld, ldy / ld, ldz / ld, ldw / ld],
[bdx / bd, bdy / bd, bdz / bd, bdw / bd],
[tdx / td, tdy / td, tdz / td, tdw / td],
[fdx / fd, fdy / fd, fdz / fd, fdw / fd],
[ndx / nd, ndy / nd, ndz / nd, ndw / nd]],
}
}
pub fn from_modelview_and_projection(modl: &[f32; 16], proj: &[f32; 16]) -> Frustum {
let clip: [f32; 16] = [
modl[0] * proj[0] + modl[1] * proj[4] + modl[2] * proj[8] + modl[3] * proj[12],
modl[0] * proj[1] + modl[1] * proj[5] + modl[2] * proj[9] + modl[3] * proj[13],
modl[0] * proj[2] + modl[1] * proj[6] + modl[2] * proj[10] + modl[3] * proj[14],
modl[0] * proj[3] + modl[1] * proj[7] + modl[2] * proj[11] + modl[3] * proj[15],
modl[4] * proj[0] + modl[5] * proj[4] + modl[6] * proj[8] + modl[7] * proj[12],
modl[4] * proj[1] + modl[5] * proj[5] + modl[6] * proj[9] + modl[7] * proj[13],
modl[4] * proj[2] + modl[5] * proj[6] + modl[6] * proj[10] + modl[7] * proj[14],
modl[4] * proj[3] + modl[5] * proj[7] + modl[6] * proj[11] + modl[7] * proj[15],
modl[8] * proj[0] + modl[9] * proj[4] + modl[10] * proj[8] + modl[11] * proj[12],
modl[8] * proj[1] + modl[9] * proj[5] + modl[10] * proj[9] + modl[11] * proj[13],
modl[8] * proj[2] + modl[9] * proj[6] + modl[10] * proj[10] + modl[11] * proj[14],
modl[8] * proj[3] + modl[9] * proj[7] + modl[10] * proj[11] + modl[11] * proj[15],
modl[12] * proj[0] + modl[13] * proj[4] + modl[14] * proj[8] + modl[15] * proj[12],
modl[12] * proj[1] + modl[13] * proj[5] + modl[14] * proj[9] + modl[15] * proj[13],
modl[12] * proj[2] + modl[13] * proj[6] + modl[14] * proj[10] + modl[15] * proj[14],
modl[12] * proj[3] + modl[13] * proj[7] + modl[14] * proj[11] + modl[15] * proj[15],
];
Frustum::from_modelview_projection(&clip)
}
pub fn from_modelview_and_projection_2d(modl: &[[f32; 4]; 4], proj: &[[f32; 4]; 4]) -> Frustum {
let modl_1d: [f32; 16] = [
modl[0][0],
modl[0][1],
modl[0][2],
modl[0][3],
modl[1][0],
modl[1][1],
modl[1][2],
modl[1][3],
modl[2][0],
modl[2][1],
modl[2][2],
modl[2][3],
modl[3][0],
modl[3][1],
modl[3][2],
modl[3][3],
];
let proj_1d: [f32; 16] = [
proj[0][0],
proj[0][1],
proj[0][2],
proj[0][3],
proj[1][0],
proj[1][1],
proj[1][2],
proj[1][3],
proj[2][0],
proj[2][1],
proj[2][2],
proj[2][3],
proj[3][0],
proj[3][1],
proj[3][2],
proj[3][3],
];
return Frustum::from_modelview_and_projection(&modl_1d, &proj_1d);
}
pub fn point_intersecting(&self, x: &f32, y: &f32, z: &f32) -> bool {
for p in 0..6 {
if (self.planes[p][0] * x + self.planes[p][1] * y + self.planes[p][2] * z + self.planes[p][3]) <= 0.0f32 {
return false;
}
}
true
}
pub fn sphere_intersecting(&self, x: &f32, y: &f32, z: &f32, r: &f32) -> bool {
for p in 0..6 {
if self.planes[p][0] * x + self.planes[p][1] * y + self.planes[p][2] * z + self.planes[p][3] <= (0.0f32 - r) {
return false;
}
}
true
}
}