Advertisement
Guest User

Untitled

a guest
Jul 13th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 330 core
  2.  
  3.  
  4. uniform vec3 light;
  5. uniform sampler2D shadows;
  6. uniform mat4 shadowTransform;
  7.  
  8.  
  9. in vec3 g_vertex;
  10. flat in vec3 g_normal;
  11.  
  12.  
  13. layout(location = 0) out vec3 out_color;
  14.  
  15. const float minVariance = 2.0;
  16. const int kernelRadius = 3;
  17.  
  18. float chebyshevUpperBound(vec2 moments, float t)
  19. {
  20.     float p = float(t <= moments.x);
  21.     float variance = moments.y - moments.x * moments.x;
  22.     variance = max(variance, minVariance);
  23.     float d = t - moments.x;
  24.     float p_max = variance / (variance + d * d);
  25.     return max(p, p_max);
  26. }
  27.  
  28. vec4 samplerKernel(sampler2D tex, vec2 uv, int radius)
  29. {
  30.     int count = 0;
  31.     vec4 sum = vec4(0.0);
  32.     for (float x = -radius; x <= radius; x++)
  33.     {
  34.         for (float y = -radius; y <= radius; y++)
  35.         {
  36.             vec2 newPos = uv + vec2(x, y) / textureSize(tex, 0);
  37.             sum += texture(tex, newPos);
  38.             count++;
  39.         }
  40.     }
  41.     return (count > 0) ? sum / count : vec4(0.0);
  42. }
  43.  
  44.  
  45. void main()
  46. {
  47.     vec3 lightDirection = light - g_vertex;
  48.     float lambert = clamp(dot(normalize(g_normal), normalize(lightDirection)), 0.0, 1.0);
  49.  
  50.     /**
  51.      * BEGIN TODO 5.3:
  52.      * - Compute distance to light source (see light)
  53.      * - Compute shadow map uv coordinates from g_vertex (see shadowTransform)
  54.      * - Comparison of distance to stored value in variance shadow map (see shadows)
  55.      * - Apply shadow value to current lambert-term shading
  56.      * - Apply advanced filtering (either here or in e5task3.cpp)
  57.      */
  58.  
  59.     vec4 shadow_coordinate = shadowTransform * vec4(g_vertex, 1.0);
  60.     vec2 shadow_uv = shadow_coordinate.xy / shadow_coordinate.w;
  61.     shadow_uv = shadow_uv * 0.5 + vec2(0.5, 0.5);
  62.  
  63.     vec2 moments = samplerKernel(shadows, shadow_uv, kernelRadius).rg;
  64.  
  65.     float dist = distance(light, g_vertex);
  66.  
  67.     out_color = vec3(chebyshevUpperBound(moments, dist)) * lambert;
  68.  
  69.     /**
  70.      * END TODO 5.3
  71.      */
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement