2018-02-02 19:26:35 +08:00
|
|
|
#[cfg(feature = "arbitrary")]
|
2016-12-05 05:44:42 +08:00
|
|
|
use quickcheck::{Arbitrary, Gen};
|
|
|
|
use rand::{Rand, Rng};
|
|
|
|
|
2017-05-04 10:02:30 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
2017-08-03 01:37:44 +08:00
|
|
|
use serde;
|
|
|
|
use std::fmt;
|
2017-05-04 10:02:30 +08:00
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
use alga::general::Real;
|
|
|
|
|
2018-05-19 23:15:15 +08:00
|
|
|
use base::{Matrix4, Scalar, Vector, Vector3};
|
|
|
|
use base::dimension::U3;
|
|
|
|
use base::storage::Storage;
|
|
|
|
use base::helper;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
use geometry::Point3;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
/// A 3D perspective projection stored as an homogeneous 4x4 matrix.
|
2017-08-03 01:37:44 +08:00
|
|
|
pub struct Perspective3<N: Scalar> {
|
2018-02-02 19:26:35 +08:00
|
|
|
matrix: Matrix4<N>,
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
impl<N: Real> Copy for Perspective3<N> {}
|
2017-08-03 01:37:44 +08:00
|
|
|
|
|
|
|
impl<N: Real> Clone for Perspective3<N> {
|
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Perspective3::from_matrix_unchecked(self.matrix.clone())
|
2017-05-04 10:02:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real> fmt::Debug for Perspective3<N> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
self.matrix.fmt(f)
|
2017-05-04 10:02:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real> PartialEq for Perspective3<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn eq(&self, right: &Self) -> bool {
|
|
|
|
self.matrix == right.matrix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
|
|
|
impl<N: Real + serde::Serialize> serde::Serialize for Perspective3<N> {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
|
|
|
self.matrix.serialize(serializer)
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde-serialize")]
|
|
|
|
impl<'a, N: Real + serde::Deserialize<'a>> serde::Deserialize<'a> for Perspective3<N> {
|
|
|
|
fn deserialize<Des>(deserializer: Des) -> Result<Self, Des::Error>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
Des: serde::Deserializer<'a>,
|
|
|
|
{
|
|
|
|
let matrix = Matrix4::<N>::deserialize(deserializer)?;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
Ok(Perspective3::from_matrix_unchecked(matrix))
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<N: Real> Perspective3<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Creates a new perspective matrix from the aspect ratio, y field of view, and near/far planes.
|
|
|
|
pub fn new(aspect: N, fovy: N, znear: N, zfar: N) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
|
|
|
!relative_eq!(zfar - znear, N::zero()),
|
|
|
|
"The near-plane and far-plane must not be superimposed."
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
!relative_eq!(aspect, N::zero()),
|
|
|
|
"The apsect ratio must not be zero."
|
|
|
|
);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
let matrix = Matrix4::identity();
|
|
|
|
let mut res = Perspective3::from_matrix_unchecked(matrix);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
res.set_fovy(fovy);
|
|
|
|
res.set_aspect(aspect);
|
|
|
|
res.set_znear_and_zfar(znear, zfar);
|
|
|
|
|
|
|
|
res.matrix[(3, 3)] = N::zero();
|
|
|
|
res.matrix[(3, 2)] = -N::one();
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps the given matrix to interpret it as a 3D perspective matrix.
|
|
|
|
///
|
|
|
|
/// It is not checked whether or not the given matrix actually represents an orthographic
|
|
|
|
/// projection.
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn from_matrix_unchecked(matrix: Matrix4<N>) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
Perspective3 { matrix: matrix }
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Retrieves the inverse of the underlying homogeneous matrix.
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn inverse(&self) -> Matrix4<N> {
|
2017-02-13 01:17:09 +08:00
|
|
|
let mut res = self.to_homogeneous();
|
|
|
|
|
|
|
|
res[(0, 0)] = N::one() / self.matrix[(0, 0)];
|
|
|
|
res[(1, 1)] = N::one() / self.matrix[(1, 1)];
|
|
|
|
res[(2, 2)] = N::zero();
|
|
|
|
|
|
|
|
let m23 = self.matrix[(2, 3)];
|
|
|
|
let m32 = self.matrix[(3, 2)];
|
|
|
|
|
|
|
|
res[(2, 3)] = N::one() / m32;
|
|
|
|
res[(3, 2)] = N::one() / m23;
|
|
|
|
res[(3, 3)] = -self.matrix[(2, 2)] / (m23 * m32);
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Computes the corresponding homogeneous matrix.
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn to_homogeneous(&self) -> Matrix4<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
self.matrix.clone_owned()
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
/// A reference to the underlying homogeneous transformation matrix.
|
|
|
|
#[inline]
|
|
|
|
pub fn as_matrix(&self) -> &Matrix4<N> {
|
|
|
|
&self.matrix
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the underlying homogeneous matrix.
|
|
|
|
#[inline]
|
|
|
|
pub fn unwrap(self) -> Matrix4<N> {
|
|
|
|
self.matrix
|
|
|
|
}
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Gets the `width / height` aspect ratio of the view frustrum.
|
|
|
|
#[inline]
|
|
|
|
pub fn aspect(&self) -> N {
|
|
|
|
self.matrix[(1, 1)] / self.matrix[(0, 0)]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the y field of view of the view frustrum.
|
|
|
|
#[inline]
|
|
|
|
pub fn fovy(&self) -> N {
|
|
|
|
(N::one() / self.matrix[(1, 1)]).atan() * ::convert(2.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the near plane offset of the view frustrum.
|
|
|
|
#[inline]
|
|
|
|
pub fn znear(&self) -> N {
|
|
|
|
let ratio = (-self.matrix[(2, 2)] + N::one()) / (-self.matrix[(2, 2)] - N::one());
|
|
|
|
|
|
|
|
self.matrix[(2, 3)] / (ratio * ::convert(2.0)) - self.matrix[(2, 3)] / ::convert(2.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the far plane offset of the view frustrum.
|
|
|
|
#[inline]
|
|
|
|
pub fn zfar(&self) -> N {
|
|
|
|
let ratio = (-self.matrix[(2, 2)] + N::one()) / (-self.matrix[(2, 2)] - N::one());
|
|
|
|
|
|
|
|
(self.matrix[(2, 3)] - ratio * self.matrix[(2, 3)]) / ::convert(2.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: add a method to retrieve znear and zfar simultaneously?
|
|
|
|
|
|
|
|
// FIXME: when we get specialization, specialize the Mul impl instead.
|
|
|
|
/// Projects a point. Faster than matrix multiplication.
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn project_point(&self, p: &Point3<N>) -> Point3<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
let inverse_denom = -N::one() / p[2];
|
2017-08-03 01:37:44 +08:00
|
|
|
Point3::new(
|
2018-02-02 19:26:35 +08:00
|
|
|
self.matrix[(0, 0)] * p[0] * inverse_denom,
|
|
|
|
self.matrix[(1, 1)] * p[1] * inverse_denom,
|
|
|
|
(self.matrix[(2, 2)] * p[2] + self.matrix[(2, 3)]) * inverse_denom,
|
2016-12-05 05:44:42 +08:00
|
|
|
)
|
2017-02-13 01:17:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Un-projects a point. Faster than multiplication by the matrix inverse.
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn unproject_point(&self, p: &Point3<N>) -> Point3<N> {
|
2017-02-13 01:17:09 +08:00
|
|
|
let inverse_denom = self.matrix[(2, 3)] / (p[2] + self.matrix[(2, 2)]);
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
Point3::new(
|
2017-02-13 01:17:09 +08:00
|
|
|
p[0] * inverse_denom / self.matrix[(0, 0)],
|
|
|
|
p[1] * inverse_denom / self.matrix[(1, 1)],
|
2018-02-02 19:26:35 +08:00
|
|
|
-inverse_denom,
|
2017-02-13 01:17:09 +08:00
|
|
|
)
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: when we get specialization, specialize the Mul impl instead.
|
|
|
|
/// Projects a vector. Faster than matrix multiplication.
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn project_vector<SB>(&self, p: &Vector<N, U3, SB>) -> Vector3<N>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
let inverse_denom = -N::one() / p[2];
|
2017-08-03 01:37:44 +08:00
|
|
|
Vector3::new(
|
2016-12-05 05:44:42 +08:00
|
|
|
self.matrix[(0, 0)] * p[0] * inverse_denom,
|
|
|
|
self.matrix[(1, 1)] * p[1] * inverse_denom,
|
2018-02-02 19:26:35 +08:00
|
|
|
self.matrix[(2, 2)],
|
2016-12-05 05:44:42 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates this perspective matrix with a new `width / height` aspect ratio of the view
|
|
|
|
/// frustrum.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_aspect(&mut self, aspect: N) {
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
|
|
|
!relative_eq!(aspect, N::zero()),
|
|
|
|
"The aspect ratio must not be zero."
|
|
|
|
);
|
2016-12-05 05:44:42 +08:00
|
|
|
self.matrix[(0, 0)] = self.matrix[(1, 1)] / aspect;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates this perspective with a new y field of view of the view frustrum.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_fovy(&mut self, fovy: N) {
|
2018-02-02 19:26:35 +08:00
|
|
|
let old_m22 = self.matrix[(1, 1)];
|
2016-12-05 05:44:42 +08:00
|
|
|
self.matrix[(1, 1)] = N::one() / (fovy / ::convert(2.0)).tan();
|
|
|
|
self.matrix[(0, 0)] = self.matrix[(0, 0)] * (self.matrix[(1, 1)] / old_m22);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates this perspective matrix with a new near plane offset of the view frustrum.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_znear(&mut self, znear: N) {
|
|
|
|
let zfar = self.zfar();
|
|
|
|
self.set_znear_and_zfar(znear, zfar);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates this perspective matrix with a new far plane offset of the view frustrum.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_zfar(&mut self, zfar: N) {
|
|
|
|
let znear = self.znear();
|
|
|
|
self.set_znear_and_zfar(znear, zfar);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates this perspective matrix with new near and far plane offsets of the view frustrum.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_znear_and_zfar(&mut self, znear: N, zfar: N) {
|
|
|
|
self.matrix[(2, 2)] = (zfar + znear) / (znear - zfar);
|
|
|
|
self.matrix[(2, 3)] = zfar * znear * ::convert(2.0) / (znear - zfar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real + Rand> Rand for Perspective3<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
fn rand<R: Rng>(r: &mut R) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
let znear = Rand::rand(r);
|
|
|
|
let zfar = helper::reject_rand(r, |&x: &N| !(x - znear).is_zero());
|
2016-12-05 05:44:42 +08:00
|
|
|
let aspect = helper::reject_rand(r, |&x: &N| !x.is_zero());
|
|
|
|
|
|
|
|
Self::new(aspect, Rand::rand(r), znear, zfar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
#[cfg(feature = "arbitrary")]
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real + Arbitrary> Arbitrary for Perspective3<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
fn arbitrary<G: Gen>(g: &mut G) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
let znear = Arbitrary::arbitrary(g);
|
|
|
|
let zfar = helper::reject(g, |&x: &N| !(x - znear).is_zero());
|
2016-12-05 05:44:42 +08:00
|
|
|
let aspect = helper::reject(g, |&x: &N| !x.is_zero());
|
|
|
|
|
|
|
|
Self::new(aspect, Arbitrary::arbitrary(g), znear, zfar)
|
|
|
|
}
|
|
|
|
}
|