diff --git a/gpu.h b/gpu.h index 8b0623e..e1e5a30 100644 --- a/gpu.h +++ b/gpu.h @@ -6,12 +6,18 @@ #define SWAPCHAIN_IMG_BUFFER_SIZE 4 -typedef float mat4_t[16]; - typedef struct { float x, y, z; } vec3_t; +typedef struct { + float x, y, z, w; +} vec4_t; + +typedef struct { + vec4_t x, y, z, w; +} mat4_t; + typedef struct { vec3_t position; vec3_t colour; diff --git a/main.c b/main.c index 37a3c46..5a2ab0b 100644 --- a/main.c +++ b/main.c @@ -31,55 +31,53 @@ typedef enum { static axis_t rot_axis = AXIS_Y; -// clang-format off #define ID \ { \ - 1, 0, 0, 0, \ - 0, 1, 0, 0, \ - 0, 0, 1, 0, \ - 0, 0, 0, 1, \ + { 1, 0, 0, 0 }, \ + { 0, 1, 0, 0 }, \ + { 0, 0, 1, 0 }, \ + { 0, 0, 0, 1 }, \ } #define SCALE(sf) \ { \ - sf, 0, 0, 0, \ - 0, sf, 0, 0, \ - 0, 0, sf, 0, \ - 0, 0, 0, 1, \ + { sf, 0, 0, 0 }, \ + { 0, sf, 0, 0 }, \ + { 0, 0, sf, 0 }, \ + { 0, 0, 0, 1 }, \ } #define TRANSLATE(x, y, z) \ { \ - 1, 0, 0, 0, \ - 0, 1, 0, 0, \ - 0, 0, 1, 0, \ - x, y, z, 1, \ + { 1, 0, 0, 0 }, \ + { 0, 1, 0, 0 }, \ + { 0, 0, 1, 0 }, \ + { x, y, z, 1 }, \ } #define ROT_X(a) \ { \ - 1, 0, 0, 0, \ - 0, cosf(a), sinf(a), 0, \ - 0, -sinf(a), cosf(a), 0, \ - 0, 0, 0, 1, \ + { 1, 0, 0, 0 }, \ + { 0, cosf(a), sinf(a), 0 }, \ + { 0, -sinf(a), cosf(a), 0 }, \ + { 0, 0, 0, 1 }, \ } #define ROT_Y(a) \ { \ - cosf(a), 0, -sinf(a), 0, \ - 0, 1, 0, 0, \ - sinf(a), 0, cosf(a), 0, \ - 0, 0, 0, 1, \ + { cosf(a), 0, -sinf(a), 0 }, \ + { 0, 1, 0, 0 }, \ + { sinf(a), 0, cosf(a), 0 }, \ + { 0, 0, 0, 1 }, \ } #define ROT_Z(a) \ { \ - cosf(a), sinf(a), 0, 0, \ - -sinf(a), cosf(a), 0, 0, \ - 0, 0, 1, 0, \ - 0, 0, 0, 1, \ + { cosf(a), sinf(a), 0, 0 }, \ + { -sinf(a), cosf(a), 0, 0 }, \ + { 0, 0, 1, 0 }, \ + { 0, 0, 0, 1 }, \ } -// clang-format on static void 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) { - for (unsigned x = 0; x < 4; ++x) { - out[y + 4 * x] = 0; - for (unsigned i = 0; i < 4; ++i) - out[y + 4 * x] += left[y + 4 * i] * right[i + 4 * x]; - } - } +#define ELEM(i, j) \ + m.x.i *n.j.x + m.y.i *n.j.y + m.z.i *n.j.z + m.w.i *n.j.w + return (mat4_t) { + { ELEM(x, x), ELEM(y, x), ELEM(z, x), ELEM(w, 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 t = r * aspect; - - // clang-format off - const mat4_t m = { - ncp / t, 0, 0, 0, - 0, -ncp / r, 0, 0, - 0, 0, fcp / (fcp - ncp), 1, - 0, 0, -fcp * ncp / (fcp - ncp), 0, + return (mat4_t) { + { ncp / t, 0, 0, 0 }, + { 0, -ncp / r, 0, 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() @@ -163,8 +158,8 @@ int main() mvp_t mvp = { .model = SCALE(2), .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 roty = ROT_Y(M_PI / 128); @@ -173,17 +168,15 @@ int main() while (!glfwWindowShouldClose(window)) { glfwPollEvents(); - mat4_t tmp; - memcpy(tmp, mvp.model, sizeof(mat4_t)); switch (rot_axis) { case AXIS_X: - mul(mvp.model, rotx, tmp); + mvp.model = mul(rotx, mvp.model); break; case AXIS_Y: - mul(mvp.model, roty, tmp); + mvp.model = mul(roty, mvp.model); break; case AXIS_Z: - mul(mvp.model, rotz, tmp); + mvp.model = mul(rotz, mvp.model); break; }