batbox_la/mat/_4x4/
projection.rs

1use super::*;
2
3impl<T: Float> mat4<T> {
4    /// Construct prespective projection matrix.
5    pub fn perspective(fov: T, aspect: T, near: T, far: T) -> Self {
6        let ymax = near * (fov / (T::ONE + T::ONE)).tan();
7        let xmax = ymax * aspect;
8        Self::frustum(-xmax, xmax, -ymax, ymax, near, far)
9    }
10
11    /// Construct frustum projection matrix.
12    pub fn frustum(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Self {
13        let double_near = near + near;
14        let width = right - left;
15        let height = top - bottom;
16        let depth = far - near;
17        mat4::new([
18            [
19                double_near / width,
20                T::ZERO,
21                (right + left) / width,
22                T::ZERO,
23            ],
24            [
25                T::ZERO,
26                double_near / height,
27                (top + bottom) / height,
28                T::ZERO,
29            ],
30            [
31                T::ZERO,
32                T::ZERO,
33                (-far - near) / depth,
34                (-double_near * far) / depth,
35            ],
36            [T::ZERO, T::ZERO, -T::ONE, T::ZERO],
37        ])
38    }
39}