Skip to content

Commit

Permalink
Basic right-handed orthographic matrix function. (#33)
Browse files Browse the repository at this point in the history
* 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.

* Added unit tests for Mat4::orthographic_rh()

Which actually revealed significant bugs.  XD  Well,
that's what the test is for!

* aw heck, I left in commented-out code.  Removed.
  • Loading branch information
icefoxen authored and bitshifter committed Nov 21, 2019
1 parent d6e6d9c commit f4eb102
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/f32/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,24 @@ impl Mat4 {
)
}

/// Build right-handed orthographic projection matrix with the specified bounds and depth range.

This comment has been minimized.

Copy link
@hrydgard

hrydgard Nov 22, 2019

Contributor

It should also be mentioned in the comment that it creates a matrix for traditional OpenGL conventions with clip space Z from -1 to 1, instead of the D3D/Vulkan convention of 0 to 1.

For reference, here's a VK convention ortho matrix:
https://github.com/PacktPublishing/Vulkan-Cookbook/blob/5072918c059d3e6e18f951ecb008523c32ffb7b6/Library/Source%20Files/10%20Helper%20Recipes/05%20Preparing%20an%20orthographic%20projection%20matrix.cpp

This comment has been minimized.

Copy link
@bitshifter

bitshifter Nov 22, 2019

Owner

Ah thanks, I didn't think to check that. I probably need to at no zo to perspective function names like nalgebra does.

This comment has been minimized.

Copy link
@hrydgard

hrydgard Nov 22, 2019

Contributor

Right, that would be nice.

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();
Expand Down
8 changes: 8 additions & 0 deletions tests/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

1 comment on commit f4eb102

@bitshifter
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, I should have edited the commit message when I squashed it. That's what I get for merging PRs on my phone :)

Please sign in to comment.