Use struct for mat4_t instead of array

This commit is contained in:
Camden Dixie O'Brien committed 2025-09-28 01:46:59 +01:00
1 parent 6a5dba2600
commit 59f584d1d4
2 files changed
+52 -53

No files matched your search

+8 -2
View File
@@ -6,12 +6,18 @@
#define SWAPCHAIN_IMG_BUFFER_SIZE 4 #define SWAPCHAIN_IMG_BUFFER_SIZE 4
typedef float mat4_t[16];
typedef struct { typedef struct {
float x, y, z; float x, y, z;
} vec3_t; } vec3_t;
typedef struct {
float x, y, z, w;
} vec4_t;
typedef struct {
vec4_t x, y, z, w;
} mat4_t;
typedef struct { typedef struct {
vec3_t position; vec3_t position;
vec3_t colour; vec3_t colour;
+44 -51
View File
@@ -31,55 +31,53 @@ typedef enum {
static axis_t rot_axis = AXIS_Y; static axis_t rot_axis = AXIS_Y;
// clang-format off
#define ID \ #define ID \
{ \ { \
1, 0, 0, 0, \ { 1, 0, 0, 0 }, \
0, 1, 0, 0, \ { 0, 1, 0, 0 }, \
0, 0, 1, 0, \ { 0, 0, 1, 0 }, \
0, 0, 0, 1, \ { 0, 0, 0, 1 }, \
} }
#define SCALE(sf) \ #define SCALE(sf) \
{ \ { \
sf, 0, 0, 0, \ { sf, 0, 0, 0 }, \
0, sf, 0, 0, \ { 0, sf, 0, 0 }, \
0, 0, sf, 0, \ { 0, 0, sf, 0 }, \
0, 0, 0, 1, \ { 0, 0, 0, 1 }, \
} }
#define TRANSLATE(x, y, z) \ #define TRANSLATE(x, y, z) \
{ \ { \
1, 0, 0, 0, \ { 1, 0, 0, 0 }, \
0, 1, 0, 0, \ { 0, 1, 0, 0 }, \
0, 0, 1, 0, \ { 0, 0, 1, 0 }, \
x, y, z, 1, \ { x, y, z, 1 }, \
} }
#define ROT_X(a) \ #define ROT_X(a) \
{ \ { \
1, 0, 0, 0, \ { 1, 0, 0, 0 }, \
0, cosf(a), sinf(a), 0, \ { 0, cosf(a), sinf(a), 0 }, \
0, -sinf(a), cosf(a), 0, \ { 0, -sinf(a), cosf(a), 0 }, \
0, 0, 0, 1, \ { 0, 0, 0, 1 }, \
} }
#define ROT_Y(a) \ #define ROT_Y(a) \
{ \ { \
cosf(a), 0, -sinf(a), 0, \ { cosf(a), 0, -sinf(a), 0 }, \
0, 1, 0, 0, \ { 0, 1, 0, 0 }, \
sinf(a), 0, cosf(a), 0, \ { sinf(a), 0, cosf(a), 0 }, \
0, 0, 0, 1, \ { 0, 0, 0, 1 }, \
} }
#define ROT_Z(a) \ #define ROT_Z(a) \
{ \ { \
cosf(a), sinf(a), 0, 0, \ { cosf(a), sinf(a), 0, 0 }, \
-sinf(a), cosf(a), 0, 0, \ { -sinf(a), cosf(a), 0, 0 }, \
0, 0, 1, 0, \ { 0, 0, 1, 0 }, \
0, 0, 0, 1, \ { 0, 0, 0, 1 }, \
} }
// clang-format on
static void static void
key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
@@ -108,32 +106,29 @@ key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
} }
} }
static void mul(mat4_t out, const mat4_t left, const mat4_t right) static mat4_t mul(mat4_t m, mat4_t n)
{ {
for (unsigned y = 0; y < 4; ++y) { #define ELEM(i, j) \
for (unsigned x = 0; x < 4; ++x) { m.x.i *n.j.x + m.y.i *n.j.y + m.z.i *n.j.z + m.w.i *n.j.w
out[y + 4 * x] = 0; return (mat4_t) {
for (unsigned i = 0; i < 4; ++i) { ELEM(x, x), ELEM(y, x), ELEM(z, x), ELEM(w, x) },
out[y + 4 * x] += left[y + 4 * i] * right[i + 4 * x]; { ELEM(x, y), ELEM(y, y), ELEM(z, y), ELEM(w, y) },
} { ELEM(x, z), ELEM(y, z), ELEM(z, z), ELEM(w, z) },
} { ELEM(x, w), ELEM(y, w), ELEM(z, w), ELEM(w, w) },
};
#undef MUL_ELEM
} }
static void persp(mat4_t out, float aspect, float fov, float ncp, float fcp) static mat4_t persp(float aspect, float fov, float ncp, float fcp)
{ {
const float r = 2 * ncp * tanf(fov / 2); const float r = 2 * ncp * tanf(fov / 2);
const float t = r * aspect; const float t = r * aspect;
return (mat4_t) {
// clang-format off { ncp / t, 0, 0, 0 },
const mat4_t m = { { 0, -ncp / r, 0, 0 },
ncp / t, 0, 0, 0, { 0, 0, fcp / (fcp - ncp), 1 },
0, -ncp / r, 0, 0, { 0, 0, -fcp * ncp / (fcp - ncp), 0 },
0, 0, fcp / (fcp - ncp), 1,
0, 0, -fcp * ncp / (fcp - ncp), 0,
}; };
// clang-format on
memcpy(out, m, sizeof(mat4_t));
} }
int main() int main()
@@ -163,8 +158,8 @@ int main()
mvp_t mvp = { mvp_t mvp = {
.model = SCALE(2), .model = SCALE(2),
.view = TRANSLATE(0, 0, 4.0), .view = TRANSLATE(0, 0, 4.0),
.proj = persp((float)W / H, M_PI / 6, 0.1, 10.0),
}; };
persp(mvp.proj, (float)W / H, M_PI / 6, 0.1, 10.0);
const mat4_t rotx = ROT_X(M_PI / 128); const mat4_t rotx = ROT_X(M_PI / 128);
const mat4_t roty = ROT_Y(M_PI / 128); const mat4_t roty = ROT_Y(M_PI / 128);
@@ -173,17 +168,15 @@ int main()
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
glfwPollEvents(); glfwPollEvents();
mat4_t tmp;
memcpy(tmp, mvp.model, sizeof(mat4_t));
switch (rot_axis) { switch (rot_axis) {
case AXIS_X: case AXIS_X:
mul(mvp.model, rotx, tmp); mvp.model = mul(rotx, mvp.model);
break; break;
case AXIS_Y: case AXIS_Y:
mul(mvp.model, roty, tmp); mvp.model = mul(roty, mvp.model);
break; break;
case AXIS_Z: case AXIS_Z:
mul(mvp.model, rotz, tmp); mvp.model = mul(rotz, mvp.model);
break; break;
} }