diff --git a/nalgebra-glm/src/ext/matrix_clip_space.rs b/nalgebra-glm/src/ext/matrix_clip_space.rs index b4247478..bf8c6aeb 100644 --- a/nalgebra-glm/src/ext/matrix_clip_space.rs +++ b/nalgebra-glm/src/ext/matrix_clip_space.rs @@ -152,11 +152,12 @@ pub fn ortho_lh(left: N, right: N, bottom: N, top: N, znear: N, zfar: N /// pub fn ortho_lh_no(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> TMat4 { let zero : N = ::convert(0.0); + let one : N = ::convert(1.0); let two : N = ::convert(2.0); - let mut mat : TMat4 = TMat4::::new(zero,zero,zero,zero, - zero,zero,zero,zero, - zero,zero,zero,zero, - zero,zero,zero,zero); + let mut mat : TMat4 = TMat4::::new(one ,zero,zero,zero, + zero,one ,zero,zero, + zero,zero,one ,zero, + zero,zero,zero,one ); let m11 = if cfg!(feature="projection_y_flip") { @@ -166,11 +167,11 @@ pub fn ortho_lh_no(left: N, right: N, bottom: N, top: N, znear: N, zfar }; mat[(0,0)] = two / (right - left); + mat[(0,3)] = -(right + left) / (right - left); mat[(1,1)] = m11; + mat[(1,3)] = -(top + bottom) / (top - bottom); mat[(2,2)] = two / (zfar - znear); - mat[(3,0)] = -(right + left) / (right - left); - mat[(3,1)] = -(top + bottom) / (top - bottom); - mat[(3,2)] = -(zfar + znear) / (zfar - znear); + mat[(2,3)] = -(zfar + znear) / (zfar - znear); mat } @@ -199,10 +200,10 @@ pub fn ortho_lh_zo(left: N, right: N, bottom: N, top: N, znear: N, zfar let zero : N = ::convert(0.0); let one : N = ::convert(1.0); let two : N = ::convert(2.0); - let mut mat : TMat4 = TMat4::::new(zero,zero,zero,zero, - zero,zero,zero,zero, - zero,zero,zero,zero, - zero,zero,zero,zero); + let mut mat : TMat4 = TMat4::::new(one ,zero,zero,zero, + zero,one ,zero,zero, + zero,zero,one ,zero, + zero,zero,zero,one ); let m11 = if cfg!(feature="projection_y_flip") { @@ -212,11 +213,11 @@ pub fn ortho_lh_zo(left: N, right: N, bottom: N, top: N, znear: N, zfar }; mat[(0,0)] = two / (right - left); + mat[(0,3)] = - (right + left) / (right - left); mat[(1,1)] = m11; + mat[(1,3)] = - (top + bottom) / (top - bottom); mat[(2,2)] = one / (zfar - znear); - mat[(3,0)] = - (right + left) / (right - left); - mat[(3,1)] = - (top + bottom) / (top - bottom); - mat[(3,2)] = - znear / (zfar - znear); + mat[(2,3)] = - znear / (zfar - znear); mat } @@ -315,11 +316,12 @@ pub fn ortho_rh(left: N, right: N, bottom: N, top: N, znear: N, zfar: N /// pub fn ortho_rh_no(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> TMat4 { let zero : N = ::convert(0.0); + let one : N = ::convert(1.0); let two : N = ::convert(2.0); - let mut mat : TMat4 = TMat4::::new(zero,zero,zero,zero, - zero,zero,zero,zero, - zero,zero,zero,zero, - zero,zero,zero,zero); + let mut mat : TMat4 = TMat4::::new(one ,zero,zero,zero, + zero,one ,zero,zero, + zero,zero,one ,zero, + zero,zero,zero,one ); let m11 = if cfg!(feature="projection_y_flip") { @@ -329,11 +331,11 @@ pub fn ortho_rh_no(left: N, right: N, bottom: N, top: N, znear: N, zfar }; mat[(0,0)] = two / (right - left); + mat[(0,3)] = - (right + left) / (right - left); mat[(1,1)] = m11; + mat[(1,3)] = - (top + bottom) / (top - bottom); mat[(2,2)] = - two / (zfar - znear); - mat[(3,0)] = - (right + left) / (right - left); - mat[(3,1)] = - (top + bottom) / (top - bottom); - mat[(3,2)] = - (zfar + znear) / (zfar - znear); + mat[(2,3)] = - (zfar + znear) / (zfar - znear); mat } @@ -362,10 +364,10 @@ pub fn ortho_rh_zo(left: N, right: N, bottom: N, top: N, znear: N, zfar let zero : N = ::convert(0.0); let one : N = ::convert(1.0); let two : N = ::convert(2.0); - let mut mat : TMat4 = TMat4::::new(zero,zero,zero,zero, - zero,zero,zero,zero, - zero,zero,zero,zero, - zero,zero,zero,zero); + let mut mat : TMat4 = TMat4::::new(one ,zero,zero,zero, + zero,one ,zero,zero, + zero,zero,one ,zero, + zero,zero,zero,one ); let m11 = if cfg!(feature="projection_y_flip") { @@ -375,11 +377,11 @@ pub fn ortho_rh_zo(left: N, right: N, bottom: N, top: N, znear: N, zfar }; mat[(0,0)] = two / (right - left); + mat[(0,3)] = - (right + left) / (right - left); mat[(1,1)] = m11; + mat[(1,3)] = - (top + bottom) / (top - bottom); mat[(2,2)] = - one / (zfar - znear); - mat[(3,0)] = - (right + left) / (right - left); - mat[(3,1)] = - (top + bottom) / (top - bottom); - mat[(3,2)] = - znear / (zfar - znear); + mat[(2,3)] = - znear / (zfar - znear); mat } @@ -548,8 +550,8 @@ pub fn perspective_fov_lh_no(fov: N, width: N, height: N, near: N, far: mat[(0,0)] = w; mat[(1,1)] = m11; mat[(2,2)] = (far + near) / (far - near); - mat[(2,3)] = ::convert(1.0); - mat[(3,2)] = - (far * near * ::convert(2.0)) / (far - near); + mat[(2,3)] = - (far * near * ::convert(2.0)) / (far - near); + mat[(3,2)] = ::convert(1.0); mat } @@ -607,8 +609,8 @@ pub fn perspective_fov_lh_zo(fov: N, width: N, height: N, near: N, far: mat[(0,0)] = w; mat[(1,1)] = m11; mat[(2,2)] = far / (far - near); - mat[(2,3)] = ::convert(1.0); - mat[(3,2)] = -(far * near) / (far - near); + mat[(2,3)] = -(far * near) / (far - near); + mat[(3,2)] = ::convert(1.0); mat } @@ -737,8 +739,8 @@ pub fn perspective_fov_rh_no(fov: N, width: N, height: N, near: N, far: mat[(0,0)] = w; mat[(1,1)] = m11; mat[(2,2)] = - (far + near) / (far - near); - mat[(2,3)] = negone; - mat[(3,2)] = - (far * near * ::convert(2.0)) / (far - near); + mat[(2,3)] = - (far * near * ::convert(2.0)) / (far - near); + mat[(3,2)] = negone; mat } @@ -797,8 +799,8 @@ pub fn perspective_fov_rh_zo(fov: N, width: N, height: N, near: N, far: mat[(0,0)] = w; mat[(1,1)] = m11; mat[(2,2)] = far / (near - far); - mat[(2,3)] = negone; - mat[(3,2)] = -(far * near) / (far - near); + mat[(2,3)] = -(far * near) / (far - near); + mat[(3,2)] = negone; mat } @@ -960,8 +962,8 @@ pub fn perspective_lh_no(aspect: N, fovy: N, near: N, far: N) -> TMat4< mat[(0,0)] = one / (aspect * tan_half_fovy); mat[(1,1)] = m11; mat[(2,2)] = (far + near) / (far - near); - mat[(2,3)] = one; - mat[(3,2)] = -(two * far * near) / (far - near); + mat[(2,3)] = -(two * far * near) / (far - near); + mat[(3,2)] = one; mat } @@ -1014,8 +1016,8 @@ pub fn perspective_lh_zo(aspect: N, fovy: N, near: N, far: N) -> TMat4< mat[(0,0)] = one / (aspect * tan_half_fovy); mat[(1,1)] = m11; mat[(2,2)] = far / (far - near); - mat[(2,3)] = one; - mat[(3,2)] = -(far * near) / (far - near); + mat[(2,3)] = -(far * near) / (far - near); + mat[(3,2)] = one; mat } @@ -1137,8 +1139,8 @@ pub fn perspective_rh_no(aspect: N, fovy: N, near: N, far: N) -> TMat4< mat[(0,0)] = one / (aspect * tan_half_fovy); mat[(1,1)] = m11; mat[(2,2)] = - (far + near) / (far - near); - mat[(2,3)] = negone; - mat[(3,2)] = -(two * far * near) / (far - near); + mat[(2,3)] = -(two * far * near) / (far - near); + mat[(3,2)] = negone; mat } @@ -1192,8 +1194,8 @@ pub fn perspective_rh_zo(aspect: N, fovy: N, near: N, far: N) -> TMat4< mat[(0,0)] = one / (aspect * tan_half_fovy); mat[(1,1)] = m11; mat[(2,2)] = far / (near - far); - mat[(2,3)] = negone; - mat[(3,2)] = -(far * near) / (far - near); + mat[(2,3)] = -(far * near) / (far - near); + mat[(3,2)] = negone; mat } diff --git a/nalgebra-glm/tests/lib.rs b/nalgebra-glm/tests/lib.rs new file mode 100644 index 00000000..f071572a --- /dev/null +++ b/nalgebra-glm/tests/lib.rs @@ -0,0 +1,71 @@ +extern crate nalgebra as na; +extern crate nalgebra_glm as glm; + +use na::Perspective3; +use na::Orthographic3; +use glm::Mat4; +use glm::Vec4; + +#[test] +pub fn orthographic_glm_nalgebra_same() +{ + let na_mat : Mat4 = Orthographic3::new(-100.0f32,100.0f32, -50.0f32, 50.0f32, 0.1f32, 100.0f32).unwrap(); + let gl_mat : Mat4 = glm::ortho_rh_no(-100.0f32,100.0f32, -50.0f32, 50.0f32, 0.1f32, 100.0f32); + + // ortho_rh_no is the same as the standard nalgebra orthographic matrix + // + // we cant use plain ortho here as the library could be configured to produce different values + + assert_eq!(na_mat, gl_mat); +} + +#[test] +pub fn perspective_glm_nalgebra_same() +{ + let na_mat : Mat4 = Perspective3::new(16.0f32/9.0f32, 3.14f32/2.0f32, 0.1f32, 100.0f32).unwrap(); + let gl_mat : Mat4 = glm::perspective_rh_no(16.0f32/9.0f32, 3.14f32/2.0f32, 0.1f32, 100.0f32); + + // perspective_rh_no is the same as the standard nalgebra perspective matrix + // + // we cant use plain perspective here as the library could be configured to produce different + // values + + assert_eq!(na_mat, gl_mat); +} + +#[test] +pub fn orthographic_glm_nalgebra_project_same() +{ + let point = Vec4::new(1.0,0.0,-20.0,1.0); + + let na_mat : Mat4 = Orthographic3::new(-100.0f32,100.0f32, -50.0f32, 50.0f32, 0.1f32, 100.0f32).unwrap(); + let gl_mat : Mat4 = glm::ortho_rh_no(-100.0f32,100.0f32, -50.0f32, 50.0f32, 0.1f32, 100.0f32); + + // ortho_rh_no is the same as the standard nalgebra orthographic matrix + // + // we cant use plain ortho here as the library could be configured to produce different values + + let na_pt = na_mat * point; + let gl_pt = gl_mat * point; + + assert_eq!(na_pt, gl_pt); +} + +#[test] +pub fn perspective_glm_nalgebra_project_same() +{ + let point = Vec4::new(1.0,0.0,-20.0,1.0); + + let na_mat : Mat4 = Perspective3::new(16.0f32/9.0f32, 3.14f32/2.0f32, 0.1f32, 100.0f32).unwrap(); + let gl_mat : Mat4 = glm::perspective_rh_no(16.0f32/9.0f32, 3.14f32/2.0f32, 0.1f32, 100.0f32); + + // perspective_rh_no is the same as the standard nalgebra perspective matrix + // + // we cant use plain perspective here as the library could be configured to produce different + // values + + let na_pt = na_mat * point; + let gl_pt = gl_mat * point; + + assert_eq!(na_pt, gl_pt); +} \ No newline at end of file