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

Mat4::frustum_{lh,rh{,_gl}} #529

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
92 changes: 92 additions & 0 deletions codegen/templates/mat.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,98 @@ impl {{ self_t }} {
Self::look_to_rh(eye, center.sub(eye), up)
}

/// Creates a right-handed perspective projection matrix with [-1,1] depth range.
/// This is the same as the OpenGL `glFurstum` function.
/// See <https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml>
#[inline]
#[must_use]
pub fn frustum_rh_gl(
left: {{ scalar_t }},
right: {{ scalar_t }},
bottom: {{ scalar_t }},
top: {{ scalar_t }},
z_near: {{ scalar_t }},
z_far: {{ scalar_t }},
) -> Self {
let inv_width = 1.0 / (right - left);
let inv_height = 1.0 / (top - bottom);
let inv_depth = 1.0 / (z_far - z_near);
let a = (right + left) * inv_width;
let b = (top + bottom) * inv_height;
let c = -(z_far + z_near) * inv_depth;
let d = -(2.0 * z_far * z_near) * inv_depth;
Self::from_cols(
{{ col_t }}::new((2.0 * z_near) * inv_width, 0.0, 0.0, 0.0),
{{ col_t }}::new(0.0, (2.0 * z_near) * inv_height, 0.0, 0.0),
{{ col_t }}::new(a, b, c, -1.0),
{{ col_t }}::new(0.0, 0.0, d, 0.0),
)
}

/// Creates a left-handed perspective projection matrix with `[0,1]` depth range.
///
/// # Panics
///
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
/// enabled.
#[inline]
#[must_use]
pub fn frustum_lh(
left: {{ scalar_t }},
right: {{ scalar_t }},
bottom: {{ scalar_t }},
top: {{ scalar_t }},
z_near: {{ scalar_t }},
z_far: {{ scalar_t }},
) -> Self {
glam_assert!(z_near > 0.0 && z_far > 0.0);
let inv_width = 1.0 / (right - left);
let inv_height = 1.0 / (top - bottom);
let inv_depth = 1.0 / (z_far - z_near);
let a = (right + left) * inv_width;
let b = (top + bottom) * inv_height;
let c = z_far * inv_depth;
let d = -(z_far * z_near) * inv_depth;
Self::from_cols(
{{ col_t }}::new((2.0 * z_near) * inv_width, 0.0, 0.0, 0.0),
{{ col_t }}::new(0.0, (2.0 * z_near) * inv_height, 0.0, 0.0),
{{ col_t }}::new(a, b, c, 1.0),
{{ col_t }}::new(0.0, 0.0, d, 0.0),
)
}

/// Creates a right-handed perspective projection matrix with `[0,1]` depth range.
///
/// # Panics
///
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
/// enabled.
#[inline]
#[must_use]
pub fn frustum_rh(
left: {{ scalar_t }},
right: {{ scalar_t }},
bottom: {{ scalar_t }},
top: {{ scalar_t }},
z_near: {{ scalar_t }},
z_far: {{ scalar_t }},
) -> Self {
glam_assert!(z_near > 0.0 && z_far > 0.0);
let inv_width = 1.0 / (right - left);
let inv_height = 1.0 / (top - bottom);
let inv_depth = 1.0 / (z_far - z_near);
let a = (right + left) * inv_width;
let b = (top + bottom) * inv_height;
let c = -z_far * inv_depth;
let d = -(z_far * z_near) * inv_depth;
Self::from_cols(
{{ col_t }}::new((2.0 * z_near) * inv_width, 0.0, 0.0, 0.0),
{{ col_t }}::new(0.0, (2.0 * z_near) * inv_height, 0.0, 0.0),
{{ col_t }}::new(a, b, c, -1.0),
{{ col_t }}::new(0.0, 0.0, d, 0.0),
)
}

/// Creates a right-handed perspective projection matrix with [-1,1] depth range.
/// This is the same as the OpenGL `gluPerspective` function.
/// See <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml>
Expand Down
92 changes: 92 additions & 0 deletions src/f32/coresimd/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,98 @@ impl Mat4 {
Self::look_to_rh(eye, center.sub(eye), up)
}

/// Creates a right-handed perspective projection matrix with [-1,1] depth range.
/// This is the same as the OpenGL `glFurstum` function.
/// See <https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml>
#[inline]
#[must_use]
pub fn frustum_rh_gl(
left: f32,
right: f32,
bottom: f32,
top: f32,
z_near: f32,
z_far: f32,
) -> Self {
let inv_width = 1.0 / (right - left);
let inv_height = 1.0 / (top - bottom);
let inv_depth = 1.0 / (z_far - z_near);
let a = (right + left) * inv_width;
let b = (top + bottom) * inv_height;
let c = -(z_far + z_near) * inv_depth;
let d = -(2.0 * z_far * z_near) * inv_depth;
Self::from_cols(
Vec4::new((2.0 * z_near) * inv_width, 0.0, 0.0, 0.0),
Vec4::new(0.0, (2.0 * z_near) * inv_height, 0.0, 0.0),
Vec4::new(a, b, c, -1.0),
Vec4::new(0.0, 0.0, d, 0.0),
)
}

