diff options
author | 3gg <3gg@shellblade.net> | 2023-02-17 08:52:12 -0800 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2023-02-17 08:52:12 -0800 |
commit | ebed78f26411a951fd2e94938b6d7aa99ca0fe39 (patch) | |
tree | 876d08fe89400c1f31eabfa578a1054c193dc009 /include | |
parent | fc175dbb72f80764431c1fe82ab8996b114831da (diff) |
Format.
Diffstat (limited to 'include')
-rw-r--r-- | include/math/mat4.h | 80 |
1 files 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 { | |||
23 | static inline mat4 mat4_make( | 23 | static inline mat4 mat4_make( |
24 | R m00, R m01, R m02, R m03, R m10, R m11, R m12, R m13, R m20, R m21, R m22, | 24 | R m00, R m01, R m02, R m03, R m10, R m11, R m12, R m13, R m20, R m21, R m22, |
25 | R m23, R m30, R m31, R m32, R m33) { | 25 | R m23, R m30, R m31, R m32, R m33) { |
26 | // clang-format off | ||
27 | // We store the matrix in columns, this is why the following looks flipped. | ||
26 | mat4 m; | 28 | mat4 m; |
27 | m.val[0][0] = m00; | 29 | m.val[0][0] = m00; m.val[0][1] = m10; m.val[0][2] = m20; m.val[0][3] = m30; |
28 | m.val[0][1] = m10; | 30 | m.val[1][0] = m01; m.val[1][1] = m11; m.val[1][2] = m21; m.val[1][3] = m31; |
29 | m.val[0][2] = m20; | 31 | m.val[2][0] = m02; m.val[2][1] = m12; m.val[2][2] = m22; m.val[2][3] = m32; |
30 | m.val[0][3] = m30; | 32 | m.val[3][0] = m03; m.val[3][1] = m13; m.val[3][2] = m23; m.val[3][3] = m33; |
31 | |||
32 | m.val[1][0] = m01; | ||
33 | m.val[1][1] = m11; | ||
34 | m.val[1][2] = m21; | ||
35 | m.val[1][3] = m31; | ||
36 | |||
37 | m.val[2][0] = m02; | ||
38 | m.val[2][1] = m12; | ||
39 | m.val[2][2] = m22; | ||
40 | m.val[2][3] = m32; | ||
41 | |||
42 | m.val[3][0] = m03; | ||
43 | m.val[3][1] = m13; | ||
44 | m.val[3][2] = m23; | ||
45 | m.val[3][3] = m33; | ||
46 | return m; | 33 | return m; |
34 | // clang-format on | ||
47 | } | 35 | } |
48 | 36 | ||
49 | /// Construct a matrix from a column-major matrix array. | 37 | /// Construct a matrix from a column-major matrix array. |
50 | static inline mat4 mat4_from_array(const R M[16]) { | 38 | static inline mat4 mat4_from_array(const R M[16]) { |
39 | // clang-format off | ||
51 | mat4 m; | 40 | mat4 m; |
52 | m.val[0][0] = M[0]; | 41 | m.val[0][0] = M[0]; m.val[0][1] = M[1]; m.val[0][2] = M[2]; m.val[0][3] = M[3]; |
53 | m.val[0][1] = M[1]; | 42 | m.val[1][0] = M[4]; m.val[1][1] = M[5]; m.val[1][2] = M[6]; m.val[1][3] = M[7]; |
54 | m.val[0][2] = M[2]; | 43 | m.val[2][0] = M[8]; m.val[2][1] = M[9]; m.val[2][2] = M[10]; m.val[2][3] = M[11]; |
55 | m.val[0][3] = M[3]; | 44 | m.val[3][0] = M[12]; m.val[3][1] = M[13]; m.val[3][2] = M[14]; m.val[3][3] = M[15]; |
56 | |||
57 | m.val[1][0] = M[4]; | ||
58 | m.val[1][1] = M[5]; | ||
59 | m.val[1][2] = M[6]; | ||
60 | m.val[1][3] = M[7]; | ||
61 | |||
62 | m.val[2][0] = M[8]; | ||
63 | m.val[2][1] = M[9]; | ||
64 | m.val[2][2] = M[10]; | ||
65 | m.val[2][3] = M[11]; | ||
66 | |||
67 | m.val[3][0] = M[12]; | ||
68 | m.val[3][1] = M[13]; | ||
69 | m.val[3][2] = M[14]; | ||
70 | m.val[3][3] = M[15]; | ||
71 | return m; | 45 | return m; |
46 | // clang-format on | ||
72 | } | 47 | } |
73 | 48 | ||
74 | /// Construct the identity matrix. | 49 | /// Construct the identity matrix. |
75 | static inline mat4 mat4_id() { | 50 | static inline mat4 mat4_id() { |
51 | // clang-format off | ||
76 | return mat4_make( | 52 | return mat4_make( |
77 | 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, | 53 | 1.0, 0.0, 0.0, 0.0, |
78 | 1.0); | 54 | 0.0, 1.0, 0.0, 0.0, |
55 | 0.0, 0.0, 1.0, 0.0, | ||
56 | 0.0, 0.0, 0.0, 1.0); | ||
57 | // clang-format on | ||
79 | } | 58 | } |
80 | 59 | ||
81 | /// Construct a matrix from 4 column vectors. | 60 | /// Construct a matrix from 4 column vectors. |
82 | static inline mat4 mat4_from_vec4(vec4 v0, vec4 v1, vec4 v2, vec4 v3) { | 61 | static inline mat4 mat4_from_vec4(vec4 v0, vec4 v1, vec4 v2, vec4 v3) { |
62 | // clang-format off | ||
83 | return mat4_make( | 63 | return mat4_make( |
84 | v0.x, v0.y, v0.z, v0.w, v1.x, v1.y, v1.z, v1.w, v2.x, v2.y, v2.z, v2.w, | 64 | v0.x, v1.x, v2.x, v3.x, |
85 | v3.x, v3.y, v3.z, v3.w); | 65 | v0.y, v1.y, v2.y, v3.y, |
66 | v0.z, v1.z, v2.z, v3.z, | ||
67 | 0,0,0,1); | ||
68 | // clang-format on | ||
86 | } | 69 | } |
87 | 70 | ||
88 | /// Construct a transformation matrix from 4 vectors. | 71 | /// Construct a transformation matrix from a position and vectors forming a |
72 | /// coordinate system. | ||
89 | static inline mat4 mat4_from_vec3( | 73 | static inline mat4 mat4_from_vec3( |
90 | vec3 right, vec3 up, vec3 forward, vec3 position) { | 74 | vec3 right, vec3 up, vec3 forward, vec3 position) { |
75 | // clang-format off | ||
91 | return mat4_make( | 76 | return mat4_make( |
92 | right.x, right.y, right.z, 0.0, up.x, up.y, up.z, 0.0, forward.x, | 77 | right.x, up.x, forward.x, position.x, |
93 | forward.y, forward.z, 0.0, position.x, position.y, position.z, 1.0); | 78 | right.y, up.y, forward.y, position.y, |
79 | right.z, up.z, forward.z, position.z, | ||
80 | 0, 0, 0, 1); | ||
81 | // clant-format on | ||
94 | } | 82 | } |
95 | 83 | ||
96 | /// Return the value at the specified position. | 84 | /// Return the value at the specified position. |
@@ -309,7 +297,7 @@ static inline mat4 mat4_lookat(vec3 position, vec3 target, vec3 up) { | |||
309 | const vec3 fwd = vec3_normalize(vec3_sub(target, position)); | 297 | const vec3 fwd = vec3_normalize(vec3_sub(target, position)); |
310 | const vec3 right = vec3_normalize(vec3_cross(fwd, up)); | 298 | const vec3 right = vec3_normalize(vec3_cross(fwd, up)); |
311 | up = vec3_normalize(vec3_cross(right, fwd)); | 299 | up = vec3_normalize(vec3_cross(right, fwd)); |
312 | return mat4_from_vec3(right, up, fwd, position); | 300 | return mat4_from_vec3(right, up, vec3_neg(fwd), position); |
313 | } | 301 | } |
314 | 302 | ||
315 | /// Create an orthographic projection matrix. | 303 | /// Create an orthographic projection matrix. |