Implement configurable lighting

This commit is contained in:
Camden Dixie O'Brien committed 2025-10-05 13:51:14 +01:00
1 parent 070758b6cc
commit b5bda150d2
6 files changed
+148 -49

No files matched your search

+21 -9
View File
@@ -7,13 +7,15 @@
#include <stdio.h>
#include <string.h>
#define W 800
#define H 600
#define W 1280
#define H 720
#define DT (1'000'000 / 60) // Assume 60 Hz for now
#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))
static light_params_t light_params;
static void
key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
@@ -28,6 +30,17 @@ key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
case GLFW_KEY_Q:
glfwSetWindowShouldClose(window, GL_TRUE);
break;
case GLFW_KEY_UP:
light_params.cloudiness += 0.05;
if (light_params.cloudiness > 1)
light_params.cloudiness = 1;
break;
case GLFW_KEY_DOWN:
light_params.cloudiness -= 0.05;
if (light_params.cloudiness < 0)
light_params.cloudiness = 0;
break;
}
}
@@ -57,16 +70,12 @@ int main(int argc, char *argv[])
gpu_model_load(&gpu, &model, gpu_models + 0);
gpu_model_load(&gpu, &model, gpu_models + 1);
const vec3_t cam = { 0, 4, -8 };
const vec3_t cam = { 2, 2, -8 };
const vec3_t tgt = { 0, 0, 0 };
const vec3_t up = { 0, 1, 0 };
const vec3_t light = { 2, 2, -1 };
const mat4_t view = mat4_lookat(cam, tgt, up);
const mat4_t proj = mat4_perspective((float)W / H, PI / 8, 0.1, 100.0);
const mat4_t proj = mat4_perspective((float)W / H, PI / 12, 0.1, 100.0);
const mat4_t vp = mat4_mul(proj, view);
const mat4_t light_view = mat4_lookat(light, tgt, up);
const mat4_t light_proj = mat4_orthographic(5, 5, 10);
const mat4_t light_vp = mat4_mul(light_proj, light_view);
mat4_t transforms[NELEMS(gpu_models)] = {
mat4_mul(
MAT4_TRANSLATE(1, 0, 0),
@@ -92,6 +101,9 @@ int main(int argc, char *argv[])
animation_state_t anim = animation_init(&model);
animation_start(&anim, &model, 0);
light_params.sun_dir = vec3_norm((vec3_t) { 2, 4, -1 });
light_params.cloudiness = 0.0;
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
@@ -105,7 +117,7 @@ int main(int argc, char *argv[])
anim.bone_matrices,
};
gpu_render(
&gpu, vp, light_vp, NELEMS(gpu_models), gpu_models,
&gpu, vp, light_params, NELEMS(gpu_models), gpu_models,
bone_matrices, transforms);
}