2012-12-17 07:25:41 +00:00
|
|
|
use numeric::funs::*;
|
2012-12-16 05:05:04 +00:00
|
|
|
use numeric::types::angle::Angle;
|
|
|
|
use numeric::types::float::Float;
|
|
|
|
use numeric::types::number::Number;
|
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
use mat::Mat4;
|
2012-09-07 10:48:47 +00:00
|
|
|
|
2012-11-26 12:45:55 +00:00
|
|
|
/**
|
|
|
|
* Create a perspective projection matrix
|
|
|
|
*
|
|
|
|
* This is the equivalent of the gluPerspective function, the algorithm of which
|
|
|
|
* can be found [here](http://www.opengl.org/wiki/GluPerspective_code).
|
|
|
|
*/
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-12-03 01:10:14 +00:00
|
|
|
pub pure fn perspective<T:Copy Float, A:Angle<T>>(fovy: A, aspectRatio: T, near: T, far: T) -> Mat4<T> {
|
2012-11-29 05:59:45 +00:00
|
|
|
let ymax = near * tan(&fovy.to_radians());
|
2012-09-07 10:48:47 +00:00
|
|
|
let xmax = ymax * aspectRatio;
|
2012-11-26 01:52:48 +00:00
|
|
|
|
|
|
|
frustum(-xmax, xmax, -ymax, ymax, near, far)
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-26 12:45:55 +00:00
|
|
|
/**
|
|
|
|
* Define a view frustrum
|
|
|
|
*
|
|
|
|
* This is the equivalent of the now deprecated [glFrustrum]
|
|
|
|
* (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function.
|
|
|
|
*/
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-12-03 01:10:14 +00:00
|
|
|
pub pure fn frustum<T:Copy Float>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4<T> {
|
2012-12-03 06:19:53 +00:00
|
|
|
let _0: T = Number::from(0);
|
|
|
|
let _2: T = Number::from(2);
|
2012-09-07 10:48:47 +00:00
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
let c0r0 = (_2 * near) / (right - left);
|
2012-11-04 04:28:17 +00:00
|
|
|
let c0r1 = _0;
|
|
|
|
let c0r2 = _0;
|
|
|
|
let c0r3 = _0;
|
|
|
|
let c1r0 = _0;
|
2012-11-15 02:23:39 +00:00
|
|
|
let c1r1 = (_2 * near) / (top - bottom);
|
2012-11-04 04:28:17 +00:00
|
|
|
let c1r2 = _0;
|
|
|
|
let c1r3 = _0;
|
2012-11-15 02:23:39 +00:00
|
|
|
let c2r0 = (right + left) / (right - left);
|
|
|
|
let c2r1 = (top + bottom) / (top - bottom);
|
|
|
|
let c2r2 = -(far + near) / (far - near);
|
2012-12-03 06:19:53 +00:00
|
|
|
let c2r3 = Number::from(-1);
|
2012-11-04 04:28:17 +00:00
|
|
|
let c3r0 = _0;
|
|
|
|
let c3r1 = _0;
|
2012-11-15 02:23:39 +00:00
|
|
|
let c3r2 = -(_2 * far * near) / (far - near);
|
2012-11-04 04:28:17 +00:00
|
|
|
let c3r3 = _0;
|
|
|
|
|
2012-11-26 01:52:48 +00:00
|
|
|
Mat4::new(c0r0, c0r1, c0r2, c0r3,
|
|
|
|
c1r0, c1r1, c1r2, c1r3,
|
|
|
|
c2r0, c2r1, c2r2, c2r3,
|
|
|
|
c3r0, c3r1, c3r2, c3r3)
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|