Repository: bitshifter/glam-rs (rust)

Issue:
Add `Mat4::ortho`
Because orthographic projections are cool.  If you want this, I'll happily PR it.

Pull request description:
Basic right-handed orthographic matrix function.
A little WIP at the moment still, but it's Code That I've Used
So I Know It Works(tm).  Barring typos, I guess.  Also checked
against `glm::ortho`.  I agree that we do want actual unit tests
though, so that's what I'm going to attempt next.

Will resolve #32.

Patch:
diff --git a/src/f32/mat4.rs b/src/f32/mat4.rs
index 100519f..dbd9f1c 100644
--- a/src/f32/mat4.rs
+++ b/src/f32/mat4.rs
@@ -540,6 +540,24 @@ impl Mat4 {
         )
     }
 
+    /// Build right-handed orthographic projection matrix with the specified bounds and depth range.
+    pub fn orthographic_rh(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) -> Mat4 {
+        let a = 2.0 / (right - left);
+        let b = 2.0 / (top - bottom);
+        let c = -2.0 / (far - near);
+        let d1 = -(right + left) / (right - left);
+        let d2 = -(top + bottom) / (top - bottom);
+        let d3 = -(far + near) / (far - near);
+
+        Mat4::from_cols(
+            Vec4::new(a, 0.0, 0.0, 0.0),
+            Vec4::new(0.0, b, 0.0, 0.0),
+            Vec4::new(0.0, 0.0, c, 0.0),
+            Vec4::new(d1, d2, d3, 1.0),
+        )
+    }
+
+
     #[inline]
     pub fn mul_vec4(&self, other: Vec4) -> Vec4 {
         let mut res = self.x_axis * other.dup_x();


Test patch:
diff --git a/tests/mat4.rs b/tests/mat4.rs
index a76ec4b..f6d54ac 100644
--- a/tests/mat4.rs
+++ b/tests/mat4.rs
@@ -254,6 +254,14 @@ fn test_mat4_look_at() {
     assert_approx_eq!(rh.transform_point3(point), Vec3::new(0.0, 1.0, -5.0));
 }
 
+#[test]
+fn test_mat4_orthographic_rh() {
+    let projection = Mat4::orthographic_rh(-10.0, 10.0, -5.0, 5.0, 0.0, -10.0);
+    let original = Vec4::new(5.0, 5.0, -5.0, 1.0);
+    let projected = projection.mul_vec4(original);
+    assert_approx_eq!(projected, Vec4::new(0.5, 1.0, -2.0, 1.0));
+}
+
 #[test]
 fn test_mat4_ops() {
     let m0 = Mat4::from_cols_array_2d(&MATRIX);