Skip to content

Commit

Permalink
Add look_at and look_to methods to Quat and Mat3.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitshifter committed Aug 19, 2024
1 parent 833ec1f commit 767ffb6
Show file tree
Hide file tree
Showing 22 changed files with 1,104 additions and 87 deletions.
97 changes: 85 additions & 12 deletions codegen/templates/mat.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -1660,25 +1660,98 @@ impl {{ self_t }} {
{{ mat2_t }}::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs
}

{% elif dim == 4 %}
/// Creates a left-handed view matrix using a camera position, an up direction, and a facing
/// Creates a left-handed view matrix using a facing direction and an up direction.
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
///
/// # Panics
///
/// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled.
#[inline]
#[must_use]
pub fn look_to_lh(dir: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
Self::look_to_rh(-dir, up)
}

/// Creates a right-handed view matrix using a facing direction and an up direction.
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
///
/// # Panics
///
/// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled.
#[inline]
#[must_use]
pub fn look_to_rh(dir: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
glam_assert!(dir.is_normalized());
glam_assert!(up.is_normalized());
let f = dir;
let s = f.cross(up).normalize();
let u = s.cross(f);

Self::from_cols(
{{ col_t }}::new(s.x, u.x, -f.x),
{{ col_t }}::new(s.y, u.y, -f.y),
{{ col_t }}::new(s.z, u.z, -f.z),
)
}

/// Creates a left-handed view matrix using a camera position, a focal point and an up
/// direction.
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
///
/// # Panics
///
/// Will panic if `up` is not normalized when `glam_assert` is enabled.
#[inline]
#[must_use]
pub fn look_at_lh(eye: {{ vec3_t }}, center: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
Self::look_to_lh(center.sub(eye).normalize(), up)
}

/// Creates a right-handed view matrix using a camera position, a focal point and an up
/// direction.
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
///
/// # Panics
///
/// Will panic if `up` is not normalized when `glam_assert` is enabled.
#[inline]
pub fn look_at_rh(eye: {{ vec3_t }}, center: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
Self::look_to_rh(center.sub(eye).normalize(), up)
}

{% elif dim == 4 %}
/// Creates a left-handed view matrix using a camera position, a facing direction and an up
/// direction
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
///
/// # Panics
///
/// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled.
#[inline]
#[must_use]
pub fn look_to_lh(eye: {{ vec3_t }}, dir: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
Self::look_to_rh(eye, -dir, up)
}

/// Creates a right-handed view matrix using a camera position, an up direction, and a facing
/// Creates a right-handed view matrix using a camera position, a facing direction, and an up
/// direction.
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
///
/// # Panics
///
/// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled.
#[inline]
#[must_use]
pub fn look_to_rh(eye: {{ vec3_t }}, dir: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
let f = dir.normalize();
glam_assert!(dir.is_normalized());
glam_assert!(up.is_normalized());
let f = dir;
let s = f.cross(up).normalize();
let u = s.cross(f);

Expand All @@ -1690,8 +1763,9 @@ impl {{ self_t }} {
)
}

/// Creates a left-handed view matrix using a camera position, an up direction, and a focal
/// point.
/// Creates a left-handed view matrix using a camera position, a focal points and an up
/// direction.
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
///
/// # Panics
Expand All @@ -1700,21 +1774,20 @@ impl {{ self_t }} {
#[inline]
#[must_use]
pub fn look_at_lh(eye: {{ vec3_t }}, center: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
glam_assert!(up.is_normalized());
Self::look_to_lh(eye, center.sub(eye), up)
Self::look_to_lh(eye, center.sub(eye).normalize(), up)
}

/// Creates a right-handed view matrix using a camera position, an up direction, and a focal
/// point.
/// Creates a right-handed view matrix using a camera position, a focal point, and an up
/// direction.
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
///
/// # Panics
///
/// Will panic if `up` is not normalized when `glam_assert` is enabled.
#[inline]
pub fn look_at_rh(eye: {{ vec3_t }}, center: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
glam_assert!(up.is_normalized());
Self::look_to_rh(eye, center.sub(eye), up)
Self::look_to_rh(eye, center.sub(eye).normalize(), up)
}

/// Creates a right-handed perspective projection matrix with [-1,1] depth range.
Expand Down
63 changes: 63 additions & 0 deletions codegen/templates/quat.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,69 @@ impl {{ self_t }} {
}
}

/// Creates a quaterion rotation from a facing direction and an up direction.
///
/// For a left-handed view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
///
/// # Panics
///
/// Will panic if `up` is not normalized when `glam_assert` is enabled.
#[inline]
#[must_use]
pub fn look_to_lh(dir: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
Self::look_to_rh(-dir, up)
}

/// Creates a quaterion rotation from facing direction and an up direction.
///
/// For a right-handed view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
///
/// # Panics
///
/// Will panic if `dir` and `up` are not normalized when `glam_assert` is enabled.
#[inline]
#[must_use]
pub fn look_to_rh(dir: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
glam_assert!(dir.is_normalized());
glam_assert!(up.is_normalized());
let f = dir;
let s = f.cross(up).normalize();
let u = s.cross(f);

Self::from_rotation_axes(
{{ vec3_t }}::new(s.x, u.x, -f.x),
{{ vec3_t }}::new(s.y, u.y, -f.y),
{{ vec3_t }}::new(s.z, u.z, -f.z),
)
}

