Files
gpu-renderer/main.c
T

116 lines
2.6 KiB
C

#include "animation.h"
#include "gpu.h"
#include "model_format_glb.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define W 800
#define H 600
#define DT (1'000'000 / 60) // Assume 60 Hz for now
#define LIL_GUY_COUNT 2
static void
key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
(void)window;
(void)scancode;
(void)mods;
if (action != GLFW_PRESS)
return;
switch (key) {
case GLFW_KEY_Q:
glfwSetWindowShouldClose(window, GL_TRUE);
break;
}
}
int main(int argc, char *argv[])
{
// Read model from passed path
if (argc != 2) {
fprintf(stderr, "Usage: %s PATH/TO/MODEL\n", argv[0]);
return 1;
}
model_t model = model_format_glb_read(argv[1]);
// Set up window
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
GLFWwindow *window = glfwCreateWindow(W, H, "Foo", nullptr, nullptr);
assert(window);
// Register input callbacks
glfwSetKeyCallback(window, key_callback);
// Initialise GPU and load model
gpu_ctx_t gpu;
gpu_model_t gpu_model1, gpu_model2;
gpu_init(&gpu, window, "Foo");
gpu_model_load(&gpu, &model, &gpu_model1);
gpu_model_load(&gpu, &model, &gpu_model2);
// Initialise animation
animation_state_t anim = animation_init(&model);
const vec3_t cam = { 0, 4, -4 };
const vec3_t tgt = { 0, 0.5, 0 };
const vp_t vp = {
.view = mat4_lookat(cam, tgt, (vec3_t) { 0, 1, 0 }),
.proj = mat4_perspective((float)W / H, PI / 8, 0.1, 10.0),
};
mat4_t transform1 = mat4_mul(
MAT4_TRANSLATE(1, 0, 0),
(mat4_t) {
{ 0, 0, -1, 0 },
{ -1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 0, 1 },
});
mat4_t transform2 = mat4_mul(
MAT4_TRANSLATE(2, 0, 0),
mat4_mul(
MAT4_ROTATE_Y(PI),
(mat4_t) {
{ 0, 0, -1, 0 },
{ -1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 0, 1 },
}));
const mat4_t rot1 = MAT4_ROTATE_Y(PI / 170);
const mat4_t rot2 = MAT4_ROTATE_Y(-PI / 340);
animation_start(&anim, &model, 0);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
transform1 = mat4_mul(rot1, transform1);
transform2 = mat4_mul(rot2, transform2);
animation_update(&anim, &model, DT);
gpu_render_begin(&gpu, vp);
gpu_render_model(&gpu, &gpu_model1, anim.bone_matrices, transform1);
gpu_render_model(&gpu, &gpu_model2, anim.bone_matrices, transform2);
gpu_render_submit(&gpu);
}
// Clean up
animation_cleanup(&anim);
gpu_model_cleanup(&gpu, &gpu_model1);
gpu_model_cleanup(&gpu, &gpu_model2);
gpu_cleanup(&gpu);
model_cleanup(&model);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}