1uniform mat4 mvpl;
2uniform sampler2D shadowMap;
3uniform float texSize;
4varying vec4 shadowCoord;
5uniform float shadowIntensity; // 1.0 black, 0, transparent
6
7vec4 shadowCoordPostW;
8float chebyshevUpperBound( float distance) {
9  // We retrive the two moments previously stored (depth and depth*depth)
10  vec2 moments = texture2D(shadowMap,shadowCoordPostW.xy).rb;
11  // Surface is fully lit. as the current fragment is before the light occluder
12  if (distance <= moments.x)
13    return 1.0 ;
14
15  float variance = moments.y - (moments.x*moments.x);
16  //variance = max(variance,0.00002);
17  variance = max(variance,0.000195);
18  float d = distance - moments.x;
19  float p_max = variance / (variance + d*d);
20  return p_max;
21}
22
23void main() {
24  shadowCoordPostW = shadowCoord / shadowCoord.w;
25  shadowCoordPostW = shadowCoordPostW * 0.5 + 0.5;
26  float shadow = chebyshevUpperBound(shadowCoordPostW.z);
27
28  vec4 kd = gl_LightSource[0].diffuse;
29  if (shadow > 0.4)
30    discard;
31
32  // gl_FragColor = vec4(vec3(0.0), 0.5 - shadow);
33  gl_FragColor = vec4(vec3(0.0), (shadowIntensity-shadow));
34
35}
36