From 94540a43327a2f7908ac95ab157ace30508925c7 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Fri, 8 Sep 2023 17:53:37 -0700 Subject: [PATCH] math: add orthographic projection Signed-off-by: Stephen Gutekanst --- src/math/mat.zig | 64 +++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/src/math/mat.zig b/src/math/mat.zig index 610907d9..c02f7a62 100644 --- a/src/math/mat.zig +++ b/src/math/mat.zig @@ -135,6 +135,39 @@ pub fn Mat( pub inline fn translateScalar(scalar: Vec.T) Matrix { return translate(Vec.splat(scalar)); } + + /// Constructs an orthographic projection matrix; an orthogonal transformation matrix + /// which transforms from the given left, right, bottom, and top dimensions into + /// `(-1, +1)` in `(x, y)`, and `(0, +1)` in `z`. + /// + /// The near/far parameters denotes the depth (z coordinate) of the near/far clipping + /// plane. + /// + /// Returns an orthographic projection matrix. + // TODO: needs tests + pub inline fn ortho( + /// The sides of the near clipping plane viewport + left: f32, + right: f32, + bottom: f32, + top: f32, + /// The depth (z coordinate) of the near/far clipping plane. + near: f32, + far: f32, + ) Matrix { + const xx = 2 / (right - left); + const yy = 2 / (top - bottom); + const zz = 1 / (near - far); + const tx = (right + left) / (left - right); + const ty = (top + bottom) / (bottom - top); + const tz = near / (near - far); + return init( + RowVec.init(xx, 0, 0, 0), + RowVec.init(0, yy, 0, 0), + RowVec.init(0, 0, zz, 0), + RowVec.init(tx, ty, tz, 1), + ); + } }, else => @compileError("Expected Mat3x3, Mat4x4 found '" ++ @typeName(Matrix) ++ "'"), }; @@ -143,37 +176,6 @@ pub fn Mat( // to work with this new Mat approach, swapping f32 for the generic T float type, moving 3x3 // and 4x4 specific functions into the mixin above, writing new tests, etc. - // /// Constructs an orthographic projection matrix; an orthogonal transformation matrix which - // /// transforms from the given left, right, bottom, and top dimensions into -1 +1 in x and y, - // /// and 0 to +1 in z. - // /// - // /// The near/far parameters denotes the depth (z coordinate) of the near/far clipping plane. - // /// - // /// Returns an orthographic projection matrix. - // pub inline fn ortho( - // /// The sides of the near clipping plane viewport - // left: f32, - // right: f32, - // bottom: f32, - // top: f32, - // /// The depth (z coordinate) of the near/far clipping plane. - // near: f32, - // far: f32, - // ) Mat4x4 { - // const xx = 2 / (right - left); - // const yy = 2 / (top - bottom); - // const zz = 1 / (near - far); - // const tx = (right + left) / (left - right); - // const ty = (top + bottom) / (bottom - top); - // const tz = near / (near - far); - // return init(Mat4x4, .{ - // xx, 0, 0, 0, - // 0, yy, 0, 0, - // 0, 0, zz, 0, - // tx, ty, tz, 1, - // }); - // } - // /// Returns the translation component of the 2D matrix. // pub inline fn translation2d(v: Mat3x3) Vec2 { // return .{ mat.index(v, 8), mat.index(v, 9) };