nalgebra/nalgebra-glm/src/ext/matrix_transform.rs

167 lines
5.1 KiB
Rust
Raw Normal View History

2021-08-08 18:59:40 +08:00
use na::{Point3, Rotation3, Unit};
2019-03-23 21:29:07 +08:00
use crate::aliases::{TMat, TMat4, TVec, TVec3};
2021-08-08 18:59:40 +08:00
use crate::traits::{Number, RealNumber};
/// The identity matrix.
2021-04-11 17:00:38 +08:00
pub fn identity<T: Number, const D: usize>() -> TMat<T, D, D> {
TMat::<T, D, D>::identity()
}
/// Build a look at view matrix based on the right handedness.
///
2018-10-08 10:42:02 +08:00
/// # Parameters:
///
/// * `eye` Position of the camera.
/// * `center` Position where the camera is looking at.
/// * `u` Normalized up vector, how the camera is oriented. Typically `(0, 1, 0)`.
2018-10-08 10:30:16 +08:00
///
/// # See also:
///
/// * [`look_at_lh`](fn.look_at_lh.html)
/// * [`look_at_rh`](fn.look_at_rh.html)
2021-08-08 18:59:40 +08:00
pub fn look_at<T: RealNumber>(eye: &TVec3<T>, center: &TVec3<T>, up: &TVec3<T>) -> TMat4<T> {
look_at_rh(eye, center, up)
}
/// Build a left handed look at view matrix.
///
2018-10-08 10:42:02 +08:00
/// # Parameters:
///
/// * `eye` Position of the camera.
/// * `center` Position where the camera is looking at.
/// * `u` Normalized up vector, how the camera is oriented. Typically `(0, 1, 0)`.
2018-10-08 10:30:16 +08:00
///
/// # See also:
///
/// * [`look_at`](fn.look_at.html)
/// * [`look_at_rh`](fn.look_at_rh.html)
2021-08-08 18:59:40 +08:00
pub fn look_at_lh<T: RealNumber>(eye: &TVec3<T>, center: &TVec3<T>, up: &TVec3<T>) -> TMat4<T> {
2018-10-27 20:28:40 +08:00
TMat::look_at_lh(&Point3::from(*eye), &Point3::from(*center), up)
}
/// Build a right handed look at view matrix.
///
2018-10-08 10:42:02 +08:00
/// # Parameters:
///
/// * `eye` Position of the camera.
/// * `center` Position where the camera is looking at.
/// * `u` Normalized up vector, how the camera is oriented. Typically `(0, 1, 0)`.
2018-10-08 10:30:16 +08:00
///
/// # See also:
///
/// * [`look_at`](fn.look_at.html)
/// * [`look_at_lh`](fn.look_at_lh.html)
2021-08-08 18:59:40 +08:00
pub fn look_at_rh<T: RealNumber>(eye: &TVec3<T>, center: &TVec3<T>, up: &TVec3<T>) -> TMat4<T> {
2018-10-27 20:28:40 +08:00
TMat::look_at_rh(&Point3::from(*eye), &Point3::from(*center), up)
}
2018-09-22 23:36:08 +08:00
/// Builds a rotation 4 * 4 matrix created from an axis vector and an angle and right-multiply it to `m`.
///
2018-10-08 10:42:02 +08:00
/// # Parameters:
///
/// * `m` Input matrix multiplied by this rotation matrix.
/// * `angle` Rotation angle expressed in radians.
/// * `axis` Rotation axis, recommended to be normalized.
2018-10-08 10:30:16 +08:00
///
/// # See also:
///
/// * [`rotate_x`](fn.rotate_x.html)
/// * [`rotate_y`](fn.rotate_y.html)
/// * [`rotate_z`](fn.rotate_z.html)
/// * [`scale`](fn.scale.html)
/// * [`translate`](fn.translate.html)
2021-08-08 18:59:40 +08:00
pub fn rotate<T: RealNumber>(m: &TMat4<T>, angle: T, axis: &TVec3<T>) -> TMat4<T> {
m * Rotation3::from_axis_angle(&Unit::new_normalize(*axis), angle).to_homogeneous()
}
2018-09-23 01:05:37 +08:00
/// Builds a rotation 4 * 4 matrix around the X axis and right-multiply it to `m`.
///
2018-10-08 10:42:02 +08:00
/// # Parameters:
///
/// * `m` Input matrix multiplied by this rotation matrix.
/// * `angle` Rotation angle expressed in radians.
2018-10-08 10:30:16 +08:00
///
/// # See also:
///
/// * [`rotate`](fn.rotate.html)
/// * [`rotate_y`](fn.rotate_y.html)
/// * [`rotate_z`](fn.rotate_z.html)
/// * [`scale`](fn.scale.html)
/// * [`translate`](fn.translate.html)
2021-08-08 18:59:40 +08:00
pub fn rotate_x<T: RealNumber>(m: &TMat4<T>, angle: T) -> TMat4<T> {
rotate(m, angle, &TVec::x())
2018-09-23 01:05:37 +08:00
}
/// Builds a rotation 4 * 4 matrix around the Y axis and right-multiply it to `m`.
///
2018-10-08 10:42:02 +08:00
/// # Parameters:
///
/// * `m` Input matrix multiplied by this rotation matrix.
/// * `angle` Rotation angle expressed in radians.
2018-10-08 10:30:16 +08:00
///
/// # See also:
///
/// * [`rotate`](fn.rotate.html)
/// * [`rotate_x`](fn.rotate_x.html)
/// * [`rotate_z`](fn.rotate_z.html)
/// * [`scale`](fn.scale.html)
/// * [`translate`](fn.translate.html)
2021-08-08 18:59:40 +08:00
pub fn rotate_y<T: RealNumber>(m: &TMat4<T>, angle: T) -> TMat4<T> {
rotate(m, angle, &TVec::y())
2018-09-23 01:05:37 +08:00
}
/// Builds a rotation 4 * 4 matrix around the Z axis and right-multiply it to `m`.
///
2018-10-08 10:42:02 +08:00
/// # Parameters:
///
/// * `m` Input matrix multiplied by this rotation matrix.
/// * `angle` Rotation angle expressed in radians.
2018-10-08 10:30:16 +08:00
///
/// # See also:
///
/// * [`rotate`](fn.rotate.html)
/// * [`rotate_x`](fn.rotate_x.html)
/// * [`rotate_y`](fn.rotate_y.html)
/// * [`scale`](fn.scale.html)
/// * [`translate`](fn.translate.html)
2021-08-08 18:59:40 +08:00
pub fn rotate_z<T: RealNumber>(m: &TMat4<T>, angle: T) -> TMat4<T> {
rotate(m, angle, &TVec::z())
2018-09-23 01:05:37 +08:00
}
2018-09-22 23:36:08 +08:00
/// Builds a scale 4 * 4 matrix created from 3 scalars and right-multiply it to `m`.
///
2018-10-08 10:42:02 +08:00
/// # Parameters:
///
/// * `m` Input matrix multiplied by this scale matrix.
/// * `v` Ratio of scaling for each axis.
2018-10-08 10:30:16 +08:00
///
/// # See also:
///
/// * [`rotate`](fn.rotate.html)
/// * [`rotate_x`](fn.rotate_x.html)
/// * [`rotate_y`](fn.rotate_y.html)
/// * [`rotate_z`](fn.rotate_z.html)
/// * [`translate`](fn.translate.html)
2021-04-11 17:00:38 +08:00
pub fn scale<T: Number>(m: &TMat4<T>, v: &TVec3<T>) -> TMat4<T> {
m.prepend_nonuniform_scaling(v)
}
2018-09-22 23:36:08 +08:00
/// Builds a translation 4 * 4 matrix created from a vector of 3 components and right-multiply it to `m`.
///
2018-10-08 10:42:02 +08:00
/// # Parameters:
///
/// * `m` Input matrix multiplied by this translation matrix.
/// * `v` Coordinates of a translation vector.
2018-10-08 10:30:16 +08:00
///
/// # See also:
///
/// * [`rotate`](fn.rotate.html)
/// * [`rotate_x`](fn.rotate_x.html)
/// * [`rotate_y`](fn.rotate_y.html)
/// * [`rotate_z`](fn.rotate_z.html)
/// * [`scale`](fn.scale.html)
2021-04-11 17:00:38 +08:00
pub fn translate<T: Number>(m: &TMat4<T>, v: &TVec3<T>) -> TMat4<T> {
m.prepend_translation(v)
}