/// Creates a left-handed view matrix using a camera position, a focal point, and an up
/// direction.
///
/// For a left-handed view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
///
/// # Panics
///
/// Will panic if `up` is not normalized when `glam_assert` is enabled.
#[inline]
#[must_use]
pub fn look_at_lh(eye: {{ vec3_t }}, center: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
Self::look_to_lh(center.sub(eye).normalize(), up)
}

/// Creates a right-handed view matrix using a camera position, an up direction, and a focal
/// point.
///
/// For a right-handed view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
///
/// # Panics
///
/// Will panic if `up` is not normalized when `glam_assert` is enabled.
#[inline]
pub fn look_at_rh(eye: {{ vec3_t }}, center: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
Self::look_to_rh(center.sub(eye).normalize(), up)
}

/// Returns the rotation axis (normalized) and angle (in radians) of `self`.
#[inline]
#[must_use]
Expand Down
63 changes: 63 additions & 0 deletions src/f32/coresimd/mat3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,69 @@ impl Mat3A {
Mat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs
}

/// Creates a left-handed view matrix using a facing direction and an up direction.
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
///
/// # Panics
///
/// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled.
#[inline]
#[must_use]
pub fn look_to_lh(dir: Vec3, up: Vec3) -> Self {
Self::look_to_rh(-dir, up)
}

/// Creates a right-handed view matrix using a facing direction and an up direction.
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
///
/// # Panics
///
/// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled.
#[inline]
#[must_use]
pub fn look_to_rh(dir: Vec3, up: Vec3) -> Self {
glam_assert!(dir.is_normalized());
glam_assert!(up.is_normalized());
let f = dir;
let s = f.cross(up).normalize();
let u = s.cross(f);

Self::from_cols(
Vec3A::new(s.x, u.x, -f.x),
Vec3A::new(s.y, u.y, -f.y),
Vec3A::new(s.z, u.z, -f.z),
)
}

/// Creates a left-handed view matrix using a camera position, a focal point and an up
/// direction.
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
///
/// # Panics
///
/// Will panic if `up` is not normalized when `glam_assert` is enabled.
#[inline]
#[must_use]
pub fn look_at_lh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
Self::look_to_lh(center.sub(eye).normalize(), up)
}

/// Creates a right-handed view matrix using a camera position, a focal point and an up
/// direction.
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
///
/// # Panics
///
/// Will panic if `up` is not normalized when `glam_assert` is enabled.
#[inline]
pub fn look_at_rh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
Self::look_to_rh(center.sub(eye).normalize(), up)
}

/// Transforms a 3D vector.
#[inline]
#[must_use]
Expand Down
34 changes: 22 additions & 12 deletions src/f32/coresimd/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,24 +793,34 @@ impl Mat4 {
}
}

/// Creates a left-handed view matrix using a camera position, an up direction, and a facing
/// direction.
/// Creates a left-handed view matrix using a camera position, a facing direction and an up
/// direction
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
///
/// # Panics
///
/// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled.
#[inline]
#[must_use]
pub fn look_to_lh(eye: Vec3, dir: Vec3, up: Vec3) -> Self {
Self::look_to_rh(eye, -dir, up)
}

/// Creates a right-handed view matrix using a camera position, an up direction, and a facing
/// Creates a right-handed view matrix using a camera position, a facing direction, and an up
/// direction.
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
///
/// # Panics
///
/// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled.
#[inline]
#[must_use]
pub fn look_to_rh(eye: Vec3, dir: Vec3, up: Vec3) -> Self {
let f = dir.normalize();
glam_assert!(dir.is_normalized());
glam_assert!(up.is_normalized());
let f = dir;
let s = f.cross(up).normalize();
let u = s.cross(f);

Expand All @@ -822,8 +832,9 @@ impl Mat4 {
)
}

/// Creates a left-handed view matrix using a camera position, an up direction, and a focal
/// point.
/// Creates a left-handed view matrix using a camera position, a focal points and an up
/// direction.
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
///
/// # Panics
Expand All @@ -832,21 +843,20 @@ impl Mat4 {
#[inline]
#[must_use]
pub fn look_at_lh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
glam_assert!(up.is_normalized());
Self::look_to_lh(eye, center.sub(eye), up)
Self::look_to_lh(eye, center.sub(eye).normalize(), up)
}

/// Creates a right-handed view matrix using a camera position, an up direction, and a focal
/// point.
/// Creates a right-handed view matrix using a camera position, a focal point, and an up
/// direction.
///
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
///
/// # Panics
///
/// Will panic if `up` is not normalized when `glam_assert` is enabled.
#[inline]
pub fn look_at_rh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
glam_assert!(up.is_normalized());
Self::look_to_rh(eye, center.sub(eye), up)
Self::look_to_rh(eye, center.sub(eye).normalize(), up)
}

/// Creates a right-handed perspective projection matrix with [-1,1] depth range.
Expand Down
Loading

0 comments on commit 767ffb6

Please sign in to comment.