nalgebra_glm/ext/
matrix_transform.rs

1use na::{Point3, Rotation3, Unit};
2
3use crate::aliases::{TMat, TMat4, TVec, TVec3};
4use crate::traits::{Number, RealNumber};
5
6/// The identity matrix.
7pub fn identity<T: Number, const D: usize>() -> TMat<T, D, D> {
8    TMat::<T, D, D>::identity()
9}
10
11/// Build a look at view matrix based on the right handedness.
12///
13/// # Parameters:
14///
15/// * `eye` − Position of the camera.
16/// * `center` − Position where the camera is looking at.
17/// * `u` − Normalized up vector, how the camera is oriented. Typically `(0, 1, 0)`.
18///
19/// # See also:
20///
21/// * [`look_at_lh()`]
22/// * [`look_at_rh()`]
23pub fn look_at<T: RealNumber>(eye: &TVec3<T>, center: &TVec3<T>, up: &TVec3<T>) -> TMat4<T> {
24    look_at_rh(eye, center, up)
25}
26
27/// Build a left handed look at view matrix.
28///
29/// # Parameters:
30///
31/// * `eye` − Position of the camera.
32/// * `center` − Position where the camera is looking at.
33/// * `u` − Normalized up vector, how the camera is oriented. Typically `(0, 1, 0)`.
34///
35/// # See also:
36///
37/// * [`look_at()`]
38/// * [`look_at_rh()`]
39pub fn look_at_lh<T: RealNumber>(eye: &TVec3<T>, center: &TVec3<T>, up: &TVec3<T>) -> TMat4<T> {
40    TMat::look_at_lh(&Point3::from(*eye), &Point3::from(*center), up)
41}
42
43/// Build a right handed look at view matrix.
44///
45/// # Parameters:
46///
47/// * `eye` − Position of the camera.
48/// * `center` − Position where the camera is looking at.
49/// * `u` − Normalized up vector, how the camera is oriented. Typically `(0, 1, 0)`.
50///
51/// # See also:
52///
53/// * [`look_at()`]
54/// * [`look_at_lh()`]
55pub fn look_at_rh<T: RealNumber>(eye: &TVec3<T>, center: &TVec3<T>, up: &TVec3<T>) -> TMat4<T> {
56    TMat::look_at_rh(&Point3::from(*eye), &Point3::from(*center), up)
57}
58
59/// Builds a rotation 4 * 4 matrix created from an axis vector and an angle and right-multiply it to `m`.
60///
61/// # Parameters:
62///
63/// * `m` − Input matrix multiplied by this rotation matrix.
64/// * `angle` − Rotation angle expressed in radians.
65/// * `axis` − Rotation axis, recommended to be normalized.
66///
67/// # See also:
68///
69/// * [`rotate_x()`]
70/// * [`rotate_y()`]
71/// * [`rotate_z()`]
72/// * [`scale()`]
73/// * [`translate()`]
74pub fn rotate<T: RealNumber>(m: &TMat4<T>, angle: T, axis: &TVec3<T>) -> TMat4<T> {
75    m * Rotation3::from_axis_angle(&Unit::new_normalize(*axis), angle).to_homogeneous()
76}
77
78/// Builds a rotation 4 * 4 matrix around the X axis and right-multiply it to `m`.
79///
80/// # Parameters:
81///
82/// * `m` − Input matrix multiplied by this rotation matrix.
83/// * `angle` − Rotation angle expressed in radians.
84///
85/// # See also:
86///
87/// * [`rotate()`]
88/// * [`rotate_y()`]
89/// * [`rotate_z()`]
90/// * [`scale()`]
91/// * [`translate()`]
92pub fn rotate_x<T: RealNumber>(m: &TMat4<T>, angle: T) -> TMat4<T> {
93    rotate(m, angle, &TVec::x())
94}
95
96/// Builds a rotation 4 * 4 matrix around the Y axis and right-multiply it to `m`.
97///
98/// # Parameters:
99///
100/// * `m` − Input matrix multiplied by this rotation matrix.
101/// * `angle` − Rotation angle expressed in radians.
102///
103/// # See also:
104///
105/// * [`rotate()`]
106/// * [`rotate_x()`]
107/// * [`rotate_z()`]
108/// * [`scale()`]
109/// * [`translate()`]
110pub fn rotate_y<T: RealNumber>(m: &TMat4<T>, angle: T) -> TMat4<T> {
111    rotate(m, angle, &TVec::y())
112}
113
114/// Builds a rotation 4 * 4 matrix around the Z axis and right-multiply it to `m`.
115///
116/// # Parameters:
117///
118/// * `m` − Input matrix multiplied by this rotation matrix.
119/// * `angle` − Rotation angle expressed in radians.
120///
121/// # See also:
122///
123/// * [`rotate()`]
124/// * [`rotate_x()`]
125/// * [`rotate_y()`]
126/// * [`scale()`]
127/// * [`translate()`]
128pub fn rotate_z<T: RealNumber>(m: &TMat4<T>, angle: T) -> TMat4<T> {
129    rotate(m, angle, &TVec::z())
130}
131
132/// Builds a scale 4 * 4 matrix created from 3 scalars and right-multiply it to `m`.
133///
134/// # Parameters:
135///
136/// * `m` − Input matrix multiplied by this scale matrix.
137/// * `v` − Ratio of scaling for each axis.
138///
139/// # See also:
140///
141/// * [`rotate()`]
142/// * [`rotate_x()`]
143/// * [`rotate_y()`]
144/// * [`rotate_z()`]
145/// * [`translate()`]
146pub fn scale<T: Number>(m: &TMat4<T>, v: &TVec3<T>) -> TMat4<T> {
147    m.prepend_nonuniform_scaling(v)
148}
149
150/// Builds a translation 4 * 4 matrix created from a vector of 3 components and right-multiply it to `m`.
151///
152/// # Parameters:
153///
154/// * `m` − Input matrix multiplied by this translation matrix.
155/// * `v` − Coordinates of a translation vector.
156///
157/// # See also:
158///
159/// * [`rotate()`]
160/// * [`rotate_x()`]
161/// * [`rotate_y()`]
162/// * [`rotate_z()`]
163/// * [`scale()`]
164pub fn translate<T: Number>(m: &TMat4<T>, v: &TVec3<T>) -> TMat4<T> {
165    m.prepend_translation(v)
166}