summaryrefslogtreecommitdiff
path: root/engine/src/projection.rs
blob: aa84a9fdc7f4707bae8b4474f55b3829d8d600bc (plain)
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
use crate::matrix::Matrix;

#[derive(Debug)]
#[non_exhaustive]
pub enum Projection
{
    Perspective(Perspective),
}

/// Perspective projection parameters.
#[derive(Debug)]
pub struct Perspective
{
    pub fov_radians: f32,
    pub far: f32,
    pub near: f32,
}

impl Default for Perspective
{
    fn default() -> Self
    {
        Self {
            fov_radians: 80.0f32.to_radians(),
            far: 100.0,
            near: 0.1,
        }
    }
}

pub(crate) fn new_perspective_matrix(
    perspective: &Perspective,
    aspect: f32,
) -> Matrix<f32, 4, 4>
{
    let mut out = Matrix::new();

    out.set_cell(0, 0, (1.0 / (perspective.fov_radians / 2.0).tan()) / aspect);

    out.set_cell(1, 1, 1.0 / (perspective.fov_radians / 2.0).tan());

    out.set_cell(
        2,
        2,
        (perspective.near + perspective.far) / (perspective.near - perspective.far),
    );

    out.set_cell(
        2,
        3,
        (2.0 * perspective.near * perspective.far) / (perspective.near - perspective.far),
    );

    out.set_cell(3, 2, -1.0);

    out
}