summaryrefslogtreecommitdiff
path: root/engine/src/projection.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-04-15 23:09:11 +0200
committerHampusM <hampus@hampusmat.com>2024-04-15 23:09:11 +0200
commit9778d9668f8b5d651aaed1f64414f7ed195d153d (patch)
treee7c67bec8e03816e75aad7b1dd89730912908c8d /engine/src/projection.rs
parentb748977205eea249dc61e6f755bd6ff86c8f535b (diff)
feat(engine): add projection options to camera component
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