Add MVP projection passed through push constant

This commit is contained in:
Camden Dixie O'Brien committed 2025-09-27 14:18:32 +01:00
1 parent c770895217
commit 3d10fe76ce
4 files changed
+103 -12

No files matched your search

+69 -6
View File
@@ -2,16 +2,56 @@
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#ifndef M_PI
#define M_PI 3.14159265358979324
#endif
static const vertex_t vertices[] = {
{ { -0.5, -0.5 }, { 1.0, 0.0, 0.0 } },
{ { 0.5, -0.5 }, { 0.0, 1.0, 0.0 } },
{ { 0.5, 0.5 }, { 0.0, 0.0, 1.0 } },
{ { -0.5, 0.5 }, { 1.0, 1.0, 1.0 } },
{ { -1, -1 }, { 1, 0, 0 } },
{ { 1, -1 }, { 0, 1, 0 } },
{ { 1, 1 }, { 0, 0, 1 } },
{ { -1, 1 }, { 1, 1, 1 } },
};
static const uint16_t indices[] = { 0, 1, 2, 2, 3, 0 };
// clang-format off
#define ID \
{ \
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, \
}
#define TRANSLATE(x, y, z) \
{ \
1, 0, 0, 0, \
0, 1, 0, 0, \
0, 0, 1, 0, \
x, y, z, 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, \
}
// clang-format on
static void
key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
@@ -27,13 +67,24 @@ 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)
{
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];
}
}
}
int main()
{
// Set up window
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
GLFWwindow *window = glfwCreateWindow(800, 600, "Foo", nullptr, nullptr);
GLFWwindow *window = glfwCreateWindow(800, 800, "Foo", nullptr, nullptr);
assert(window);
// Register input callbacks
@@ -51,9 +102,21 @@ int main()
gpu_ctx_t gpu;
gpu_init(&gpu, &gpu_config);
mvp_t mvp = {
.model = SCALE(0.2),
.view = TRANSLATE(0, 0, 0.5),
.proj = ID,
};
const mat4_t rot = ROT_Y(M_PI / 128);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
gpu_render(&gpu);
mat4_t tmp;
memcpy(tmp, mvp.model, sizeof(mat4_t));
mul(mvp.model, rot, tmp);
gpu_render(&gpu, &mvp);
}
// Clean up