]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/shadow.glsl
Refactor ShadowMap to allow different types of lights
[libs/gl.git] / shaderlib / shadow.glsl
1 import msp_interface;
2
3 struct ShadowParameters
4 {
5         int type;
6         float darkness;
7         int matrix_index;
8         vec4 region;
9 };
10
11 uniform ShadowMap
12 {
13         ShadowParameters shadows[max_lights];
14         mat4 shd_world_matrix[max_lights];
15 };
16
17 uniform sampler2DShadow shadow_map;
18
19 layout(constant_id=auto) const bool use_shadow_map = false;
20
21 #pragma MSP stage(fragment)
22 virtual float get_shadow_factor(int index, vec4 world_pos)
23 {
24         if(use_shadow_map)
25         {
26                 int type = shadows[index].type;
27                 vec3 shadow_coord;
28                 if(type==1)
29                         shadow_coord = (shd_world_matrix[shadows[index].matrix_index]*world_pos).xyz;
30                 else
31                         return 1.0;
32
33                 if(shadow_coord.x<0 || shadow_coord.x>1 || shadow_coord.y<0 || shadow_coord.y>1)
34                         return 1.0;
35
36                 vec4 region = shadows[index].region;
37                 float shadow_sample = texture(shadow_map, shadow_coord*vec3(region.zw, 1.0)+vec3(region.xy, 0.0));
38                 return mix(1.0, shadow_sample, shadows[index].darkness);
39         }
40         else
41                 return 1.0;
42 }