/// Creates a left-handed perspective projection matrix with `[0,1]` depth range.
///
/// # Panics
///
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
/// enabled.
#[inline]
#[must_use]
pub fn frustum_lh(
left: f32,
right: f32,
bottom: f32,
top: f32,
z_near: f32,
z_far: f32,
) -> Self {
glam_assert!(z_near > 0.0 && z_far > 0.0);
let inv_width = 1.0 / (right - left);
let inv_height = 1.0 / (top - bottom);
let inv_depth = 1.0 / (z_far - z_near);
let a = (right + left) * inv_width;
let b = (top + bottom) * inv_height;
let c = z_far * inv_depth;
let d = -(z_far * z_near) * inv_depth;
Self::from_cols(
Vec4::new((2.0 * z_near) * inv_width, 0.0, 0.0, 0.0),
Vec4::new(0.0, (2.0 * z_near) * inv_height, 0.0, 0.0),
Vec4::new(a, b, c, 1.0),
Vec4::new(0.0, 0.0, d, 0.0),
)
}

/// Creates a right-handed perspective projection matrix with `[0,1]` depth range.
///
/// # Panics
///
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
/// enabled.
#[inline]
#[must_use]
pub fn frustum_rh(
left: f32,
right: f32,
bottom: f32,
top: f32,
z_near: f32,
z_far: f32,
) -> Self {
glam_assert!(z_near > 0.0 && z_far > 0.0);
let inv_width = 1.0 / (right - left);
let inv_height = 1.0 / (top - bottom);
let inv_depth = 1.0 / (z_far - z_near);
let a = (right + left) * inv_width;
let b = (top + bottom) * inv_height;
let c = -z_far * inv_depth;
let d = -(z_far * z_near) * inv_depth;
Self::from_cols(
Vec4::new((2.0 * z_near) * inv_width, 0.0, 0.0, 0.0),
Vec4::new(0.0, (2.0 * z_near) * inv_height, 0.0, 0.0),
Vec4::new(a, b, c, -1.0),
Vec4::new(0.0, 0.0, d, 0.0),
)
}

/// Creates a right-handed perspective projection matrix with [-1,1] depth range.
/// This is the same as the OpenGL `gluPerspective` function.
/// See <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml>
Expand Down
92 changes: 92 additions & 0 deletions src/f32/neon/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,98 @@ impl Mat4 {
Self::look_to_rh(eye, center.sub(eye), up)
}

/// Creates a right-handed perspective projection matrix with [-1,1] depth range.
/// This is the same as the OpenGL `glFurstum` function.
/// See <https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml>
#[inline]
#[must_use]
pub fn frustum_rh_gl(
left: f32,
right: f32,
bottom: f32,
top: f32,
z_near: f32,
z_far: f32,
) -> Self {
let inv_width = 1.0 / (right - left);
let inv_height = 1.0 / (top - bottom);
let inv_depth = 1.0 / (z_far - z_near);
let a = (right + left) * inv_width;
let b = (top + bottom) * inv_height;
let c = -(z_far + z_near) * inv_depth;
let d = -(2.0 * z_far * z_near) * inv_depth;
Self::from_cols(
Vec4::new((2.0 * z_near) * inv_width, 0.0, 0.0, 0.0),
Vec4::new(0.0, (2.0 * z_near) * inv_height, 0.0, 0.0),
Vec4::new(a, b, c, -1.0),
Vec4::new(0.0, 0.0, d, 0.0),
)
}

/// Creates a left-handed perspective projection matrix with `[0,1]` depth range.
///
/// # Panics
///
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
/// enabled.
#[inline]
#[must_use]
pub fn frustum_lh(
left: f32,
right: f32,
bottom: f32,
top: f32,
z_near: f32,
z_far: f32,
) -> Self {
glam_assert!(z_near > 0.0 && z_far > 0.0);
let inv_width = 1.0 / (right - left);
let inv_height = 1.0 / (top - bottom);
let inv_depth = 1.0 / (z_far - z_near);
let a = (right + left) * inv_width;
let b = (top + bottom) * inv_height;
let c = z_far * inv_depth;
let d = -(z_far * z_near) * inv_depth;
Self::from_cols(
Vec4::new((2.0 * z_near) * inv_width, 0.0, 0.0, 0.0),
Vec4::new(0.0, (2.0 * z_near) * inv_height, 0.0, 0.0),
Vec4::new(a, b, c, 1.0),
Vec4::new(0.0, 0.0, d, 0.0),
)
}

