// Copyright 2013 The Lmath Developers. For a full listing of the authors, // refer to the AUTHORS file at the top-level directory of this distribution. // // 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 core::Mat4; use geom::{Plane, Point3}; #[path = "../num_macros.rs"] mod num_macros; #[deriving(Clone, Eq)] pub struct Frustum { left: Plane, right: Plane, bottom: Plane, top: Plane, near: Plane, far: Plane, } #[deriving(Clone, Eq)] pub struct FrustumPoints { near_top_left: Point3, near_top_right: Point3, near_bottom_left: Point3, near_bottom_right: Point3, far_top_left: Point3, far_top_right: Point3, far_bottom_left: Point3, far_bottom_right: Point3, } impl Frustum { /// Constructs a frustum pub fn from_planes(left: Plane, right: Plane, bottom: Plane, top: Plane, near: Plane, far: Plane) -> Frustum { Frustum { left: left, right: right, bottom: bottom, top: top, near: near, far: far, } } /// Extracts frustum planes from a projection matrix pub fn from_matrix(mat: Mat4) -> Frustum { Frustum { left: Plane::from_vec4(mat.row(3).add_v(&mat.row(0)).normalize()), right: Plane::from_vec4(mat.row(3).sub_v(&mat.row(0)).normalize()), bottom: Plane::from_vec4(mat.row(3).add_v(&mat.row(1)).normalize()), top: Plane::from_vec4(mat.row(3).sub_v(&mat.row(1)).normalize()), near: Plane::from_vec4(mat.row(3).add_v(&mat.row(2)).normalize()), far: Plane::from_vec4(mat.row(3).sub_v(&mat.row(2)).normalize()), } } pub fn base() -> Frustum { Frustum { left: Plane::from_abcd( one!(T), zero!(T), zero!(T), one!(T)), right: Plane::from_abcd(-one!(T), zero!(T), zero!(T), one!(T)), bottom: Plane::from_abcd( zero!(T), one!(T), zero!(T), one!(T)), top: Plane::from_abcd( zero!(T), -one!(T), zero!(T), one!(T)), near: Plane::from_abcd( zero!(T), zero!(T), -one!(T), one!(T)), far: Plane::from_abcd( zero!(T), zero!(T), one!(T), one!(T)), } } } impl Frustum { /// Computes where the frustum planes intersect to form corners and returns /// a struct containing the eight resulting position vectors. pub fn to_points(&self) -> FrustumPoints { FrustumPoints { near_top_left: self.near.intersection_3pl(&self.top, &self.left).unwrap(), near_top_right: self.near.intersection_3pl(&self.top, &self.right).unwrap(), near_bottom_left: self.near.intersection_3pl(&self.bottom, &self.left).unwrap(), near_bottom_right: self.near.intersection_3pl(&self.bottom, &self.right).unwrap(), far_top_left: self.far.intersection_3pl(&self.top, &self.left).unwrap(), far_top_right: self.far.intersection_3pl(&self.top, &self.right).unwrap(), far_bottom_left: self.far.intersection_3pl(&self.bottom, &self.left).unwrap(), far_bottom_right: self.far.intersection_3pl(&self.bottom, &self.right).unwrap(), } } } impl> ApproxEq for Frustum { #[inline] pub fn approx_epsilon() -> T { ApproxEq::approx_epsilon::() } #[inline] pub fn approx_eq(&self, other: &Frustum) -> bool { self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) } #[inline] pub fn approx_eq_eps(&self, other: &Frustum, epsilon: &T) -> bool { self.left.approx_eq_eps(&other.left, epsilon) && self.right.approx_eq_eps(&other.right, epsilon) && self.bottom.approx_eq_eps(&other.bottom, epsilon) && self.top.approx_eq_eps(&other.top, epsilon) && self.near.approx_eq_eps(&other.near, epsilon) && self.far.approx_eq_eps(&other.far, epsilon) } }