]> git.tdb.fi Git - libs/gl.git/blobdiff - shaderlib/shadow.glsl
Apply shadow depth bias in a different way
[libs/gl.git] / shaderlib / shadow.glsl
index f0338b95f8a10c1f0a9ced28210d59fb345e5d4d..438af68d45181a86b6ec16f3cef065ba91ab88c4 100644 (file)
@@ -1,30 +1,45 @@
 import msp_interface;
 
+struct ShadowParameters
+{
+       int type;
+       float darkness;
+       int matrix_index;
+       vec4 region;
+       float bias;
+};
+
 uniform ShadowMap
 {
-       float shadow_darkness;
-       mat4 shd_eye_matrix;
+       ShadowParameters shadows[max_lights];
+       mat4 shd_world_matrix[max_lights];
 };
 
 uniform sampler2DShadow shadow_map;
 
 layout(constant_id=auto) const bool use_shadow_map = false;
 
-#pragma MSP stage(vertex)
-void shadow_transform(vec4 eye_vertex)
-{
-       out vec3 shadow_coord = (shd_eye_matrix*eye_vertex).xyz;
-}
-
 #pragma MSP stage(fragment)
-virtual float get_shadow_factor(int index)
+virtual float get_shadow_factor(int index, vec4 world_pos)
 {
        if(use_shadow_map)
        {
+               int type = shadows[index].type;
+               vec3 shadow_coord;
+               if(type==1)
+               {
+                       shadow_coord = (shd_world_matrix[shadows[index].matrix_index]*world_pos).xyz;
+                       shadow_coord.z -= shadows[index].bias;
+               }
+               else
+                       return 1.0;
+
                if(shadow_coord.x<0 || shadow_coord.x>1 || shadow_coord.y<0 || shadow_coord.y>1)
                        return 1.0;
-               float shadow_sample = texture(shadow_map, shadow_coord);
-               return mix(1.0, shadow_sample, shadow_darkness);
+
+               vec4 region = shadows[index].region;
+               float shadow_sample = texture(shadow_map, vec3(shadow_coord.xy*region.zw+region.xy, shadow_coord.z));
+               return mix(1.0, shadow_sample, shadows[index].darkness);
        }
        else
                return 1.0;