/// Creates a right-handed perspective projection matrix with `[0,1]` depth range.
///
/// # Panics
///
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
/// enabled.
#[inline]
#[must_use]
pub fn frustum_rh(
left: f32,
right: f32,
bottom: f32,
top: f32,
z_near: f32,
z_far: f32,
) -> Self {
glam_assert!(z_near > 0.0 && z_far > 0.0);
let inv_width = 1.0 / (right - left);
let inv_height = 1.0 / (top - bottom);
let inv_depth = 1.0 / (z_far - z_near);
let a = (right + left) * inv_width;
let b = (top + bottom) * inv_height;
let c = -z_far * inv_depth;
let d = -(z_far * z_near) * inv_depth;
Self::from_cols(
Vec4::new((2.0 * z_near) * inv_width, 0.0, 0.0, 0.0),
Vec4::new(0.0, (2.0 * z_near) * inv_height, 0.0, 0.0),
Vec4::new(a, b, c, -1.0),
Vec4::new(0.0, 0.0, d, 0.0),
)
}

/// Creates a right-handed perspective projection matrix with [-1,1] depth range.
/// This is the same as the OpenGL `gluPerspective` function.
/// See <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml>
Expand Down
92 changes: 92 additions & 0 deletions src/f32/scalar/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,98 @@ impl Mat4 {
Self::look_to_rh(eye, center.sub(eye), up)
}

/// Creates a right-handed perspective projection matrix with [-1,1] depth range.
/// This is the same as the OpenGL `glFurstum` function.
/// See <https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml>
#[inline]
#[must_use]
pub fn frustum_rh_gl(
left: f32,
right: f32,
bottom: f32,
top: f32,
z_near: f32,
z_far: f32,
) -> Self {
let inv_width = 1.0 / (right - left);
let inv_height = 1.0 / (top - bottom);
let inv_depth = 1.0 / (z_far - z_near);
let a = (right + left) * inv_width;
let b = (top + bottom) * inv_height;
let c = -(z_far + z_near) * inv_depth;
let d = -(2.0 * z_far * z_near) * inv_depth;
Self::from_cols(
Vec4::new((2.0 * z_near) * inv_width, 0.0, 0.0, 0.0),
Vec4::new(0.0, (2.0 * z_near) * inv_height, 0.0, 0.0),
Vec4::new(a, b, c, -1.0),
Vec4::new(0.0, 0.0, d, 0.0),
)
}

/// Creates a left-handed perspective projection matrix with `[0,1]` depth range.
///
/// # Panics
///
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
/// enabled.
#[inline]
#[must_use]
pub fn frustum_lh(
left: f32,
right: f32,
bottom: f32,
top: f32,
z_near: f32,
z_far: f32,
) -> Self {
glam_assert!(z_near > 0.0 && z_far > 0.0);
let inv_width = 1.0 / (right - left);
let inv_height = 1.0 / (top - bottom);
let inv_depth = 1.0 / (z_far - z_near);
let a = (right + left) * inv_width;
let b = (top + bottom) * inv_height;
let c = z_far * inv_depth;
let d = -(z_far * z_near) * inv_depth;
Self::from_cols(
Vec4::new((2.0 * z_near) * inv_width, 0.0, 0.0, 0.0),
Vec4::new(0.0, (2.0 * z_near) * inv_height, 0.0, 0.0),
Vec4::new(a, b, c, 1.0),
Vec4::new(0.0, 0.0, d, 0.0),
)
}

/// Creates a right-handed perspective projection matrix with `[0,1]` depth range.
///
/// # Panics
///
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
/// enabled.
#[inline]
#[must_use]
pub fn frustum_rh(
left: f32,
right: f32,
bottom: f32,
top: f32,
z_near: f32,
z_far: f32,
) -> Self {
glam_assert!(z_near > 0.0 && z_far > 0.0);
let inv_width = 1.0 / (right - left);
let inv_height = 1.0 / (top - bottom);
let inv_depth = 1.0 / (z_far - z_near);
let a = (right + left) * inv_width;
let b = (top + bottom) * inv_height;
let c = -z_far * inv_depth;
let d = -(z_far * z_near) * inv_depth;
Self::from_cols(
Vec4::new((2.0 * z_near) * inv_width, 0.0, 0.0, 0.0),
Vec4::new(0.0, (2.0 * z_near) * inv_height, 0.0, 0.0),
Vec4::new(a, b, c, -1.0),
Vec4::new(0.0, 0.0, d, 0.0),
)
}

/// Creates a right-handed perspective projection matrix with [-1,1] depth range.
/// This is the same as the OpenGL `gluPerspective` function.
/// See <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml>
Expand Down
Loading
Loading