Fix incorrect matrix indexing

This commit is contained in:
MindSpunk 2018-12-14 22:50:58 +11:00 committed by Sébastien Crozet
parent 3518f95006
commit 205ca1f10c
2 changed files with 117 additions and 44 deletions

View File

@ -152,11 +152,12 @@ pub fn ortho_lh<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N
/// ///
pub fn ortho_lh_no<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> TMat4<N> { pub fn ortho_lh_no<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> TMat4<N> {
let zero : N = ::convert(0.0); let zero : N = ::convert(0.0);
let one : N = ::convert(1.0);
let two : N = ::convert(2.0); let two : N = ::convert(2.0);
let mut mat : TMat4<N> = TMat4::<N>::new(zero,zero,zero,zero, let mut mat : TMat4<N> = TMat4::<N>::new(one ,zero,zero,zero,
zero,zero,zero,zero, zero,one ,zero,zero,
zero,zero,zero,zero, zero,zero,one ,zero,
zero,zero,zero,zero); zero,zero,zero,one );
let m11 = let m11 =
if cfg!(feature="projection_y_flip") { if cfg!(feature="projection_y_flip") {
@ -166,11 +167,11 @@ pub fn ortho_lh_no<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar
}; };
mat[(0,0)] = two / (right - left); mat[(0,0)] = two / (right - left);
mat[(0,3)] = -(right + left) / (right - left);
mat[(1,1)] = m11; mat[(1,1)] = m11;
mat[(1,3)] = -(top + bottom) / (top - bottom);
mat[(2,2)] = two / (zfar - znear); mat[(2,2)] = two / (zfar - znear);
mat[(3,0)] = -(right + left) / (right - left); mat[(2,3)] = -(zfar + znear) / (zfar - znear);
mat[(3,1)] = -(top + bottom) / (top - bottom);
mat[(3,2)] = -(zfar + znear) / (zfar - znear);
mat mat
} }
@ -199,10 +200,10 @@ pub fn ortho_lh_zo<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar
let zero : N = ::convert(0.0); let zero : N = ::convert(0.0);
let one : N = ::convert(1.0); let one : N = ::convert(1.0);
let two : N = ::convert(2.0); let two : N = ::convert(2.0);
let mut mat : TMat4<N> = TMat4::<N>::new(zero,zero,zero,zero, let mut mat : TMat4<N> = TMat4::<N>::new(one ,zero,zero,zero,
zero,zero,zero,zero, zero,one ,zero,zero,
zero,zero,zero,zero, zero,zero,one ,zero,
zero,zero,zero,zero); zero,zero,zero,one );
let m11 = let m11 =
if cfg!(feature="projection_y_flip") { if cfg!(feature="projection_y_flip") {
@ -212,11 +213,11 @@ pub fn ortho_lh_zo<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar
}; };
mat[(0,0)] = two / (right - left); mat[(0,0)] = two / (right - left);
mat[(0,3)] = - (right + left) / (right - left);
mat[(1,1)] = m11; mat[(1,1)] = m11;
mat[(1,3)] = - (top + bottom) / (top - bottom);
mat[(2,2)] = one / (zfar - znear); mat[(2,2)] = one / (zfar - znear);
mat[(3,0)] = - (right + left) / (right - left); mat[(2,3)] = - znear / (zfar - znear);
mat[(3,1)] = - (top + bottom) / (top - bottom);
mat[(3,2)] = - znear / (zfar - znear);
mat mat
} }
@ -315,11 +316,12 @@ pub fn ortho_rh<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N
/// ///
pub fn ortho_rh_no<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> TMat4<N> { pub fn ortho_rh_no<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> TMat4<N> {
let zero : N = ::convert(0.0); let zero : N = ::convert(0.0);
let one : N = ::convert(1.0);
let two : N = ::convert(2.0); let two : N = ::convert(2.0);
let mut mat : TMat4<N> = TMat4::<N>::new(zero,zero,zero,zero, let mut mat : TMat4<N> = TMat4::<N>::new(one ,zero,zero,zero,
zero,zero,zero,zero, zero,one ,zero,zero,
zero,zero,zero,zero, zero,zero,one ,zero,
zero,zero,zero,zero); zero,zero,zero,one );
let m11 = let m11 =
if cfg!(feature="projection_y_flip") { if cfg!(feature="projection_y_flip") {
@ -329,11 +331,11 @@ pub fn ortho_rh_no<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar
}; };
mat[(0,0)] = two / (right - left); mat[(0,0)] = two / (right - left);
mat[(0,3)] = - (right + left) / (right - left);
mat[(1,1)] = m11; mat[(1,1)] = m11;
mat[(1,3)] = - (top + bottom) / (top - bottom);
mat[(2,2)] = - two / (zfar - znear); mat[(2,2)] = - two / (zfar - znear);
mat[(3,0)] = - (right + left) / (right - left); mat[(2,3)] = - (zfar + znear) / (zfar - znear);
mat[(3,1)] = - (top + bottom) / (top - bottom);
mat[(3,2)] = - (zfar + znear) / (zfar - znear);
mat mat
} }
@ -362,10 +364,10 @@ pub fn ortho_rh_zo<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar
let zero : N = ::convert(0.0); let zero : N = ::convert(0.0);
let one : N = ::convert(1.0); let one : N = ::convert(1.0);
let two : N = ::convert(2.0); let two : N = ::convert(2.0);
let mut mat : TMat4<N> = TMat4::<N>::new(zero,zero,zero,zero, let mut mat : TMat4<N> = TMat4::<N>::new(one ,zero,zero,zero,
zero,zero,zero,zero, zero,one ,zero,zero,
zero,zero,zero,zero, zero,zero,one ,zero,
zero,zero,zero,zero); zero,zero,zero,one );
let m11 = let m11 =
if cfg!(feature="projection_y_flip") { if cfg!(feature="projection_y_flip") {
@ -375,11 +377,11 @@ pub fn ortho_rh_zo<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar
}; };
mat[(0,0)] = two / (right - left); mat[(0,0)] = two / (right - left);
mat[(0,3)] = - (right + left) / (right - left);
mat[(1,1)] = m11; mat[(1,1)] = m11;
mat[(1,3)] = - (top + bottom) / (top - bottom);
mat[(2,2)] = - one / (zfar - znear); mat[(2,2)] = - one / (zfar - znear);
mat[(3,0)] = - (right + left) / (right - left); mat[(2,3)] = - znear / (zfar - znear);
mat[(3,1)] = - (top + bottom) / (top - bottom);
mat[(3,2)] = - znear / (zfar - znear);
mat mat
} }
@ -548,8 +550,8 @@ pub fn perspective_fov_lh_no<N: Real>(fov: N, width: N, height: N, near: N, far:
mat[(0,0)] = w; mat[(0,0)] = w;
mat[(1,1)] = m11; mat[(1,1)] = m11;
mat[(2,2)] = (far + near) / (far - near); mat[(2,2)] = (far + near) / (far - near);
mat[(2,3)] = ::convert(1.0); mat[(2,3)] = - (far * near * ::convert(2.0)) / (far - near);
mat[(3,2)] = - (far * near * ::convert(2.0)) / (far - near); mat[(3,2)] = ::convert(1.0);
mat mat
} }
@ -607,8 +609,8 @@ pub fn perspective_fov_lh_zo<N: Real>(fov: N, width: N, height: N, near: N, far:
mat[(0,0)] = w; mat[(0,0)] = w;
mat[(1,1)] = m11; mat[(1,1)] = m11;
mat[(2,2)] = far / (far - near); mat[(2,2)] = far / (far - near);
mat[(2,3)] = ::convert(1.0); mat[(2,3)] = -(far * near) / (far - near);
mat[(3,2)] = -(far * near) / (far - near); mat[(3,2)] = ::convert(1.0);
mat mat
} }
@ -737,8 +739,8 @@ pub fn perspective_fov_rh_no<N: Real>(fov: N, width: N, height: N, near: N, far:
mat[(0,0)] = w; mat[(0,0)] = w;
mat[(1,1)] = m11; mat[(1,1)] = m11;
mat[(2,2)] = - (far + near) / (far - near); mat[(2,2)] = - (far + near) / (far - near);
mat[(2,3)] = negone; mat[(2,3)] = - (far * near * ::convert(2.0)) / (far - near);
mat[(3,2)] = - (far * near * ::convert(2.0)) / (far - near); mat[(3,2)] = negone;
mat mat
} }
@ -797,8 +799,8 @@ pub fn perspective_fov_rh_zo<N: Real>(fov: N, width: N, height: N, near: N, far:
mat[(0,0)] = w; mat[(0,0)] = w;
mat[(1,1)] = m11; mat[(1,1)] = m11;
mat[(2,2)] = far / (near - far); mat[(2,2)] = far / (near - far);
mat[(2,3)] = negone; mat[(2,3)] = -(far * near) / (far - near);
mat[(3,2)] = -(far * near) / (far - near); mat[(3,2)] = negone;
mat mat
} }
@ -960,8 +962,8 @@ pub fn perspective_lh_no<N: Real>(aspect: N, fovy: N, near: N, far: N) -> TMat4<
mat[(0,0)] = one / (aspect * tan_half_fovy); mat[(0,0)] = one / (aspect * tan_half_fovy);
mat[(1,1)] = m11; mat[(1,1)] = m11;
mat[(2,2)] = (far + near) / (far - near); mat[(2,2)] = (far + near) / (far - near);
mat[(2,3)] = one; mat[(2,3)] = -(two * far * near) / (far - near);
mat[(3,2)] = -(two * far * near) / (far - near); mat[(3,2)] = one;
mat mat
} }
@ -1014,8 +1016,8 @@ pub fn perspective_lh_zo<N: Real>(aspect: N, fovy: N, near: N, far: N) -> TMat4<
mat[(0,0)] = one / (aspect * tan_half_fovy); mat[(0,0)] = one / (aspect * tan_half_fovy);
mat[(1,1)] = m11; mat[(1,1)] = m11;
mat[(2,2)] = far / (far - near); mat[(2,2)] = far / (far - near);
mat[(2,3)] = one; mat[(2,3)] = -(far * near) / (far - near);
mat[(3,2)] = -(far * near) / (far - near); mat[(3,2)] = one;
mat mat
} }
@ -1137,8 +1139,8 @@ pub fn perspective_rh_no<N: Real>(aspect: N, fovy: N, near: N, far: N) -> TMat4<
mat[(0,0)] = one / (aspect * tan_half_fovy); mat[(0,0)] = one / (aspect * tan_half_fovy);
mat[(1,1)] = m11; mat[(1,1)] = m11;
mat[(2,2)] = - (far + near) / (far - near); mat[(2,2)] = - (far + near) / (far - near);
mat[(2,3)] = negone; mat[(2,3)] = -(two * far * near) / (far - near);
mat[(3,2)] = -(two * far * near) / (far - near); mat[(3,2)] = negone;
mat mat
} }
@ -1192,8 +1194,8 @@ pub fn perspective_rh_zo<N: Real>(aspect: N, fovy: N, near: N, far: N) -> TMat4<
mat[(0,0)] = one / (aspect * tan_half_fovy); mat[(0,0)] = one / (aspect * tan_half_fovy);
mat[(1,1)] = m11; mat[(1,1)] = m11;
mat[(2,2)] = far / (near - far); mat[(2,2)] = far / (near - far);
mat[(2,3)] = negone; mat[(2,3)] = -(far * near) / (far - near);
mat[(3,2)] = -(far * near) / (far - near); mat[(3,2)] = negone;
mat mat
} }

71
nalgebra-glm/tests/lib.rs Normal file
View File

@ -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);
}