Simplify projection code
This commit is contained in:
parent
6534855673
commit
aef1d153a1
2 changed files with 141 additions and 161 deletions
|
@ -101,6 +101,10 @@ impl<S: Clone + Float> Angle<S> for Deg<S> {
|
|||
#[inline] pub fn tan<S: Clone + Float, A: Angle<S>>(theta: A) -> S { theta.to_rad().s.tan() }
|
||||
#[inline] pub fn sin_cos<S: Clone + Float, A: Angle<S>>(theta: A) -> (S, S) { theta.to_rad().s.sin_cos() }
|
||||
|
||||
#[inline] pub fn cot<S: Clone + Float, A: Angle<S>>(theta: A) -> S { tan(theta).recip() }
|
||||
#[inline] pub fn sec<S: Clone + Float, A: Angle<S>>(theta: A) -> S { cos(theta).recip() }
|
||||
#[inline] pub fn csc<S: Clone + Float, A: Angle<S>>(theta: A) -> S { sin(theta).recip() }
|
||||
|
||||
#[inline] pub fn asin<S: Clone + Float, A: Angle<S>>(s: S) -> A { Angle::from(rad(s.asin())) }
|
||||
#[inline] pub fn acos<S: Clone + Float, A: Angle<S>>(s: S) -> A { Angle::from(rad(s.acos())) }
|
||||
#[inline] pub fn atan<S: Clone + Float, A: Angle<S>>(s: S) -> A { Angle::from(rad(s.atan())) }
|
||||
|
|
|
@ -15,102 +15,66 @@
|
|||
|
||||
use std::num::{zero, one};
|
||||
|
||||
use angle::{Angle, Rad, rad, tan};
|
||||
use matrix::Mat4;
|
||||
use angle::{Angle, rad, tan, cot};
|
||||
use matrix::{Mat4, ToMat4};
|
||||
use util::two;
|
||||
|
||||
/// 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
|
||||
/// can be found [here](http://www.opengl.org/wiki/GluPerspective_code).
|
||||
pub fn perspective<S: Clone + Float>(fovy: Rad<S>, aspectRatio: S, near: S, far: S) -> Mat4<S> {
|
||||
let ymax = near * tan(fovy.div_s(two::<S>()));
|
||||
let xmax = ymax * aspectRatio;
|
||||
|
||||
frustum(-xmax, xmax, -ymax, ymax, near, far)
|
||||
/// This is the equivalent to the [gluPerspective]
|
||||
/// (http://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml) function.
|
||||
pub fn perspective<S: Clone + Float, A: Angle<S>>(fovy: A, aspect: S, near: S, far: S) -> Mat4<S> {
|
||||
PerspectiveFov {
|
||||
fovy: fovy,
|
||||
aspect: aspect,
|
||||
near: near,
|
||||
far: far,
|
||||
}.to_mat4()
|
||||
}
|
||||
|
||||
/// Define a view frustrum
|
||||
/// Create a perspective matrix from a view frustrum.
|
||||
///
|
||||
/// This is the equivalent of the now deprecated [glFrustrum]
|
||||
/// (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function.
|
||||
pub fn frustum<S: Clone + Float>(left: S, right: S, bottom: S, top: S, near: S, far: S) -> Mat4<S> {
|
||||
let c0r0 = (two::<S>() * near) / (right - left);
|
||||
let c0r1 = zero();
|
||||
let c0r2 = zero();
|
||||
let c0r3 = zero();
|
||||
|
||||
let c1r0 = zero();
|
||||
let c1r1 = (two::<S>() * near) / (top - bottom);
|
||||
let c1r2 = zero();
|
||||
let c1r3 = zero();
|
||||
|
||||
let c2r0 = (right + left) / (right - left);
|
||||
let c2r1 = (top + bottom) / (top - bottom);
|
||||
let c2r2 = -(far + near) / (far - near);
|
||||
let c2r3 = -one::<S>();
|
||||
|
||||
let c3r0 = zero();
|
||||
let c3r1 = zero();
|
||||
let c3r2 = -(two::<S>() * far * near) / (far - near);
|
||||
let c3r3 = zero();
|
||||
|
||||
Mat4::new(c0r0, c0r1, c0r2, c0r3,
|
||||
c1r0, c1r1, c1r2, c1r3,
|
||||
c2r0, c2r1, c2r2, c2r3,
|
||||
c3r0, c3r1, c3r2, c3r3)
|
||||
Perspective {
|
||||
left: left,
|
||||
right: right,
|
||||
bottom: bottom,
|
||||
top: top,
|
||||
near: near,
|
||||
far: far,
|
||||
}.to_mat4()
|
||||
}
|
||||
|
||||
/// Create an orthographic projection matrix
|
||||
/// 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.
|
||||
pub fn ortho<S: Clone + Float>(left: S, right: S, bottom: S, top: S, near: S, far: S) -> Mat4<S> {
|
||||
let c0r0 = two::<S>() / (right - left);
|
||||
let c0r1 = zero();
|
||||
let c0r2 = zero();
|
||||
let c0r3 = zero();
|
||||
|
||||
let c1r0 = zero();
|
||||
let c1r1 = two::<S>() / (top - bottom);
|
||||
let c1r2 = zero();
|
||||
let c1r3 = zero();
|
||||
|
||||
let c2r0 = zero();
|
||||
let c2r1 = zero();
|
||||
let c2r2 = -two::<S>() / (far - near);
|
||||
let c2r3 = zero();
|
||||
|
||||
let c3r0 = -(right + left) / (right - left);
|
||||
let c3r1 = -(top + bottom) / (top - bottom);
|
||||
let c3r2 = -(far + near) / (far - near);
|
||||
let c3r3 = one();
|
||||
|
||||
Mat4::new(c0r0, c0r1, c0r2, c0r3,
|
||||
c1r0, c1r1, c1r2, c1r3,
|
||||
c2r0, c2r1, c2r2, c2r3,
|
||||
c3r0, c3r1, c3r2, c3r3)
|
||||
Ortho {
|
||||
left: left,
|
||||
right: right,
|
||||
bottom: bottom,
|
||||
top: top,
|
||||
near: near,
|
||||
far: far,
|
||||
}.to_mat4()
|
||||
}
|
||||
|
||||
pub trait Projection<S> {
|
||||
fn if_valid<U:Clone>(&self, f: &fn() -> U) -> Result<U, ~str>;
|
||||
fn to_mat4(&self) -> Result<Mat4<S>, ~str>;
|
||||
}
|
||||
pub trait Projection<S>: ToMat4<S> {}
|
||||
|
||||
/// A perspective projection based on a vertical field-of-view angle.
|
||||
#[deriving(Clone, Eq)]
|
||||
pub struct PerspectiveFov<S> {
|
||||
fovy: Rad<S>,
|
||||
pub struct PerspectiveFov<S, A> {
|
||||
fovy: A,
|
||||
aspect: S,
|
||||
near: S,
|
||||
far: S,
|
||||
}
|
||||
|
||||
impl<S: Clone + Float> PerspectiveFov<S> {
|
||||
pub fn to_perspective(&self) -> Result<Perspective<S>, ~str> {
|
||||
do self.if_valid {
|
||||
impl<S: Clone + Float, A: Angle<S>> PerspectiveFov<S, A> {
|
||||
pub fn to_perspective(&self) -> Perspective<S> {
|
||||
let angle = self.fovy.div_s(two::<S>());
|
||||
let ymax = self.near * tan(angle);
|
||||
let xmax = ymax * self.aspect;
|
||||
|
@ -124,27 +88,49 @@ impl<S: Clone + Float> PerspectiveFov<S> {
|
|||
far: self.far.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Clone + Float, A: Angle<S>> ToMat4<S> for PerspectiveFov<S, A> {
|
||||
fn to_mat4(&self) -> Mat4<S> {
|
||||
let half_turn: A = Angle::from(rad::<S>(Real::frac_pi_2()));
|
||||
|
||||
assert!(self.fovy < zero(), "The vertical field of view cannot be below zero, found: %?", self.fovy);
|
||||
assert!(self.fovy > half_turn, "The vertical field of view cannot be greater than a half turn, found: %?", self.fovy);
|
||||
assert!(self.aspect < zero(), "The aspect ratio cannot be below zero, found: %?", self.aspect);
|
||||
assert!(self.near < zero(), "The near plane distance cannot be below zero, found: %?", self.near);
|
||||
assert!(self.far < zero(), "The far plane distance cannot be below zero, found: %?", self.far);
|
||||
assert!(self.far < self.near, "The far plane cannot be closer than the near plane, found: far: %?, near: %?", self.far, self.near);
|
||||
|
||||
let f = cot(self.fovy.div_s(two::<S>()));
|
||||
|
||||
let c0r0 = f / self.aspect;
|
||||
let c0r1 = zero();
|
||||
let c0r2 = zero();
|
||||
let c0r3 = zero();
|
||||
|
||||
let c1r0 = zero();
|
||||
let c1r1 = f;
|
||||
let c1r2 = zero();
|
||||
let c1r3 = zero();
|
||||
|
||||
let c2r0 = zero();
|
||||
let c2r1 = zero();
|
||||
let c2r2 = (self.far + self.near) / (self.near - self.far);
|
||||
let c2r3 = -one::<S>();
|
||||
|
||||
let c3r0 = zero();
|
||||
let c3r1 = zero();
|
||||
let c3r2 = (two::<S>() * self.far * self.near) / (self.near - self.far);
|
||||
let c3r3 = zero();
|
||||
|
||||
Mat4::new(c0r0, c0r1, c0r2, c0r3,
|
||||
c1r0, c1r1, c1r2, c1r3,
|
||||
c2r0, c2r1, c2r2, c2r3,
|
||||
c3r0, c3r1, c3r2, c3r3)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Clone + Float> Projection<S> for PerspectiveFov<S> {
|
||||
fn if_valid<U:Clone>(&self, f: &fn() -> U) -> Result<U, ~str> {
|
||||
let half_turn: Rad<S> = rad(Real::frac_pi_2());
|
||||
cond! (
|
||||
(self.fovy < zero()) { Err(fmt!("The vertical field of view cannot be below zero, found: %?", self.fovy)) }
|
||||
(self.fovy > half_turn) { Err(fmt!("The vertical field of view cannot be greater than a half turn, found: %?", self.fovy)) }
|
||||
(self.aspect < zero()) { Err(fmt!("The aspect ratio cannot be below zero, found: %?", self.aspect)) }
|
||||
(self.near < zero()) { Err(fmt!("The near plane distance cannot be below zero, found: %?", self.near)) }
|
||||
(self.far < zero()) { Err(fmt!("The far plane distance cannot be below zero, found: %?", self.far)) }
|
||||
(self.far < self.near) { Err(fmt!("The far plane cannot be closer than the near plane, found: far: %?, near: %?", self.far, self.near)) }
|
||||
_ { Ok(f()) }
|
||||
)
|
||||
}
|
||||
|
||||
fn to_mat4(&self) -> Result<Mat4<S>, ~str> {
|
||||
do self.to_perspective().chain |proj| { proj.to_mat4() }
|
||||
}
|
||||
}
|
||||
impl<S: Clone + Float, A: Angle<S>> Projection<S> for PerspectiveFov<S, A>;
|
||||
|
||||
/// A perspective projection with arbitrary left/right/bottom/top distances
|
||||
#[deriving(Clone, Eq)]
|
||||
|
@ -154,18 +140,12 @@ pub struct Perspective<S> {
|
|||
near: S, far: S,
|
||||
}
|
||||
|
||||
impl<S: Clone + Float> Projection<S> for Perspective<S> {
|
||||
fn if_valid<U:Clone>(&self, f: &fn() -> U) -> Result<U, ~str> {
|
||||
cond! (
|
||||
(self.left > self.right) { Err(fmt!("`left` cannot be greater than `right`, found: left: %? right: %?", self.left, self.right)) }
|
||||
(self.bottom > self.top) { Err(fmt!("`bottom` cannot be greater than `top`, found: bottom: %? top: %?", self.bottom, self.top)) }
|
||||
(self.near > self.far) { Err(fmt!("`near` cannot be greater than `far`, found: near: %? far: %?", self.near, self.far)) }
|
||||
_ { Ok(f()) }
|
||||
)
|
||||
}
|
||||
impl<S: Clone + Float> ToMat4<S> for Perspective<S> {
|
||||
fn to_mat4(&self) -> Mat4<S> {
|
||||
assert!(self.left > self.right, "`left` cannot be greater than `right`, found: left: %? right: %?", self.left, self.right);
|
||||
assert!(self.bottom > self.top, "`bottom` cannot be greater than `top`, found: bottom: %? top: %?", self.bottom, self.top);
|
||||
assert!(self.near > self.far, "`near` cannot be greater than `far`, found: near: %? far: %?", self.near, self.far);
|
||||
|
||||
fn to_mat4(&self) -> Result<Mat4<S>, ~str> {
|
||||
do self.if_valid {
|
||||
let c0r0 = (two::<S>() * self.near) / (self.right - self.left);
|
||||
let c0r1 = zero();
|
||||
let c0r2 = zero();
|
||||
|
@ -191,9 +171,10 @@ impl<S: Clone + Float> Projection<S> for Perspective<S> {
|
|||
c2r0, c2r1, c2r2, c2r3,
|
||||
c3r0, c3r1, c3r2, c3r3)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Clone + Float> Projection<S> for Perspective<S>;
|
||||
|
||||
/// An orthographic projection with arbitrary left/right/bottom/top distances
|
||||
#[deriving(Clone, Eq)]
|
||||
pub struct Ortho<S> {
|
||||
|
@ -202,18 +183,12 @@ pub struct Ortho<S> {
|
|||
near: S, far: S,
|
||||
}
|
||||
|
||||
impl<S: Clone + Float> Projection<S> for Ortho<S> {
|
||||
fn if_valid<U:Clone>(&self, f: &fn() -> U) -> Result<U, ~str> {
|
||||
cond! (
|
||||
(self.left > self.right) { Err(fmt!("`left` cannot be greater than `right`, found: left: %? right: %?", self.left, self.right)) }
|
||||
(self.bottom > self.top) { Err(fmt!("`bottom` cannot be greater than `top`, found: bottom: %? top: %?", self.bottom, self.top)) }
|
||||
(self.near > self.far) { Err(fmt!("`near` cannot be greater than `far`, found: near: %? far: %?", self.near, self.far)) }
|
||||
_ { Ok(f()) }
|
||||
)
|
||||
}
|
||||
impl<S: Clone + Float> ToMat4<S> for Ortho<S> {
|
||||
fn to_mat4(&self) -> Mat4<S> {
|
||||
assert!(self.left > self.right, "`left` cannot be greater than `right`, found: left: %? right: %?", self.left, self.right);
|
||||
assert!(self.bottom > self.top, "`bottom` cannot be greater than `top`, found: bottom: %? top: %?", self.bottom, self.top);
|
||||
assert!(self.near > self.far, "`near` cannot be greater than `far`, found: near: %? far: %?", self.near, self.far);
|
||||
|
||||
fn to_mat4(&self) -> Result<Mat4<S>, ~str> {
|
||||
do self.if_valid {
|
||||
let c0r0 = two::<S>() / (self.right - self.left);
|
||||
let c0r1 = zero();
|
||||
let c0r2 = zero();
|
||||
|
@ -239,5 +214,6 @@ impl<S: Clone + Float> Projection<S> for Ortho<S> {
|
|||
c2r0, c2r1, c2r2, c2r3,
|
||||
c3r0, c3r1, c3r2, c3r3)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Clone + Float> Projection<S> for Ortho<S>;
|
||||
|
|
Loading…
Reference in a new issue