~ubuntu-branches/ubuntu/utopic/castle-game-engine/utopic

« back to all changes in this revision

Viewing changes to src/x3d/opengl/glsl/variance_shadow_map_common.fs

  • Committer: Package Import Robot
  • Author(s): Abou Al Montacir
  • Date: 2013-04-27 18:06:40 UTC
  • Revision ID: package-import@ubuntu.com-20130427180640-eink4nmwzuivez1c
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Extract shadow (how light is the point) from variance shadow map.
 
2
   This closely follows VSM presentation, slide 16. */
 
3
 
 
4
float shadow(sampler2D shadowMap, const vec4 shadowMapCoord, const in float size)
 
5
{
 
6
  /* Avoid back-projecting shadows. */
 
7
  if (shadowMapCoord.z < 0.0) return 0.0;
 
8
 
 
9
  /* When coord2 is outside (0, 0) - (1, 1) square,
 
10
     it's always in the shadow. Otherwise shadows would be stretched
 
11
     over whole scene, due to clamping. */
 
12
  vec2 coord2 = shadowMapCoord.st / shadowMapCoord.q;
 
13
  if (coord2.s < 0.0 || coord2.s > 1.0 ||
 
14
      coord2.t < 0.0 || coord2.t > 1.0)
 
15
    return 0.0;
 
16
 
 
17
  vec4 moments = texture2D(shadowMap, coord2);
 
18
  float distance_to_light = shadowMapCoord.z / shadowMapCoord.q;
 
19
 
 
20
  if (distance_to_light <= moments[0])
 
21
    return 1.0; else
 
22
  {
 
23
    float E_x2 = moments[1];
 
24
    float Ex_2 = moments[0] * moments[0];
 
25
    float variance = E_x2 - Ex_2;
 
26
    float m_d = moments[0] - distance_to_light;
 
27
    return variance / (variance + m_d * m_d);
 
28
  }
 
29
}
 
30
 
 
31
float shadow_depth(sampler2D shadowMap, const vec4 shadowMapCoord)
 
32
{
 
33
  /* Avoid back-projecting shadows. */
 
34
  if (shadowMapCoord.z < 0.0) return 0.0;
 
35
 
 
36
  vec2 coord2 = shadowMapCoord.st / shadowMapCoord.q;
 
37
 
 
38
  /* When coord2 is outside (0, 0) - (1, 1) square, set d = 0.
 
39
     Otherwise texture would be visible stretched due to clamping. */
 
40
  if (coord2.s < 0.0 || coord2.s > 1.0 ||
 
41
      coord2.t < 0.0 || coord2.t > 1.0)
 
42
    return 0.0; else
 
43
    return texture2D(shadowMap, coord2).x;
 
44
}