Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mat3/Quat look_at and look_to methods, look_to direction must now be normalized. #551

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading