From f1a5fa63867d7dc4aaba785d3aafce3f11940df4 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 14 Sep 2026 10:16:46 +0200 Subject: feat(engine): add free standing fn for creating a ortho projection matrix --- engine/src/projection.rs | 55 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/engine/src/projection.rs b/engine/src/projection.rs index 2191a93..b3bfa00 100644 --- a/engine/src/projection.rs +++ b/engine/src/projection.rs @@ -119,8 +119,6 @@ impl Orthographic clip_volume: ClipVolume, ) -> Matrix { - let mut result = Matrix::::new(); - let size = match self.size { OrthographicSize::FixedSize(fixed_size) => fixed_size, OrthographicSize::WindowSize => Dimens { @@ -141,21 +139,10 @@ impl Orthographic let near = self.near; let far = self.far; - match clip_volume { - ClipVolume::NegOneToOne => { - result[MatrixCellPos { row: 0, col: 0 }] = 2.0 / (right - left); - result[MatrixCellPos { row: 1, col: 1 }] = 2.0 / (top - bottom); - result[MatrixCellPos { row: 2, col: 2 }] = -2.0 / (far - near); - result[MatrixCellPos { row: 0, col: 3 }] = - -(right + left) / (right - left); - result[MatrixCellPos { row: 1, col: 3 }] = - -(top + bottom) / (top - bottom); - result[MatrixCellPos { row: 2, col: 3 }] = -(far + near) / (far - near); - result[MatrixCellPos { row: 3, col: 3 }] = 1.0; - } - } - - result + orthographic_rh( + OrthographicParams { left, right, bottom, top, near, far }, + clip_volume, + ) } } @@ -187,3 +174,37 @@ pub enum ClipVolume /// -1 to +1. This is the OpenGL clip volume definition. NegOneToOne, } + +#[derive(Debug, Clone)] +pub struct OrthographicParams +{ + pub left: f32, + pub right: f32, + pub bottom: f32, + pub top: f32, + pub near: f32, + pub far: f32, +} + +/// Creates a orthographic projection matrix using right-handed coordinates. +pub fn orthographic_rh( + OrthographicParams { left, right, bottom, top, near, far }: OrthographicParams, + clip_volume: ClipVolume, +) -> Matrix +{ + let mut result = Matrix::::new(); + + match clip_volume { + ClipVolume::NegOneToOne => { + result[MatrixCellPos { row: 0, col: 0 }] = 2.0 / (right - left); + result[MatrixCellPos { row: 1, col: 1 }] = 2.0 / (top - bottom); + result[MatrixCellPos { row: 2, col: 2 }] = -2.0 / (far - near); + result[MatrixCellPos { row: 0, col: 3 }] = -(right + left) / (right - left); + result[MatrixCellPos { row: 1, col: 3 }] = -(top + bottom) / (top - bottom); + result[MatrixCellPos { row: 2, col: 3 }] = -(far + near) / (far - near); + result[MatrixCellPos { row: 3, col: 3 }] = 1.0; + } + } + + result +} -- cgit v1.2.3-18-g5258