Use matrices to transform light and normals

This commit is contained in:
Abdelrahman Said 2024-08-24 00:25:41 +01:00
parent 996597684b
commit 6f5c094f49

View File

@ -25,7 +25,7 @@ internal V3f g_ambient_light = {0.3f, 0.75f, 0.15f};
internal DirectionalLight g_directional_light = { internal DirectionalLight g_directional_light = {
.diffuse_intensity = {0.9f, 0.8f, 0.6f}, .diffuse_intensity = {0.9f, 0.8f, 0.6f},
.specular_intensity = {0.5f, 0.8f, 2.0f}, .specular_intensity = {0.5f, 0.8f, 2.0f},
.direction = {0.0f, 0.0f, 1.0f}, .direction = {0.0f, 0.0f, 1.4f},
}; };
internal V3f g_eye = {0.2f, 0.1f, 0.75f}; internal V3f g_eye = {0.2f, 0.1f, 0.75f};
internal V3f g_target = {0}; internal V3f g_target = {0};
@ -88,18 +88,33 @@ internal FragmentResult phong_shader_fragment(void *shader,
const FragmentData *data, const FragmentData *data,
const Colour *colour, const Colour *colour,
const Model *model) { const Model *model) {
Shader *shader_ptr = (Shader *)shader;
V4f hnorm;
V3f norm; V3f norm;
if (model->normal) { if (model->normal) {
u64 nm_x = data->tex_coords.u * model->normal->width; u64 nm_x = data->tex_coords.u * model->normal->width;
u64 nm_y = (1.0f - data->tex_coords.v) * model->normal->height; u64 nm_y = (1.0f - data->tex_coords.v) * model->normal->height;
Colour pixel = get_pixel(Colour, model->normal, nm_x, nm_y); Colour pixel = get_pixel(Colour, model->normal, nm_x, nm_y);
norm = (V3f){.x = pixel.r, .y = pixel.g, .z = pixel.b}; hnorm = (V4f){
normalise_v3(norm); .x = pixel.r,
.y = pixel.g,
.z = pixel.b,
.w = 1.0f,
};
} else { } else {
norm = data->normal; hnorm = V3_to_V4(data->normal);
} }
M4x4f matrix = mat4x4_mul(shader_ptr->projection, shader_ptr->model_view);
M4x4f transposed = mat4x4_transpose(matrix);
M4x4f inv_transpose = mat4x4_inv(transposed);
hnorm = mat4x4_mul_vec4(inv_transpose, hnorm);
norm = project_vec4(hnorm);
normalise_v3(norm);
Colour output; Colour output;
if (model->texture) { if (model->texture) {
u64 tx_x = data->tex_coords.u * model->texture->width; u64 tx_x = data->tex_coords.u * model->texture->width;
@ -115,8 +130,12 @@ internal FragmentResult phong_shader_fragment(void *shader,
// Ambient term // Ambient term
V3f intensity = num_mul_v3(g_ambient_light, model->material.ambient); V3f intensity = num_mul_v3(g_ambient_light, model->material.ambient);
V4f hdir = V3_to_V4(g_directional_light.direction);
hdir = mat4x4_mul_vec4(matrix, hdir);
V3f light_dir = project_vec4(hdir);
// Diffuse term // Diffuse term
f32 l_dot_n = dot_v3(norm, g_directional_light.direction); f32 l_dot_n = dot_v3(norm, light_dir);
if (l_dot_n <= 0.0f) { if (l_dot_n <= 0.0f) {
goto RETURN_OUTPUT_COLOUR; goto RETURN_OUTPUT_COLOUR;
} }
@ -127,7 +146,7 @@ internal FragmentResult phong_shader_fragment(void *shader,
// Specular term // Specular term
V3f _2_l_dot_n_norm = num_mul_v3(norm, 2.0f * l_dot_n); V3f _2_l_dot_n_norm = num_mul_v3(norm, 2.0f * l_dot_n);
V3f reflected = sub_v3(_2_l_dot_n_norm, g_directional_light.direction); V3f reflected = sub_v3(_2_l_dot_n_norm, light_dir);
f32 r_dot_v = dot_v3(reflected, data->position); f32 r_dot_v = dot_v3(reflected, data->position);
if (r_dot_v <= 0.0f) { if (r_dot_v <= 0.0f) {
goto RETURN_OUTPUT_COLOUR; goto RETURN_OUTPUT_COLOUR;