diff --git a/src/na.rs b/src/na.rs index aab2cea4..b6b56643 100644 --- a/src/na.rs +++ b/src/na.rs @@ -83,6 +83,27 @@ pub fn one() -> T { // // +/* + * Perspective + */ +/// Computes a projection matrix given the frustrum near plane width, height, the field of +/// view, and the distance to the clipping planes (`znear` and `zfar`). +pub fn perspective3d + Zero + One>(width: N, height: N, fov: N, znear: N, zfar: N) -> Mat4 { + let aspect = width / height; + + let _1: N = one(); + let sy = _1 / (fov * cast(0.5)).tan(); + let sx = -sy / aspect; + let sz = -(zfar + znear) / (znear - zfar); + let tz = zfar * znear * cast(2.0) / (znear - zfar); + + Mat4::new( + sx, zero(), zero(), zero(), + zero(), sy, zero(), zero(), + zero(), zero(), sz, tz, + zero(), zero(), one(), zero()) +} + /* * Translation */ diff --git a/src/structs/spec/mat.rs b/src/structs/spec/mat.rs index 8a1d0446..ccd307c2 100644 --- a/src/structs/spec/mat.rs +++ b/src/structs/spec/mat.rs @@ -1,9 +1,8 @@ use std::num::{Zero, One}; use structs::vec::{Vec2, Vec3, Vec2MulRhs, Vec3MulRhs}; use structs::mat::{Mat1, Mat2, Mat3, Mat3MulRhs, Mat2MulRhs}; -use structs::mat; -use traits::operations::{Inv}; -use traits::structure::{Cast, Row, Col}; +use traits::operations::Inv; +use traits::structure::{Row, Col}; // some specializations: impl @@ -264,25 +263,3 @@ impl + Add> Mat2MulRhs> for Vec2 { ) } } - -// FIXME: move this to another file? -impl + Zero + One> mat::Mat4 { - /// Computes a projection matrix given the frustrum near plane width, height, the field of - /// view, and the distance to the clipping planes (`znear` and `zfar`). - pub fn new_perspective(width: N, height: N, fov: N, znear: N, zfar: N) -> mat::Mat4 { - let aspect = width / height; - - let _1: N = One::one(); - let sy = _1 / (fov * Cast::from(0.5)).tan(); - let sx = -sy / aspect; - let sz = -(zfar + znear) / (znear - zfar); - let tz = zfar * znear * Cast::from(2.0) / (znear - zfar); - - mat::Mat4::new( - sx, Zero::zero(), Zero::zero(), Zero::zero(), - Zero::zero(), sy, Zero::zero(), Zero::zero(), - Zero::zero(), Zero::zero(), sz, tz, - Zero::zero(), Zero::zero(), One::one(), Zero::zero() - ) - } -}