Add a `perspective3d` free function on the na:: module.
This commit is contained in:
parent
db831f922e
commit
31904cad6f
21
src/na.rs
21
src/na.rs
|
@ -83,6 +83,27 @@ pub fn one<T: 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<N: Real + Cast<f32> + Zero + One>(width: N, height: N, fov: N, znear: N, zfar: N) -> Mat4<N> {
|
||||
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<V>
|
||||
*/
|
||||
|
|
|
@ -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<N: Num + Clone>
|
||||
|
@ -264,25 +263,3 @@ impl<N: Mul<N, N> + Add<N, N>> Mat2MulRhs<N, Vec2<N>> for Vec2<N> {
|
|||
)
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: move this to another file?
|
||||
impl<N: Real + Cast<f32> + Zero + One> mat::Mat4<N> {
|
||||
/// 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<N> {
|
||||
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()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue