From 73d6575e4d36f5c6ba1e403107ebcb45d072a8a1 Mon Sep 17 00:00:00 2001 From: Caleb Gingles Date: Sun, 19 Jul 2026 14:36:08 -0400 Subject: [PATCH 1/2] Separate -1 to 1 and 0 to 1 versions of perspective matrices --- src/mat4.zig | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/mat4.zig b/src/mat4.zig index 952668e..a276a2b 100644 --- a/src/mat4.zig +++ b/src/mat4.zig @@ -264,10 +264,14 @@ pub fn Mat4x4(comptime T: type) type { return Vector3.new(scale_x.length(), scale_y.length(), scale_z.length()); } - /// Construct a perspective 4x4 matrix. + // Use depth -1 to 1 for perspective matrix by default, to match OpenGL defaults + pub const perspective = perspectiveNegativeOne; + pub const perspectiveReversedZ = perspectiveReversedZNegativeOne; + + /// Construct a perspective 4x4 matrix, with a depth from -1 to 1. /// Note: Field of view is given in degrees. /// Also for more details https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml. - pub fn perspective(fovy_in_degrees: T, aspect_ratio: T, z_near: T, z_far: T) Self { + pub fn perspectiveNegativeOne(fovy_in_degrees: T, aspect_ratio: T, z_near: T, z_far: T) Self { const f = 1 / @tan(root.toRadians(fovy_in_degrees) * 0.5); const nf = 1 / (z_near - z_far); @@ -281,11 +285,45 @@ pub fn Mat4x4(comptime T: type) type { }; } - /// Construct a perspective 4x4 matrix with reverse Z and infinite far plane. + /// Construct a perspective 4x4 matrix, with a depth from 0 to 1. + /// Note: Field of view is given in degrees. + /// Also for more details https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml. + pub fn perspectiveZeroOne(fovy_in_degrees: T, aspect_ratio: T, z_near: T, z_far: T) Self { + const f = 1 / @tan(root.toRadians(fovy_in_degrees) * 0.5); + const nf = 1 / (z_near - z_far); + + return .{ + .data = .{ + .{ f / aspect_ratio, 0, 0, 0 }, + .{ 0, f, 0, 0 }, + .{ 0, 0, z_far * nf, -1 }, + .{ 0, 0, z_near * z_far * nf, 0 }, + }, + }; + } + + /// Construct a perspective 4x4 matrix with reverse Z and infinite far plane, with a depth from -1 to 1. + /// Note: Field of view is given in degrees. + /// Also for more details https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml. + /// For Reversed-Z details https://nlguillemot.wordpress.com/2016/12/07/reversed-z-in-opengl/ + pub fn perspectiveReversedZNegativeOne(fovy_in_degrees: T, aspect_ratio: T, z_near: T) Self { + const f = 1 / @tan(root.toRadians(fovy_in_degrees) * 0.5); + + return .{ + .data = .{ + .{ f / aspect_ratio, 0, 0, 0 }, + .{ 0, f, 0, 0 }, + .{ 0, 0, 1, -1 }, + .{ 0, 0, 2 * z_near, 0 }, + }, + }; + } + + /// Construct a perspective 4x4 matrix with reverse Z and infinite far plane, with a depth from 0 to 1. /// Note: Field of view is given in degrees. /// Also for more details https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml. /// For Reversed-Z details https://nlguillemot.wordpress.com/2016/12/07/reversed-z-in-opengl/ - pub fn perspectiveReversedZ(fovy_in_degrees: T, aspect_ratio: T, z_near: T) Self { + pub fn perspectiveReversedZZeroOne(fovy_in_degrees: T, aspect_ratio: T, z_near: T) Self { const f = 1 / @tan(root.toRadians(fovy_in_degrees) * 0.5); return .{ From c40617a8a54b4260866ae2380d9792039bee347f Mon Sep 17 00:00:00 2001 From: Caleb Gingles Date: Mon, 20 Jul 2026 08:33:20 -0400 Subject: [PATCH 2/2] Update README --- README.md | 109 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 56 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 79cd6cf..718840c 100644 --- a/README.md +++ b/README.md @@ -34,29 +34,30 @@ pub fn main () void { ### Aliases -| Type | Description | -| ------------ | ---------------------------------------- | -| Vec2 | Two dimensional vector for `f32` | -| Vec2_f64 | Two dimensional vector for `f64` | -| Vec2_i32 | Two dimensional vector for `i32` | -| Vec2_usize | Two dimensional vector for `usize` | -| Vec3 | Three dimensional vector for `f32` | -| Vec3_f64 | Three dimensional vector for `f64` | -| Vec3_i32 | Three dimensional vector for `i32` | -| Vec3_usize | Three dimensional vector for `usize` | -| Vec4 | Four dimensional vector for `f32` | -| Vec4_f64 | Four dimensional vector for `f64` | -| Vec4_i32 | Four dimensional vector for `i32` | -| Vec4_usize | Four dimensional vector for `usize` | -| Mat3 | 3x3 matrix for `f32` | -| Mat3_f64 | 3x3 matrix for `f64` | -| Mat4 | 4x4 matrix for `f32` | -| Mat4_f64 | 4x4 matrix for `f64` | -| Quat | Quaternion for `f32` | -| Quat_f64 | Quaternion for `f64` | -| perspective | Perspective function for `f32` 4x4 mat4 | -| orthographic | Orthographic function for `f32` 4x4 mat4 | -| lookAt | LookAt function for `f32` 4x4 mat4 | +| Type | Description | +| -------------------- | --------------------------------------------------------------------- | +| Vec2 | Two dimensional vector for `f32` | +| Vec2_f64 | Two dimensional vector for `f64` | +| Vec2_i32 | Two dimensional vector for `i32` | +| Vec2_usize | Two dimensional vector for `usize` | +| Vec3 | Three dimensional vector for `f32` | +| Vec3_f64 | Three dimensional vector for `f64` | +| Vec3_i32 | Three dimensional vector for `i32` | +| Vec3_usize | Three dimensional vector for `usize` | +| Vec4 | Four dimensional vector for `f32` | +| Vec4_f64 | Four dimensional vector for `f64` | +| Vec4_i32 | Four dimensional vector for `i32` | +| Vec4_usize | Four dimensional vector for `usize` | +| Mat3 | 3x3 matrix for `f32` | +| Mat3_f64 | 3x3 matrix for `f64` | +| Mat4 | 4x4 matrix for `f32` | +| Mat4_f64 | 4x4 matrix for `f64` | +| Quat | Quaternion for `f32` | +| Quat_f64 | Quaternion for `f64` | +| perspective | Perspective function for `f32` 4x4 mat4 with -1 to 1 depth | +| perspectiveReversedZ | Reversed-Z perspective function for `f32` 4x4 mat4 with -1 to 1 depth | +| orthographic | Orthographic function for `f32` 4x4 mat4 | +| lookAt | LookAt function for `f32` 4x4 mat4 | ### Vectors @@ -105,36 +106,38 @@ pub fn main () void { Note: All matrices are column-major. -| Methods | Description | -| -------------------- | ------------------------------------------------------------------------------------- | -| identity | Construct an identity matrix | -| set | Set all matrix values to given value | -| fromSlice | Construct new matrix from given slice of data | -| getSlice | Return the inner data as a slice | -| transpose | Return the transpose matrix | -| negate | Scale all components by -1 | -| cast | Cast a type to another type | -| eql | Return `true` if two matrices are equals | -| mulByVec4 | Multiply a given vec4 by matrix (only for mat4) | -| fromTranslate | Construct a translation matrix | -| translate | Construct a translation from the given matrix according to given axis (vec3) | -| fromRotation | Construct a rotation matrix | -| fromEulerAngles | Construct a rotation matrix from pitch/yaw/roll in degrees (X _ Y _ Z) | -| rotate | Construct a rotation from the given matrix according to given axis (vec3) | -| fromScale | Construct a scale matrix | -| scale | Construct a scale from the given matrix according to given axis (vec3) | -| extractTranslation | Return a vector with proper translation | -| orthoNormalize | Ortho normalize the given matrix. | -| extractEulerAngles | Return a vector with Euler angles in degrees (pitch/yaw/roll) | -| extractScale | Return a vector with proper scale | -| perspective | Construct a perspective matrix from given fovy, aspect ratio, near/far inputs | -| perspectiveReversedZ | Construct a perspective matrix with reverse Z and infinite far plane. | -| orthographic | Construct an orthographic matrix from given left, right, bottom, top, near/far inputs | -| lookAt | Construct a right-handed lookAt matrix from given position (eye) and target | -| mul | Multiply two matrices | -| inv | Inverse the given matrix | -| recompose | Return mat4 matrix from given `translation`, `rotation` and `scale` components | -| decompose | Return components `translation`, `rotation` and `scale` from given 4x4 matrix. | +| Methods | Description | +| ------------------------------- | ------------------------------------------------------------------------------------------------------- | +| identity | Construct an identity matrix | +| set | Set all matrix values to given value | +| fromSlice | Construct new matrix from given slice of data | +| getSlice | Return the inner data as a slice | +| transpose | Return the transpose matrix | +| negate | Scale all components by -1 | +| cast | Cast a type to another type | +| eql | Return `true` if two matrices are equals | +| mulByVec4 | Multiply a given vec4 by matrix (only for mat4) | +| fromTranslate | Construct a translation matrix | +| translate | Construct a translation from the given matrix according to given axis (vec3) | +| fromRotation | Construct a rotation matrix | +| fromEulerAngles | Construct a rotation matrix from pitch/yaw/roll in degrees (X _ Y _ Z) | +| rotate | Construct a rotation from the given matrix according to given axis (vec3) | +| fromScale | Construct a scale matrix | +| scale | Construct a scale from the given matrix according to given axis (vec3) | +| extractTranslation | Return a vector with proper translation | +| orthoNormalize | Ortho normalize the given matrix. | +| extractEulerAngles | Return a vector with Euler angles in degrees (pitch/yaw/roll) | +| extractScale | Return a vector with proper scale | +| perspectiveNegativeOne | Construct a perspective matrix from given fovy, aspect ratio, near/far inputs, with -1 to 1 depth range | +| perspectiveZeroOne | Construct a perspective matrix from given fovy, aspect ratio, near/far inputs, with 0 to 1 depth range | +| perspectiveReversedZNegativeOne | Construct a perspective matrix with reverse Z and infinite far plane, with -1 to 1 depth range | +| perspectiveReversedZZeroOne | Construct a perspective matrix with reverse Z and infinite far plane, with 0 to 1 depth range | +| orthographic | Construct an orthographic matrix from given left, right, bottom, top, near/far inputs | +| lookAt | Construct a right-handed lookAt matrix from given position (eye) and target | +| mul | Multiply two matrices | +| inv | Inverse the given matrix | +| recompose | Return mat4 matrix from given `translation`, `rotation` and `scale` components | +| decompose | Return components `translation`, `rotation` and `scale` from given 4x4 matrix. | ### Quaternions