From eb270458de24c7346adc1ab0a4fe1c409a2008ae Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 23 May 2024 19:37:47 +0200 Subject: refactor(engine): seperate light properties in shader --- engine/light.glsl | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'engine/light.glsl') diff --git a/engine/light.glsl b/engine/light.glsl index 6bc730a..9f51237 100644 --- a/engine/light.glsl +++ b/engine/light.glsl @@ -3,7 +3,8 @@ #ifndef LIGHT_GLSL #define LIGHT_GLSL -struct Material { +struct Material +{ vec3 ambient; vec3 diffuse; vec3 specular; @@ -13,15 +14,26 @@ struct Material { float shininess; }; -struct PointLight { - vec3 position; +struct LightPhong +{ vec3 diffuse; vec3 specular; +}; + +struct AttenuationProperties +{ float constant; float linear; float quadratic; }; +struct PointLight +{ + LightPhong phong; + vec3 position; + AttenuationProperties attenuation_props; +}; + vec3 calc_ambient_light(in Material material, in vec2 texture_coords) { return vec3(texture(material.ambient_map, texture_coords)) * material.ambient; @@ -29,7 +41,7 @@ vec3 calc_ambient_light(in Material material, in vec2 texture_coords) vec3 calc_diffuse_light( in Material material, - in PointLight light, + in LightPhong light_phong, in vec3 light_dir, in vec3 norm, in vec2 texture_coords @@ -37,14 +49,14 @@ vec3 calc_diffuse_light( { float diff = max(dot(norm, light_dir), 0.0); - return light.diffuse * ( + return light_phong.diffuse * ( diff * (vec3(texture(material.diffuse_map, texture_coords)) * material.diffuse) ); } vec3 calc_specular_light( in Material material, - in PointLight light, + in LightPhong light_phong, in vec3 light_dir, in vec3 norm, in vec3 view_pos, @@ -59,7 +71,7 @@ vec3 calc_specular_light( float spec = pow(max(dot(view_direction, reflect_direction), 0.0), material.shininess); - return light.specular * ( + return light_phong.specular * ( spec * (vec3(texture(material.specular_map, texture_coords)) * material.specular) ); } @@ -68,8 +80,9 @@ float calc_attenuation(in PointLight point_light, in vec3 position) { float light_distance = length(point_light.position - position); - return 1.0 / (point_light.constant + point_light.linear - * light_distance + point_light.quadratic + return 1.0 / (point_light.attenuation_props.constant + + point_light.attenuation_props.linear + * light_distance + point_light.attenuation_props.quadratic * pow(light_distance, 2)); } @@ -87,7 +100,7 @@ vec3 calc_point_light( vec3 diffuse_light = calc_diffuse_light( material, - light, + light.phong, light_direction, norm, texture_coords @@ -95,7 +108,7 @@ vec3 calc_point_light( vec3 specular_light = calc_specular_light( material, - light, + light.phong, light_direction, norm, view_pos, -- cgit v1.2.3-18-g5258