Merge pull request #12 from cyndis/master

Fix the projection::perspective method
This commit is contained in:
Brendan Zabarauskas 2013-04-07 03:21:23 -07:00
commit 8442e16db6

View file

@ -8,12 +8,16 @@ use mat::{Mat4, BaseMat4};
/** /**
* Create a perspective projection matrix * Create a perspective projection matrix
* *
* Note: the fovy parameter should be specified in degrees.
*
* This is the equivalent of the gluPerspective function, the algorithm of which * This is the equivalent of the gluPerspective function, the algorithm of which
* can be found [here](http://www.opengl.org/wiki/GluPerspective_code). * can be found [here](http://www.opengl.org/wiki/GluPerspective_code).
*/ */
#[inline(always)] #[inline(always)]
pub fn perspective<T:Copy + Float + Zero + One + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4<T> { pub fn perspective<T:Copy + Float + Zero + One + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4<T> {
let ymax = near * tan(radians(fovy)); let _2: T = num::cast(2);
let ymax = near * tan(radians(fovy / _2));
let xmax = ymax * aspectRatio; let xmax = ymax * aspectRatio;
frustum(-xmax, xmax, -ymax, ymax, near, far) frustum(-xmax, xmax, -ymax, ymax, near, far)
@ -55,4 +59,23 @@ pub fn frustum<T:Copy + Float + Zero + One + FuzzyEq<T> + Add<T,T> + Sub<T,T> +
c1r0, c1r1, c1r2, c1r3, c1r0, c1r1, c1r2, c1r3,
c2r0, c2r1, c2r2, c2r3, c2r0, c2r1, c2r2, c2r3,
c3r0, c3r1, c3r2, c3r3) c3r0, c3r1, c3r2, c3r3)
} }
/**
* Create an orthographic projection matrix
*
* This is the equivalent of the now deprecated [glOrtho]
* (http://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml) function.
*/
#[inline(always)]
pub fn orthographic<T:Copy + Float + Zero + One + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4<T> {
let _0: T = num::cast(0);
let _1: T = num::cast(1);
let _2: T = num::cast(2);
BaseMat4::new(_2 / (right - left), _0, _0, _0,
_0, _2 / (top - bottom), _0, _0,
_0, _0, -_2 / (far - near), _0,
-(right + left) / (right - left), -(top + bottom) / (top - bottom),
-(far + near) / (far - near), _1)
}