From ebed78f26411a951fd2e94938b6d7aa99ca0fe39 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 17 Feb 2023 08:52:12 -0800 Subject: Format. --- include/math/mat4.h | 80 +++++++++++++++++++++++------------------------------ 1 file changed, 34 insertions(+), 46 deletions(-) diff --git a/include/math/mat4.h b/include/math/mat4.h index e808d73..f3601b6 100644 --- a/include/math/mat4.h +++ b/include/math/mat4.h @@ -23,74 +23,62 @@ typedef struct mat4 { static inline mat4 mat4_make( R m00, R m01, R m02, R m03, R m10, R m11, R m12, R m13, R m20, R m21, R m22, R m23, R m30, R m31, R m32, R m33) { + // clang-format off + // We store the matrix in columns, this is why the following looks flipped. mat4 m; - m.val[0][0] = m00; - m.val[0][1] = m10; - m.val[0][2] = m20; - m.val[0][3] = m30; - - m.val[1][0] = m01; - m.val[1][1] = m11; - m.val[1][2] = m21; - m.val[1][3] = m31; - - m.val[2][0] = m02; - m.val[2][1] = m12; - m.val[2][2] = m22; - m.val[2][3] = m32; - - m.val[3][0] = m03; - m.val[3][1] = m13; - m.val[3][2] = m23; - m.val[3][3] = m33; + m.val[0][0] = m00; m.val[0][1] = m10; m.val[0][2] = m20; m.val[0][3] = m30; + m.val[1][0] = m01; m.val[1][1] = m11; m.val[1][2] = m21; m.val[1][3] = m31; + m.val[2][0] = m02; m.val[2][1] = m12; m.val[2][2] = m22; m.val[2][3] = m32; + m.val[3][0] = m03; m.val[3][1] = m13; m.val[3][2] = m23; m.val[3][3] = m33; return m; + // clang-format on } /// Construct a matrix from a column-major matrix array. static inline mat4 mat4_from_array(const R M[16]) { + // clang-format off mat4 m; - m.val[0][0] = M[0]; - m.val[0][1] = M[1]; - m.val[0][2] = M[2]; - m.val[0][3] = M[3]; - - m.val[1][0] = M[4]; - m.val[1][1] = M[5]; - m.val[1][2] = M[6]; - m.val[1][3] = M[7]; - - m.val[2][0] = M[8]; - m.val[2][1] = M[9]; - m.val[2][2] = M[10]; - m.val[2][3] = M[11]; - - m.val[3][0] = M[12]; - m.val[3][1] = M[13]; - m.val[3][2] = M[14]; - m.val[3][3] = M[15]; + m.val[0][0] = M[0]; m.val[0][1] = M[1]; m.val[0][2] = M[2]; m.val[0][3] = M[3]; + m.val[1][0] = M[4]; m.val[1][1] = M[5]; m.val[1][2] = M[6]; m.val[1][3] = M[7]; + m.val[2][0] = M[8]; m.val[2][1] = M[9]; m.val[2][2] = M[10]; m.val[2][3] = M[11]; + m.val[3][0] = M[12]; m.val[3][1] = M[13]; m.val[3][2] = M[14]; m.val[3][3] = M[15]; return m; + // clang-format on } /// Construct the identity matrix. static inline mat4 mat4_id() { + // clang-format off return mat4_make( - 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, - 1.0); + 1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0); + // clang-format on } /// Construct a matrix from 4 column vectors. static inline mat4 mat4_from_vec4(vec4 v0, vec4 v1, vec4 v2, vec4 v3) { + // clang-format off return mat4_make( - v0.x, v0.y, v0.z, v0.w, v1.x, v1.y, v1.z, v1.w, v2.x, v2.y, v2.z, v2.w, - v3.x, v3.y, v3.z, v3.w); + v0.x, v1.x, v2.x, v3.x, + v0.y, v1.y, v2.y, v3.y, + v0.z, v1.z, v2.z, v3.z, + 0,0,0,1); + // clang-format on } -/// Construct a transformation matrix from 4 vectors. +/// Construct a transformation matrix from a position and vectors forming a +/// coordinate system. static inline mat4 mat4_from_vec3( vec3 right, vec3 up, vec3 forward, vec3 position) { + // clang-format off return mat4_make( - right.x, right.y, right.z, 0.0, up.x, up.y, up.z, 0.0, forward.x, - forward.y, forward.z, 0.0, position.x, position.y, position.z, 1.0); + right.x, up.x, forward.x, position.x, + right.y, up.y, forward.y, position.y, + right.z, up.z, forward.z, position.z, + 0, 0, 0, 1); + // clant-format on } /// Return the value at the specified position. @@ -309,7 +297,7 @@ static inline mat4 mat4_lookat(vec3 position, vec3 target, vec3 up) { const vec3 fwd = vec3_normalize(vec3_sub(target, position)); const vec3 right = vec3_normalize(vec3_cross(fwd, up)); up = vec3_normalize(vec3_cross(right, fwd)); - return mat4_from_vec3(right, up, fwd, position); + return mat4_from_vec3(right, up, vec3_neg(fwd), position); } /// Create an orthographic projection matrix. -- cgit v1.2.3