From 386aad9dac7c22cabb0f1bbba33d248c40bb0310 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 23 Oct 2023 22:23:25 +0200 Subject: feat(engine): add perspective --- engine/src/projection.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 engine/src/projection.rs (limited to 'engine/src/projection.rs') diff --git a/engine/src/projection.rs b/engine/src/projection.rs new file mode 100644 index 0000000..d5186ba --- /dev/null +++ b/engine/src/projection.rs @@ -0,0 +1,21 @@ +use crate::matrix::Matrix; + +/// Creates a new perspective projection matrix. +#[must_use] +pub fn new_perspective( + fov_radians: f32, + aspect: f32, + far: f32, + near: f32, +) -> Matrix +{ + let mut out = Matrix::new(); + + out.set_cell(0, 0, (1.0 / (fov_radians / 2.0).tan()) / aspect); + out.set_cell(1, 1, 1.0 / (fov_radians / 2.0).tan()); + out.set_cell(2, 2, (near + far) / (near - far)); + out.set_cell(2, 3, (2.0 * near * far) / (near - far)); + out.set_cell(3, 2, -1.0); + + out +} -- cgit v1.2.3-18-g5258