cgmath/src/projection.rs

222 lines
7.7 KiB
Rust
Raw Normal View History

2014-05-26 17:10:04 +00:00
// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors,
2015-03-14 02:49:46 +00:00
// refer to the Cargo.toml file at the top-level directory of this distribution.
2013-09-03 05:23:56 +00:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use rust_num::{Zero, One};
2015-04-25 03:28:31 +00:00
use rust_num::traits::cast;
2015-04-05 01:19:11 +00:00
2015-05-06 08:10:26 +00:00
use angle::{Angle, Rad, tan, cot};
2015-05-06 08:27:52 +00:00
use matrix::Matrix4;
2015-04-05 01:19:11 +00:00
use num::BaseFloat;
2013-09-03 05:23:56 +00:00
2013-09-05 06:44:27 +00:00
/// Create a perspective projection matrix.
2013-09-03 05:23:56 +00:00
///
2013-09-05 06:44:27 +00:00
/// This is the equivalent to the [gluPerspective]
/// (http://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml) function.
pub fn perspective<S: BaseFloat, A: Angle<S>>(fovy: A, aspect: S, near: S, far: S) -> Matrix4<S> {
2013-09-05 06:44:27 +00:00
PerspectiveFov {
fovy: fovy,
aspect: aspect,
near: near,
far: far,
2015-05-06 08:27:52 +00:00
}.into()
2013-09-03 05:23:56 +00:00
}
2013-09-05 06:44:27 +00:00
/// Create a perspective matrix from a view frustrum.
2013-09-03 05:23:56 +00:00
///
/// This is the equivalent of the now deprecated [glFrustrum]
/// (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function.
pub fn frustum<S: BaseFloat>(left: S, right: S, bottom: S, top: S, near: S, far: S) -> Matrix4<S> {
2013-09-05 06:44:27 +00:00
Perspective {
left: left,
right: right,
bottom: bottom,
top: top,
near: near,
far: far,
2015-05-06 08:27:52 +00:00
}.into()
2013-09-03 05:23:56 +00:00
}
2013-09-05 06:44:27 +00:00
/// Create an orthographic projection matrix.
2013-09-03 05:23:56 +00:00
///
/// This is the equivalent of the now deprecated [glOrtho]
/// (http://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml) function.
pub fn ortho<S: BaseFloat>(left: S, right: S, bottom: S, top: S, near: S, far: S) -> Matrix4<S> {
2013-09-05 06:44:27 +00:00
Ortho {
left: left,
right: right,
bottom: bottom,
top: top,
near: near,
far: far,
2015-05-06 08:27:52 +00:00
}.into()
2013-09-03 05:23:56 +00:00
}
2013-09-04 04:39:21 +00:00
/// A perspective projection based on a vertical field-of-view angle.
2015-01-03 21:29:26 +00:00
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
2013-09-05 06:44:27 +00:00
pub struct PerspectiveFov<S, A> {
2014-04-01 11:00:17 +00:00
pub fovy: A,
pub aspect: S,
pub near: S,
pub far: S,
2013-09-04 04:39:21 +00:00
}
2014-05-26 17:10:04 +00:00
impl<S: BaseFloat, A: Angle<S>> PerspectiveFov<S, A> {
2013-09-05 06:44:27 +00:00
pub fn to_perspective(&self) -> Perspective<S> {
2015-01-09 23:16:39 +00:00
let angle = self.fovy.div_s(cast(2i8).unwrap());
2015-05-06 08:10:26 +00:00
let angle: Rad<_> = angle.into();
let ymax = self.near * tan(angle);
2013-09-05 06:44:27 +00:00
let xmax = ymax * self.aspect;
Perspective {
left: -xmax,
right: xmax,
bottom: -ymax,
top: ymax,
near: self.near.clone(),
far: self.far.clone(),
2013-09-04 04:39:21 +00:00
}
}
}
2015-05-06 08:27:52 +00:00
impl<S: BaseFloat, A: Angle<S>> From<PerspectiveFov<S, A>> for Matrix4<S> {
fn from(persp: PerspectiveFov<S, A>) -> Matrix4<S> {
let half_turn: A = Angle::turn_div_2();
2013-09-05 06:44:27 +00:00
assert!(persp.fovy > A::zero(), "The vertical field of view cannot be below zero, found: {:?}", persp.fovy);
2015-05-06 08:27:52 +00:00
assert!(persp.fovy < half_turn, "The vertical field of view cannot be greater than a half turn, found: {:?}", persp.fovy);
assert!(persp.aspect > S::zero(), "The aspect ratio cannot be below zero, found: {:?}", persp.aspect);
assert!(persp.near > S::zero(), "The near plane distance cannot be below zero, found: {:?}", persp.near);
assert!(persp.far > S::zero(), "The far plane distance cannot be below zero, found: {:?}", persp.far);
2015-05-06 08:27:52 +00:00
assert!(persp.far > persp.near, "The far plane cannot be closer than the near plane, found: far: {:?}, near: {:?}", persp.far, persp.near);
2013-09-05 06:44:27 +00:00
2015-05-06 08:27:52 +00:00
let f: Rad<_> = persp.fovy.div_s(cast(2i8).unwrap()).into();
2015-05-06 08:10:26 +00:00
let f = cot(f);
2015-01-09 23:16:39 +00:00
let two: S = cast(2i8).unwrap();
2013-09-05 06:44:27 +00:00
2015-05-06 08:27:52 +00:00
let c0r0 = f / persp.aspect;
let c0r1 = S::zero();
let c0r2 = S::zero();
let c0r3 = S::zero();
2013-09-05 06:44:27 +00:00
let c1r0 = S::zero();
2013-09-05 06:44:27 +00:00
let c1r1 = f;
let c1r2 = S::zero();
let c1r3 = S::zero();
2013-09-05 06:44:27 +00:00
let c2r0 = S::zero();
let c2r1 = S::zero();
2015-05-06 08:27:52 +00:00
let c2r2 = (persp.far + persp.near) / (persp.near - persp.far);
let c2r3 = -S::one();
2013-09-05 06:44:27 +00:00
let c3r0 = S::zero();
let c3r1 = S::zero();
2015-05-06 08:27:52 +00:00
let c3r2 = (two * persp.far * persp.near) / (persp.near - persp.far);
let c3r3 = S::zero();
2013-09-05 06:44:27 +00:00
Matrix4::new(c0r0, c0r1, c0r2, c0r3,
2014-04-14 01:41:29 +00:00
c1r0, c1r1, c1r2, c1r3,
c2r0, c2r1, c2r2, c2r3,
c3r0, c3r1, c3r2, c3r3)
2013-09-04 04:39:21 +00:00
}
}
/// A perspective projection with arbitrary left/right/bottom/top distances
2015-01-03 21:29:26 +00:00
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
2013-09-04 04:39:21 +00:00
pub struct Perspective<S> {
2015-03-17 21:59:43 +00:00
pub left: S,
pub right: S,
pub bottom: S,
pub top: S,
pub near: S,
pub far: S,
2013-09-04 04:39:21 +00:00
}
impl<S: BaseFloat> From<Perspective<S>> for Matrix4<S> {
2015-05-06 08:27:52 +00:00
fn from(persp: Perspective<S>) -> Matrix4<S> {
assert!(persp.left <= persp.right, "`left` cannot be greater than `right`, found: left: {:?} right: {:?}", persp.left, persp.right);
assert!(persp.bottom <= persp.top, "`bottom` cannot be greater than `top`, found: bottom: {:?} top: {:?}", persp.bottom, persp.top);
assert!(persp.near <= persp.far, "`near` cannot be greater than `far`, found: near: {:?} far: {:?}", persp.near, persp.far);
2013-09-05 06:44:27 +00:00
2015-01-09 23:16:39 +00:00
let two: S = cast(2i8).unwrap();
2013-09-19 04:57:36 +00:00
2015-05-06 08:27:52 +00:00
let c0r0 = (two * persp.near) / (persp.right - persp.left);
let c0r1 = S::zero();
let c0r2 = S::zero();
let c0r3 = S::zero();
2013-09-05 06:44:27 +00:00
let c1r0 = S::zero();
2015-05-06 08:27:52 +00:00
let c1r1 = (two * persp.near) / (persp.top - persp.bottom);
let c1r2 = S::zero();
let c1r3 = S::zero();
2013-09-05 06:44:27 +00:00
2015-05-06 08:27:52 +00:00
let c2r0 = (persp.right + persp.left) / (persp.right - persp.left);
let c2r1 = (persp.top + persp.bottom) / (persp.top - persp.bottom);
let c2r2 = -(persp.far + persp.near) / (persp.far - persp.near);
let c2r3 = -S::one();
2013-09-05 06:44:27 +00:00
let c3r0 = S::zero();
let c3r1 = S::zero();
2015-05-06 08:27:52 +00:00
let c3r2 = -(two * persp.far * persp.near) / (persp.far - persp.near);
let c3r3 = S::zero();
2013-09-05 06:44:27 +00:00
Matrix4::new(c0r0, c0r1, c0r2, c0r3,
2014-04-14 01:41:29 +00:00
c1r0, c1r1, c1r2, c1r3,
c2r0, c2r1, c2r2, c2r3,
c3r0, c3r1, c3r2, c3r3)
2013-09-04 04:39:21 +00:00
}
}
/// An orthographic projection with arbitrary left/right/bottom/top distances
2015-01-03 21:29:26 +00:00
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
2013-09-04 04:39:21 +00:00
pub struct Ortho<S> {
pub left: S,
pub right: S,
pub bottom: S,
pub top: S,
pub near: S,
pub far: S,
2013-09-04 04:39:21 +00:00
}
2015-05-06 08:27:52 +00:00
impl<S: BaseFloat> From<Ortho<S>> for Matrix4<S> {
fn from(ortho: Ortho<S>) -> Matrix4<S> {
2015-01-09 23:16:39 +00:00
let two: S = cast(2i8).unwrap();
2013-09-19 04:57:36 +00:00
2015-05-06 08:27:52 +00:00
let c0r0 = two / (ortho.right - ortho.left);
let c0r1 = S::zero();
let c0r2 = S::zero();
let c0r3 = S::zero();
2013-09-05 06:44:27 +00:00
let c1r0 = S::zero();
2015-05-06 08:27:52 +00:00
let c1r1 = two / (ortho.top - ortho.bottom);
let c1r2 = S::zero();
let c1r3 = S::zero();
2013-09-05 06:44:27 +00:00
let c2r0 = S::zero();
let c2r1 = S::zero();
2015-05-06 08:27:52 +00:00
let c2r2 = -two / (ortho.far - ortho.near);
let c2r3 = S::zero();
2013-09-05 06:44:27 +00:00
2015-05-06 08:27:52 +00:00
let c3r0 = -(ortho.right + ortho.left) / (ortho.right - ortho.left);
let c3r1 = -(ortho.top + ortho.bottom) / (ortho.top - ortho.bottom);
let c3r2 = -(ortho.far + ortho.near) / (ortho.far - ortho.near);
let c3r3 = S::one();
2013-09-05 06:44:27 +00:00
Matrix4::new(c0r0, c0r1, c0r2, c0r3,
2014-04-14 01:41:29 +00:00
c1r0, c1r1, c1r2, c1r3,
c2r0, c2r1, c2r2, c2r3,
c3r0, c3r1, c3r2, c3r3)
2013-09-04 04:39:21 +00:00
}
}