]> git.tdb.fi Git - libs/gl.git/commitdiff
Some refactoring of light calculations in shaders
authorMikko Rasa <tdb@tdb.fi>
Sat, 9 Oct 2021 17:47:39 +0000 (20:47 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 9 Oct 2021 17:52:55 +0000 (20:52 +0300)
shaderlib/common.glsl
shaderlib/cooktorrance.glsl
shaderlib/phong.glsl

index dde09c12cff45bca1a4a60fe95d6a37305404ef8..4d6112d9f2b37a58127f6a5330837b5b55973217 100644 (file)
@@ -71,6 +71,12 @@ void main()
 }
 
 #pragma MSP stage(fragment)
+struct IncomingLight
+{
+       vec3 direction;
+       vec3 color;
+};
+
 virtual vec3 get_fragment_normal()
 {
        if(use_normal_map)
@@ -79,10 +85,12 @@ virtual vec3 get_fragment_normal()
                return normalize(world_normal);
 }
 
-virtual vec3 get_light_direction(int index)
+virtual IncomingLight get_incoming_light(int index, vec3 world_pos)
 {
        vec4 light_pos = light_sources[index].position;
-       return normalize(light_pos.xyz-world_vertex.xyz*light_pos.w);
+       vec3 rel_pos = light_pos.xyz-world_pos*light_pos.w;
+       float d = length(rel_pos);
+       return IncomingLight(rel_pos/d, light_sources[index].color);
 }
 
 virtual vec3 get_environment_sample(vec3 direction, float roughness)
index f441a6d392088fe05f8152dc3430d2de1b701c3a..79f88f6475db747479996e81c73079de8d1106bd 100644 (file)
@@ -118,7 +118,7 @@ vec3 fresnel_schlick(vec3 halfway, vec3 look, vec3 base_color, float metalness)
 }
 
 /* Computes the full contribution of a single light */
-vec3 cooktorrance_one_light_direct(vec3 normal, vec3 look, vec3 light, vec3 light_color, vec3 base_color, float metalness, float roughness)
+vec3 cooktorrance_one_light_direct(vec3 normal, vec3 look, vec3 light, vec3 base_color, float metalness, float roughness)
 {
        vec3 halfway = normalize(light-look);
        float ndist = normal_distribution_ggxtr(normal, halfway, roughness);
@@ -128,7 +128,7 @@ vec3 cooktorrance_one_light_direct(vec3 normal, vec3 look, vec3 light, vec3 ligh
        vec3 k_diff = (1.0-k_spec)*(1.0-metalness);
 
        float spec_denom = max(4.0*max(dot(normal, -look), 0.0)*max(dot(normal, light), 0.0), 0.001);
-       return max(dot(normal, light), 0.0)*light_color*(k_diff*lambert_diffuse(base_color)+k_spec*ndist*geom/spec_denom);
+       return max(dot(normal, light), 0.0)*(k_diff*lambert_diffuse(base_color)+k_spec*ndist*geom/spec_denom);
 }
 
 vec3 cooktorrance_environment(vec3 normal, vec3 look, vec3 base_color, float metalness, float roughness)
@@ -155,9 +155,9 @@ vec3 cooktorrance_lighting(vec3 normal, vec3 look, vec3 base_color, float metaln
        for(int i=0; i<max_lights; ++i)
                if(light_sources[i].type!=0)
                {
-                       vec3 light = get_light_direction(i);
+                       IncomingLight incoming = get_incoming_light(i, world_vertex.xyz);
                        float shadow = get_shadow_factor(i);
-                       color += cooktorrance_one_light_direct(normal, look, light, light_sources[i].color, base_color, metalness, roughness)*shadow;
+                       color += cooktorrance_one_light_direct(normal, look, incoming.direction, base_color, metalness, roughness)*incoming.color*shadow;
                }
 
        color += cooktorrance_environment(normal, look, base_color, metalness, roughness);
index 004232acd503e4c76c607ca6206f2f4e228cdef1..ed091ce9a9374d0646ca900b9ca6210fd4be9dfb 100644 (file)
@@ -79,15 +79,15 @@ vec3 phong_ambient(vec3 surface_diffuse)
        return ambient_color.rgb*surface_diffuse;
 }
 
-vec3 phong_one_light(vec3 light, vec3 normal, vec3 look, vec3 light_color, vec3 surface_diffuse, vec3 surface_specular, float shininess)
+vec3 phong_one_light(vec3 light, vec3 normal, vec3 look, vec3 surface_diffuse, vec3 surface_specular, float shininess)
 {
        float diffuse_intensity = max(dot(light, normal), 0.0);
-       vec3 color = light_color*surface_diffuse*diffuse_intensity;
+       vec3 color = surface_diffuse*diffuse_intensity;
        if(use_specular)
        {
                vec3 reflected = reflect(look, normal);
                float specular_intensity = pow(max(dot(reflected, light), 0.0), shininess);
-               color += light_color*surface_specular*specular_intensity;
+               color += surface_specular*specular_intensity;
        }
        return color;
 }
@@ -98,9 +98,9 @@ vec3 phong_lighting(vec3 normal, vec3 look, vec3 surface_diffuse, vec3 surface_s
        for(int i=0; i<max_lights; ++i)
                if(light_sources[i].type!=0)
                {
-                       vec3 light = get_light_direction(i);
+                       IncomingLight incoming = get_incoming_light(i, world_vertex.xyz);
                        float shadow = get_shadow_factor(i);
-                       color += phong_one_light(light, normal, look, light_sources[i].color, surface_diffuse, surface_specular, shininess)*shadow;
+                       color += phong_one_light(incoming.direction, normal, look, surface_diffuse, surface_specular, shininess)*incoming.color*shadow;
                }
 
        if(use_emission)