X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=shaderlib%2Fshadow.glsl;h=d533b4ec389913cef931cd7cce4021f5c253ef3d;hb=HEAD;hp=6f4acd2786a6319c242d9ff8300a00ecc535d0a7;hpb=6dcf74922f46b086ad394c19fd6ce083a635b290;p=libs%2Fgl.git diff --git a/shaderlib/shadow.glsl b/shaderlib/shadow.glsl index 6f4acd27..d533b4ec 100644 --- a/shaderlib/shadow.glsl +++ b/shaderlib/shadow.glsl @@ -1,20 +1,59 @@ -import msp_interface; +import lighting; -layout(constant_id=auto) const bool use_shadow_map = false; +struct ShadowParameters +{ + int type; + float darkness; + int matrix_index; + vec4 region; + vec2 bias; +}; -#pragma MSP stage(vertex) -void shadow_transform(vec4 eye_vertex) +layout(set=0) uniform ShadowMap { - out vec3 shadow_coord = (shd_eye_matrix*eye_vertex).xyz; -} + ShadowParameters shadows[max_lights]; + mat4 shd_world_matrix[max_lights*4]; +}; + +layout(set=0) uniform sampler2DShadow shadow_map; + +layout(constant_id=auto) const bool use_shadow_map = false; #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) { - float shadow_sample = texture(shadow_map, shadow_coord); - return mix(1.0, shadow_sample, shadow_darkness); + 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.x; + } + else if(type==2) + { + int base = shadows[index].matrix_index; + vec4 clip_coord = shd_world_matrix[base]*world_pos; + for(int i=1; i<4; ++i) + { + vec4 c = shd_world_matrix[base+i]*world_pos; + if(c.w>clip_coord.w) + clip_coord = c; + } + shadow_coord = clip_coord.xyz/clip_coord.w; + vec2 bias = shadows[index].bias; + shadow_coord.z = (shadow_coord.z+bias.x)/bias.y-bias.x; + } + else + return 1.0; + + if(shadow_coord.x<0 || shadow_coord.x>1 || shadow_coord.y<0 || shadow_coord.y>1) + return 1.0; + + vec4 region = shadows[index].region; + float shadow_sample = texture(shadow_map, vec3(shadow_coord.xy*region.zw+region.xy, min(shadow_coord.z, 1.0))); + return mix(1.0, shadow_sample, shadows[index].darkness); } else return 1.0;