summaryrefslogtreecommitdiff
path: root/engine/src/projection.rs
diff options
context:
space:
mode:
Diffstat (limited to 'engine/src/projection.rs')
-rw-r--r--engine/src/projection.rs56
1 files changed, 46 insertions, 10 deletions
diff --git a/engine/src/projection.rs b/engine/src/projection.rs
index d5186ba..aa84a9f 100644
--- a/engine/src/projection.rs
+++ b/engine/src/projection.rs
@@ -1,20 +1,56 @@
use crate::matrix::Matrix;
-/// Creates a new perspective projection matrix.
-#[must_use]
-pub fn new_perspective(
- fov_radians: f32,
+#[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,
- far: f32,
- near: f32,
) -> Matrix<f32, 4, 4>
{
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(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