From 8ccc424b0634478544fcf668f816f68cc42c5cf3 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sat, 24 Aug 2024 23:16:11 +0100 Subject: [PATCH] Calculate light direction from position --- src/shader/shaders.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/shader/shaders.c b/src/shader/shaders.c index 1d1d9d2..2a2bfb9 100644 --- a/src/shader/shaders.c +++ b/src/shader/shaders.c @@ -130,9 +130,11 @@ internal FragmentResult phong_shader_fragment(void *shader, // Ambient term V3f intensity = num_mul_v3(g_ambient_light, model->material.ambient); - V4f hdir = V3_to_V4(g_directional_light.direction); + V4f hdir = V3_to_V4(g_directional_light.position); hdir = mat4x4_mul_vec4(matrix, hdir); - V3f light_dir = project_vec4(hdir); + V3f light_pos = project_vec4(hdir); + normalise_v3(light_pos); + V3f light_dir = sub_v3(light_pos, data->position); // Diffuse term f32 l_dot_n = dot_v3(norm, light_dir); @@ -147,7 +149,9 @@ internal FragmentResult phong_shader_fragment(void *shader, // Specular term V3f _2_l_dot_n_norm = num_mul_v3(norm, 2.0f * l_dot_n); V3f reflected = sub_v3(_2_l_dot_n_norm, light_dir); + normalise_v3(reflected); V3f v = sub_v3(g_eye, data->position); + normalise_v3(v); f32 r_dot_v = dot_v3(reflected, v); if (r_dot_v <= 0.0f) { goto RETURN_OUTPUT_COLOUR; @@ -193,7 +197,7 @@ internal M4x4f get_projection_matrix(ProjectionType projection_type) { } internal f32 get_intensity(const V3f *normal) { - V3f light = g_directional_light.direction; + V3f light = g_directional_light.position; normalise_v3(light); f32 intensity = dot_v3((*normal), light);