cgmath/src/projection.rs

98 lines
2.6 KiB
Rust
Raw Normal View History

2013-01-27 22:22:15 +00:00
use numeric::*;
2013-04-02 05:12:13 +00:00
use mat::{Mat4, BaseMat4};
2012-09-07 10:48:47 +00:00
2012-11-26 12:45:55 +00:00
/**
* Create a perspective projection matrix
*
2013-04-07 09:55:03 +00:00
* Note: the fovy parameter should be specified in degrees.
*
2012-11-26 12:45:55 +00:00
* This is the equivalent of the gluPerspective function, the algorithm of which
* can be found [here](http://www.opengl.org/wiki/GluPerspective_code).
*/
#[inline(always)]
pub fn perspective<T:Copy + Float>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4<T> {
2013-04-07 09:55:03 +00:00
let _2: T = num::cast(2);
let ymax = near * tan(radians(fovy / _2));
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.
*/
#[inline(always)]
pub fn frustum<T:Copy + Float>(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);
let c0r0 = (_2 * near) / (right - left);
2012-11-04 04:28:17 +00:00
let c0r1 = _0;
let c0r2 = _0;
let c0r3 = _0;
2012-11-04 04:28:17 +00:00
let c1r0 = _0;
let c1r1 = (_2 * near) / (top - bottom);
2012-11-04 04:28:17 +00:00
let c1r2 = _0;
let c1r3 = _0;
let c2r0 = (right + left) / (right - left);
let c2r1 = (top + bottom) / (top - bottom);
let c2r2 = -(far + near) / (far - near);
let c2r3 = -_1;
2012-11-04 04:28:17 +00:00
let c3r0 = _0;
let c3r1 = _0;
let c3r2 = -(_2 * far * near) / (far - near);
2012-11-04 04:28:17 +00:00
let c3r3 = _0;
2013-04-02 05:12:13 +00:00
BaseMat4::new(c0r0, c0r1, c0r2, c0r3,
c1r0, c1r1, c1r2, c1r3,
c2r0, c2r1, c2r2, c2r3,
c3r0, c3r1, c3r2, c3r3)
2013-04-07 10:06:26 +00:00
}
/**
* 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 ortho<T:Copy + Float>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4<T> {
2013-04-07 10:06:26 +00:00
let _0: T = num::cast(0);
let _1: T = num::cast(1);
let _2: T = num::cast(2);
let c0r0 = _2 / (right - left);
let c0r1 = _0;
let c0r2 = _0;
2013-04-07 13:18:32 +00:00
let c0r3 = _0;
let c1r0 = _0;
let c1r1 = _2 / (top - bottom);
let c1r2 = _0;
2013-04-07 13:18:32 +00:00
let c1r3 = _0;
let c2r0 = _0;
let c2r1 = _0;
let c2r2 = -_2 / (far - near);
2013-04-07 13:18:32 +00:00
let c2r3 = _0;
2013-04-07 13:18:32 +00:00
let c3r0 = -(right + left) / (right - left);
let c3r1 = -(top + bottom) / (top - bottom);
let c3r2 = -(far + near) / (far - near);
let c3r3 = _1;
BaseMat4::new(c0r0, c0r1, c0r2, c0r3,
c1r0, c1r1, c1r2, c1r3,
c2r0, c2r1, c2r2, c2r3,
c3r0, c3r1, c3r2, c3r3)
2013-04-07 10:06:26 +00:00
}