Skip to main content

glam/camera/
model.rs

1// Generated from camera_model.rs.tera template. Edit the template, not the generated file.
2
3//! Model transform constructors: orient objects in world space.
4//!
5//! These functions produce local -> world transforms (object rotation and translation).
6//!
7//! Unlike view transforms, model transforms take explicit local axis parameters
8//! (`fwd_axis` and `up_axis`) because object conventions vary:
9//!
10//! | Engine / Convention | `fwd_axis`    | `up_axis` |
11//! |---------------------|---------------|-----------|
12//! | Bevy / OpenGL       | `Vec3::NEG_Z` | `Vec3::Y` |
13//! | Unreal Engine       | `Vec3::X`     | `Vec3::Z` |
14//! | Unity / glTF        | `Vec3::Z`     | `Vec3::Y` |
15//! | Blender             | `Vec3::Y`     | `Vec3::Z` |
16//!
17//! `fwd_axis` and `up_axis` must be perpendicular.
18//!
19//! `look_to_*` functions target a forward direction (`dir`), `look_at_*` functions
20//! target a focal point (`center`). Functions returning `Mat4`, `Affine3`, `Affine3A`,
21//! or similar return a full model transform (rotation and translation). Functions returning
22//! `Quat`, `Mat3`, `Mat3A`, or similar return only the model rotation.
23//!
24//! # Example
25//!
26//! ```
27//! use glam::Vec3;
28//! use glam::camera::model;
29//!
30//! // Bevy: object faces -Z, up is Y
31//! let q = model::look_to_quat(Vec3::X, Vec3::Y, Vec3::NEG_Z, Vec3::Y);
32//! assert!((q * Vec3::NEG_Z - Vec3::X).length() < 1e-6);
33//! ```
34
35use crate::{Affine3, Affine3A, Mat3, Mat3A, Mat4, Quat, Vec3, Vec3A};
36
37/// Removes the component of `v` parallel to the unit vector `n` and normalizes the
38/// result.
39#[inline]
40#[must_use]
41fn orthogonalize(v: Vec3, n: Vec3) -> Vec3 {
42    (v - n * v.dot(n)).normalize()
43}
44
45/// Returns the rotation that maps the local frame given by `fwd_axis` and `up_axis`
46/// onto the world frame given by `dir` and `up`.
47///
48/// The third local and world basis vectors are derived from the given pairs, so the
49/// handedness of the local frame is encoded in `fwd_axis` and `up_axis`.
50#[inline]
51#[must_use]
52#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
53fn look_to_rotation(dir: Vec3, up: Vec3, fwd_axis: Vec3, up_axis: Vec3) -> Mat3 {
54    glam_assert!(dir.is_normalized());
55    glam_assert!(up.is_normalized());
56    glam_assert!(fwd_axis.is_normalized());
57    glam_assert!(up_axis.is_normalized());
58    glam_assert!(
59        fwd_axis.dot(up_axis).abs() <= 2e-4,
60        "fwd_axis and up_axis must be perpendicular"
61    );
62
63    let u = orthogonalize(up, dir);
64    let z_local = fwd_axis.cross(up_axis);
65    let z_world = dir.cross(u);
66
67    let local = Mat3::from_cols(fwd_axis, up_axis, z_local);
68    let world = Mat3::from_cols(dir, u, z_world);
69
70    world * local.transpose()
71}
72
73/// Returns a `Quat` model rotation from eye, focal point, and up.
74///
75/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
76/// maps `fwd_axis` onto the direction from `eye` to `center` and `up_axis` onto `up`.
77///
78/// # Panics
79///
80/// Will panic if `up`, `fwd_axis` or `up_axis` are not normalized, if `fwd_axis` and
81/// `up_axis` are not perpendicular, or if `center` is
82/// equal to `eye`, or if the direction from `eye` to `center` is parallel to `up`, when
83/// `glam_assert` is enabled.
84#[inline]
85#[must_use]
86#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
87pub fn look_at_quat(eye: Vec3, center: Vec3, up: Vec3, fwd_axis: Vec3, up_axis: Vec3) -> Quat {
88    look_to_quat((center - eye).normalize(), up, fwd_axis, up_axis)
89}
90
91/// Returns a `Quat` model rotation from forward direction and up.
92///
93/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
94/// maps `fwd_axis` onto `dir` and `up_axis` onto `up`.
95///
96/// # Panics
97///
98/// Will panic if any argument is not normalized, if `fwd_axis` and `up_axis` are not
99/// perpendicular, or if `dir` and `up` are parallel, when `glam_assert` is enabled.
100#[inline]
101#[must_use]
102#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
103pub fn look_to_quat(dir: Vec3, up: Vec3, fwd_axis: Vec3, up_axis: Vec3) -> Quat {
104    Quat::from_mat3(&look_to_rotation(dir, up, fwd_axis, up_axis))
105}
106
107/// Returns a `Mat3` model rotation from eye, focal point, and up.
108///
109/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
110/// maps `fwd_axis` onto the direction from `eye` to `center` and `up_axis` onto `up`.
111///
112/// # Panics
113///
114/// Will panic if `up`, `fwd_axis` or `up_axis` are not normalized, if `fwd_axis` and
115/// `up_axis` are not perpendicular, or if `center` is
116/// equal to `eye`, or if the direction from `eye` to `center` is parallel to `up`, when
117/// `glam_assert` is enabled.
118#[inline]
119#[must_use]
120#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
121pub fn look_at_mat3(eye: Vec3, center: Vec3, up: Vec3, fwd_axis: Vec3, up_axis: Vec3) -> Mat3 {
122    look_to_mat3((center - eye).normalize(), up, fwd_axis, up_axis)
123}
124
125/// Returns a `Mat3` model rotation from forward direction and up.
126///
127/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
128/// maps `fwd_axis` onto `dir` and `up_axis` onto `up`.
129///
130/// # Panics
131///
132/// Will panic if any argument is not normalized, if `fwd_axis` and `up_axis` are not
133/// perpendicular, or if `dir` and `up` are parallel, when `glam_assert` is enabled.
134#[inline]
135#[must_use]
136#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
137pub fn look_to_mat3(dir: Vec3, up: Vec3, fwd_axis: Vec3, up_axis: Vec3) -> Mat3 {
138    look_to_rotation(dir, up, fwd_axis, up_axis)
139}
140
141/// Returns a `Mat3A` model rotation from eye, focal point, and up.
142///
143/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
144/// maps `fwd_axis` onto the direction from `eye` to `center` and `up_axis` onto `up`.
145///
146/// # Panics
147///
148/// Will panic if `up`, `fwd_axis` or `up_axis` are not normalized, if `fwd_axis` and
149/// `up_axis` are not perpendicular, or if `center` is
150/// equal to `eye`, or if the direction from `eye` to `center` is parallel to `up`, when
151/// `glam_assert` is enabled.
152#[inline]
153#[must_use]
154#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
155pub fn look_at_mat3a(eye: Vec3, center: Vec3, up: Vec3, fwd_axis: Vec3, up_axis: Vec3) -> Mat3A {
156    look_to_mat3a((center - eye).normalize(), up, fwd_axis, up_axis)
157}
158
159/// Returns a `Mat3A` model rotation from forward direction and up.
160///
161/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
162/// maps `fwd_axis` onto `dir` and `up_axis` onto `up`.
163///
164/// # Panics
165///
166/// Will panic if any argument is not normalized, if `fwd_axis` and `up_axis` are not
167/// perpendicular, or if `dir` and `up` are parallel, when `glam_assert` is enabled.
168#[inline]
169#[must_use]
170#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
171pub fn look_to_mat3a(dir: Vec3, up: Vec3, fwd_axis: Vec3, up_axis: Vec3) -> Mat3A {
172    let rotation = look_to_rotation(dir, up, fwd_axis, up_axis);
173    Mat3A::from_cols(
174        Vec3A::from(rotation.x_axis),
175        Vec3A::from(rotation.y_axis),
176        Vec3A::from(rotation.z_axis),
177    )
178}
179
180/// Returns a `Mat4` model transform from eye, focal point, and up.
181///
182/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
183/// maps `fwd_axis` onto the direction from `eye` to `center` and `up_axis` onto `up`.
184/// The translation of the returned matrix is `eye`.
185///
186/// # Panics
187///
188/// Will panic if `up`, `fwd_axis` or `up_axis` are not normalized, if `fwd_axis` and
189/// `up_axis` are not perpendicular, or if `center` is
190/// equal to `eye`, or if the direction from `eye` to `center` is parallel to `up`, when
191/// `glam_assert` is enabled.
192#[inline]
193#[must_use]
194#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
195pub fn look_at_mat4(eye: Vec3, center: Vec3, up: Vec3, fwd_axis: Vec3, up_axis: Vec3) -> Mat4 {
196    look_to_mat4(eye, (center - eye).normalize(), up, fwd_axis, up_axis)
197}
198
199/// Returns a `Mat4` model transform from eye, forward direction, and up.
200///
201/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
202/// maps `fwd_axis` onto `dir` and `up_axis` onto `up`. The translation of the returned
203/// matrix is `eye`.
204///
205/// # Panics
206///
207/// Will panic if any argument is not normalized, if `fwd_axis` and `up_axis` are not
208/// perpendicular, or if `dir` and `up` are parallel, when `glam_assert` is enabled.
209#[inline]
210#[must_use]
211#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
212pub fn look_to_mat4(eye: Vec3, dir: Vec3, up: Vec3, fwd_axis: Vec3, up_axis: Vec3) -> Mat4 {
213    let rotation = look_to_rotation(dir, up, fwd_axis, up_axis);
214    Mat4::from_cols(
215        rotation.x_axis.extend(0.0),
216        rotation.y_axis.extend(0.0),
217        rotation.z_axis.extend(0.0),
218        eye.extend(1.0),
219    )
220}
221
222/// Returns an `Affine3` model transform from eye, focal point, and up.
223///
224/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
225/// maps `fwd_axis` onto the direction from `eye` to `center` and `up_axis` onto `up`.
226/// The translation of the returned transform is `eye`.
227///
228/// # Panics
229///
230/// Will panic if `up`, `fwd_axis` or `up_axis` are not normalized, if `fwd_axis` and
231/// `up_axis` are not perpendicular, or if `center` is
232/// equal to `eye`, or if the direction from `eye` to `center` is parallel to `up`, when
233/// `glam_assert` is enabled.
234#[inline]
235#[must_use]
236#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
237pub fn look_at_affine3(
238    eye: Vec3,
239    center: Vec3,
240    up: Vec3,
241    fwd_axis: Vec3,
242    up_axis: Vec3,
243) -> Affine3 {
244    look_to_affine3(eye, (center - eye).normalize(), up, fwd_axis, up_axis)
245}
246
247/// Returns an `Affine3` model transform from eye, forward direction, and up.
248///
249/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
250/// maps `fwd_axis` onto `dir` and `up_axis` onto `up`. The translation of the returned
251/// transform is `eye`.
252///
253/// # Panics
254///
255/// Will panic if any argument is not normalized, if `fwd_axis` and `up_axis` are not
256/// perpendicular, or if `dir` and `up` are parallel, when `glam_assert` is enabled.
257#[inline]
258#[must_use]
259#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
260pub fn look_to_affine3(eye: Vec3, dir: Vec3, up: Vec3, fwd_axis: Vec3, up_axis: Vec3) -> Affine3 {
261    Affine3::from_mat3_translation(look_to_rotation(dir, up, fwd_axis, up_axis), eye)
262}
263
264/// Returns an `Affine3A` model transform from eye, focal point, and up.
265///
266/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
267/// maps `fwd_axis` onto the direction from `eye` to `center` and `up_axis` onto `up`.
268/// The translation of the returned transform is `eye`.
269///
270/// # Panics
271///
272/// Will panic if `up`, `fwd_axis` or `up_axis` are not normalized, if `fwd_axis` and
273/// `up_axis` are not perpendicular, or if `center` is
274/// equal to `eye`, or if the direction from `eye` to `center` is parallel to `up`, when
275/// `glam_assert` is enabled.
276#[inline]
277#[must_use]
278#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
279pub fn look_at_affine3a(
280    eye: Vec3,
281    center: Vec3,
282    up: Vec3,
283    fwd_axis: Vec3,
284    up_axis: Vec3,
285) -> Affine3A {
286    look_to_affine3a(eye, (center - eye).normalize(), up, fwd_axis, up_axis)
287}
288
289/// Returns an `Affine3A` model transform from eye, forward direction, and up.
290///
291/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
292/// maps `fwd_axis` onto `dir` and `up_axis` onto `up`. The translation of the returned
293/// transform is `eye`.
294///
295/// # Panics
296///
297/// Will panic if any argument is not normalized, if `fwd_axis` and `up_axis` are not
298/// perpendicular, or if `dir` and `up` are parallel, when `glam_assert` is enabled.
299#[inline]
300#[must_use]
301#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
302pub fn look_to_affine3a(eye: Vec3, dir: Vec3, up: Vec3, fwd_axis: Vec3, up_axis: Vec3) -> Affine3A {
303    Affine3A::from_mat3_translation(look_to_rotation(dir, up, fwd_axis, up_axis), eye)
304}