| 11 | namespace frustum |
| 12 | { |
| 13 | void from_matrix(Frustum &f, const Matrix4x4 &m, bool homogeneous_ndc, int handedness) |
| 14 | { |
| 15 | // See: https://www8.cs.umu.se/kurser/5DV051/HT12/lab/plane_extraction.pdf |
| 16 | // Left plane. |
| 17 | f.planes[0].n.x = m.x.w + m.x.y; |
| 18 | f.planes[0].n.y = m.y.w + m.y.y; |
| 19 | f.planes[0].n.z = m.z.w + m.z.y; |
| 20 | f.planes[0].d = -(m.t.w + m.t.y); |
| 21 | plane3::normalize(f.planes[0]); |
| 22 | |
| 23 | // Right plane. |
| 24 | f.planes[1].n.x = m.x.w - m.x.x; |
| 25 | f.planes[1].n.y = m.y.w - m.y.x; |
| 26 | f.planes[1].n.z = m.z.w - m.z.x; |
| 27 | f.planes[1].d = -(m.t.w - m.t.x); |
| 28 | plane3::normalize(f.planes[1]); |
| 29 | |
| 30 | // Bottom plane. |
| 31 | f.planes[2].n.x = m.x.w - m.x.y; |
| 32 | f.planes[2].n.y = m.y.w - m.y.y; |
| 33 | f.planes[2].n.z = m.z.w - m.z.y; |
| 34 | f.planes[2].d = -(m.t.w - m.t.y); |
| 35 | plane3::normalize(f.planes[2]); |
| 36 | |
| 37 | // Top plane. |
| 38 | f.planes[3].n.x = m.x.w + m.x.x; |
| 39 | f.planes[3].n.y = m.y.w + m.y.x; |
| 40 | f.planes[3].n.z = m.z.w + m.z.x; |
| 41 | f.planes[3].d = -(m.t.w + m.t.x); |
| 42 | plane3::normalize(f.planes[3]); |
| 43 | |
| 44 | // Near plane. |
| 45 | const f32 near_x = homogeneous_ndc ? m.x.w + m.x.z : m.x.z; |
| 46 | const f32 near_y = homogeneous_ndc ? m.y.w + m.y.z : m.y.z; |
| 47 | const f32 near_z = homogeneous_ndc ? m.z.w + m.z.z : m.z.z; |
| 48 | const f32 near_d = homogeneous_ndc ? -(m.t.w + m.t.z) : -m.t.z; |
| 49 | f.planes[4].n.x = handedness == 0 ? -near_x : near_x; |
| 50 | f.planes[4].n.y = handedness == 0 ? -near_y : near_y; |
| 51 | f.planes[4].n.z = handedness == 0 ? -near_z : near_z; |
| 52 | f.planes[4].d = handedness == 0 ? -near_d : near_d; |
| 53 | plane3::normalize(f.planes[4]); |
| 54 | |
| 55 | // Far plane. |
| 56 | const f32 far_x = m.x.w - m.x.z; |
| 57 | const f32 far_y = m.y.w - m.y.z; |
| 58 | const f32 far_z = m.z.w - m.z.z; |
| 59 | const f32 far_d = -(m.t.w - m.t.z); |
| 60 | f.planes[5].n.x = handedness == 0 ? -far_x : far_x; |
| 61 | f.planes[5].n.y = handedness == 0 ? -far_y : far_y; |
| 62 | f.planes[5].n.z = handedness == 0 ? -far_z : far_z; |
| 63 | f.planes[5].d = handedness == 0 ? -far_d : far_d; |
| 64 | plane3::normalize(f.planes[5]); |
| 65 | } |
| 66 | |
| 67 | void split(Frustum *splits, u32 num, const Frustum &f, f32 weight, f32 overlap) |
| 68 | { |