Skip to main content

glam/dcamera/
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       | `DVec3::NEG_Z` | `DVec3::Y` |
13//! | Unreal Engine       | `DVec3::X`     | `DVec3::Z` |
14//! | Unity / glTF        | `DVec3::Z`     | `DVec3::Y` |
15//! | Blender             | `DVec3::Y`     | `DVec3::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 `DMat4`, `DAffine3`,
21//! or similar return a full model transform (rotation and translation). Functions returning
22//! `DQuat`, `DMat3`, or similar return only the model rotation.
23//!
24//! # Example
25//!
26//! ```
27//! use glam::DVec3;
28//! use glam::dcamera::model;
29//!
30//! // Bevy: object faces -Z, up is Y
31//! let q = model::look_to_quat(DVec3::X, DVec3::Y, DVec3::NEG_Z, DVec3::Y);
32//! assert!((q * DVec3::NEG_Z - DVec3::X).length() < 1e-6);
33//! ```
34
35use crate::{DAffine3, DMat3, DMat4, DQuat, DVec3};
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: DVec3, n: DVec3) -> DVec3 {
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: DVec3, up: DVec3, fwd_axis: DVec3, up_axis: DVec3) -> DMat3 {
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 = DMat3::from_cols(fwd_axis, up_axis, z_local);
68    let world = DMat3::from_cols(dir, u, z_world);
69
70    world * local.transpose()
71}
72
73/// Returns a `DQuat` 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(
88    eye: DVec3,
89    center: DVec3,
90    up: DVec3,
91    fwd_axis: DVec3,
92    up_axis: DVec3,
93) -> DQuat {
94    look_to_quat((center - eye).normalize(), up, fwd_axis, up_axis)
95}
96
97/// Returns a `DQuat` model rotation from forward direction and up.
98///
99/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
100/// maps `fwd_axis` onto `dir` and `up_axis` onto `up`.
101///
102/// # Panics
103///
104/// Will panic if any argument is not normalized, if `fwd_axis` and `up_axis` are not
105/// perpendicular, or if `dir` and `up` are parallel, when `glam_assert` is enabled.
106#[inline]
107#[must_use]
108#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
109pub fn look_to_quat(dir: DVec3, up: DVec3, fwd_axis: DVec3, up_axis: DVec3) -> DQuat {
110    DQuat::from_mat3(&look_to_rotation(dir, up, fwd_axis, up_axis))
111}
112
113/// Returns a `DMat3` model rotation from eye, focal point, and up.
114///
115/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
116/// maps `fwd_axis` onto the direction from `eye` to `center` and `up_axis` onto `up`.
117///
118/// # Panics
119///
120/// Will panic if `up`, `fwd_axis` or `up_axis` are not normalized, if `fwd_axis` and
121/// `up_axis` are not perpendicular, or if `center` is
122/// equal to `eye`, or if the direction from `eye` to `center` is parallel to `up`, when
123/// `glam_assert` is enabled.
124#[inline]
125#[must_use]
126#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
127pub fn look_at_mat3(
128    eye: DVec3,
129    center: DVec3,
130    up: DVec3,
131    fwd_axis: DVec3,
132    up_axis: DVec3,
133) -> DMat3 {
134    look_to_mat3((center - eye).normalize(), up, fwd_axis, up_axis)
135}
136
137/// Returns a `DMat3` model rotation from forward direction and up.
138///
139/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
140/// maps `fwd_axis` onto `dir` and `up_axis` onto `up`.
141///
142/// # Panics
143///
144/// Will panic if any argument is not normalized, if `fwd_axis` and `up_axis` are not
145/// perpendicular, or if `dir` and `up` are parallel, when `glam_assert` is enabled.
146#[inline]
147#[must_use]
148#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
149pub fn look_to_mat3(dir: DVec3, up: DVec3, fwd_axis: DVec3, up_axis: DVec3) -> DMat3 {
150    look_to_rotation(dir, up, fwd_axis, up_axis)
151}
152
153/// Returns a `DMat4` model transform from eye, focal point, and up.
154///
155/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
156/// maps `fwd_axis` onto the direction from `eye` to `center` and `up_axis` onto `up`.
157/// The translation of the returned matrix is `eye`.
158///
159/// # Panics
160///
161/// Will panic if `up`, `fwd_axis` or `up_axis` are not normalized, if `fwd_axis` and
162/// `up_axis` are not perpendicular, or if `center` is
163/// equal to `eye`, or if the direction from `eye` to `center` is parallel to `up`, when
164/// `glam_assert` is enabled.
165#[inline]
166#[must_use]
167#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
168pub fn look_at_mat4(
169    eye: DVec3,
170    center: DVec3,
171    up: DVec3,
172    fwd_axis: DVec3,
173    up_axis: DVec3,
174) -> DMat4 {
175    look_to_mat4(eye, (center - eye).normalize(), up, fwd_axis, up_axis)
176}
177
178/// Returns a `DMat4` model transform from eye, forward direction, and up.
179///
180/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
181/// maps `fwd_axis` onto `dir` and `up_axis` onto `up`. The translation of the returned
182/// matrix is `eye`.
183///
184/// # Panics
185///
186/// Will panic if any argument is not normalized, if `fwd_axis` and `up_axis` are not
187/// perpendicular, or if `dir` and `up` are parallel, when `glam_assert` is enabled.
188#[inline]
189#[must_use]
190#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
191pub fn look_to_mat4(eye: DVec3, dir: DVec3, up: DVec3, fwd_axis: DVec3, up_axis: DVec3) -> DMat4 {
192    let rotation = look_to_rotation(dir, up, fwd_axis, up_axis);
193    DMat4::from_cols(
194        rotation.x_axis.extend(0.0),
195        rotation.y_axis.extend(0.0),
196        rotation.z_axis.extend(0.0),
197        eye.extend(1.0),
198    )
199}
200
201/// Returns an `DAffine3` model transform from eye, focal point, and up.
202///
203/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
204/// maps `fwd_axis` onto the direction from `eye` to `center` and `up_axis` onto `up`.
205/// The translation of the returned transform is `eye`.
206///
207/// # Panics
208///
209/// Will panic if `up`, `fwd_axis` or `up_axis` are not normalized, if `fwd_axis` and
210/// `up_axis` are not perpendicular, or if `center` is
211/// equal to `eye`, or if the direction from `eye` to `center` is parallel to `up`, when
212/// `glam_assert` is enabled.
213#[inline]
214#[must_use]
215#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
216pub fn look_at_affine3(
217    eye: DVec3,
218    center: DVec3,
219    up: DVec3,
220    fwd_axis: DVec3,
221    up_axis: DVec3,
222) -> DAffine3 {
223    look_to_affine3(eye, (center - eye).normalize(), up, fwd_axis, up_axis)
224}
225
226/// Returns an `DAffine3` model transform from eye, forward direction, and up.
227///
228/// `fwd_axis` and `up_axis` are the object's local forward and up axes. The rotation
229/// maps `fwd_axis` onto `dir` and `up_axis` onto `up`. The translation of the returned
230/// transform is `eye`.
231///
232/// # Panics
233///
234/// Will panic if any argument is not normalized, if `fwd_axis` and `up_axis` are not
235/// perpendicular, or if `dir` and `up` are parallel, when `glam_assert` is enabled.
236#[inline]
237#[must_use]
238#[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
239pub fn look_to_affine3(
240    eye: DVec3,
241    dir: DVec3,
242    up: DVec3,
243    fwd_axis: DVec3,
244    up_axis: DVec3,
245) -> DAffine3 {
246    DAffine3::from_mat3_translation(look_to_rotation(dir, up, fwd_axis, up_axis), eye)
247}