summaryrefslogtreecommitdiff
path: root/engine/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-01-22 18:12:21 +0100
committerHampusM <hampus@hampusmat.com>2025-01-22 18:13:06 +0100
commitc10f3e758f7d98750cfffef336a124d6d65c636f (patch)
tree27943f94f2f2793178680a38faa958d20bbdff37 /engine/src
parentf5b3b11ad449a60322bca096de010a35ba2b30c8 (diff)
refactor(engine): clarify & add docs to Perspective::to_matrix
Diffstat (limited to 'engine/src')
-rw-r--r--engine/src/projection.rs19
-rw-r--r--engine/src/renderer/opengl.rs6
2 files changed, 17 insertions, 8 deletions
diff --git a/engine/src/projection.rs b/engine/src/projection.rs
index ecbe297..faa741f 100644
--- a/engine/src/projection.rs
+++ b/engine/src/projection.rs
@@ -22,15 +22,22 @@ pub struct Perspective
impl Perspective
{
- pub fn to_matrix(&self, aspect: f32) -> Matrix<f32, 4, 4>
+ /// Creates a perspective projection matrix using right-handed coordinates.
+ #[inline]
+ pub fn to_matrix_rh(&self, aspect: f32, clip_volume: ClipVolume)
+ -> Matrix<f32, 4, 4>
{
let mut out = Matrix::new();
- out.set_cell(0, 0, (1.0 / (self.fov_radians / 2.0).tan()) / aspect);
- out.set_cell(1, 1, 1.0 / (self.fov_radians / 2.0).tan());
- out.set_cell(2, 2, (self.near + self.far) / (self.near - self.far));
- out.set_cell(2, 3, (2.0 * self.near * self.far) / (self.near - self.far));
- out.set_cell(3, 2, -1.0);
+ match clip_volume {
+ ClipVolume::NegOneToOne => {
+ out.set_cell(0, 0, (1.0 / (self.fov_radians / 2.0).tan()) / aspect);
+ out.set_cell(1, 1, 1.0 / (self.fov_radians / 2.0).tan());
+ out.set_cell(2, 2, (self.near + self.far) / (self.near - self.far));
+ out.set_cell(2, 3, (2.0 * self.near * self.far) / (self.near - self.far));
+ out.set_cell(3, 2, -1.0);
+ }
+ }
out
}
diff --git a/engine/src/renderer/opengl.rs b/engine/src/renderer/opengl.rs
index 6a571eb..c44a479 100644
--- a/engine/src/renderer/opengl.rs
+++ b/engine/src/renderer/opengl.rs
@@ -451,8 +451,10 @@ fn apply_transformation_matrices(
#[allow(clippy::cast_precision_loss)]
let proj_matrix = match &camera.projection {
- Projection::Perspective(perspective_proj) => perspective_proj
- .to_matrix(window_size.width as f32 / window_size.height as f32),
+ Projection::Perspective(perspective_proj) => perspective_proj.to_matrix_rh(
+ window_size.width as f32 / window_size.height as f32,
+ ClipVolume::NegOneToOne,
+ ),
Projection::Orthographic(orthographic_proj) => {
orthographic_proj.to_matrix_rh(&camera_pos.position, ClipVolume::NegOneToOne)
}