diff options
author | HampusM <hampus@hampusmat.com> | 2024-05-23 19:37:47 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-05-23 20:09:54 +0200 |
commit | eb270458de24c7346adc1ab0a4fe1c409a2008ae (patch) | |
tree | 166401361c9b3eb4f72e32cdb8fca8190f54daa1 /engine/light.glsl | |
parent | 42f4da1c9670e4e9829a822a50dc708794744ca1 (diff) |
refactor(engine): seperate light properties in shader
Diffstat (limited to 'engine/light.glsl')
-rw-r--r-- | engine/light.glsl | 35 |
1 files changed, 24 insertions, 11 deletions
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, |