Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 330 core
- uniform vec3 light;
- uniform sampler2D shadows;
- uniform mat4 shadowTransform;
- in vec3 g_vertex;
- flat in vec3 g_normal;
- layout(location = 0) out vec3 out_color;
- const float minVariance = 2.0;
- const int kernelRadius = 3;
- float chebyshevUpperBound(vec2 moments, float t)
- {
- float p = float(t <= moments.x);
- float variance = moments.y - moments.x * moments.x;
- variance = max(variance, minVariance);
- float d = t - moments.x;
- float p_max = variance / (variance + d * d);
- return max(p, p_max);
- }
- vec4 samplerKernel(sampler2D tex, vec2 uv, int radius)
- {
- int count = 0;
- vec4 sum = vec4(0.0);
- for (float x = -radius; x <= radius; x++)
- {
- for (float y = -radius; y <= radius; y++)
- {
- vec2 newPos = uv + vec2(x, y) / textureSize(tex, 0);
- sum += texture(tex, newPos);
- count++;
- }
- }
- return (count > 0) ? sum / count : vec4(0.0);
- }
- void main()
- {
- vec3 lightDirection = light - g_vertex;
- float lambert = clamp(dot(normalize(g_normal), normalize(lightDirection)), 0.0, 1.0);
- /**
- * BEGIN TODO 5.3:
- * - Compute distance to light source (see light)
- * - Compute shadow map uv coordinates from g_vertex (see shadowTransform)
- * - Comparison of distance to stored value in variance shadow map (see shadows)
- * - Apply shadow value to current lambert-term shading
- * - Apply advanced filtering (either here or in e5task3.cpp)
- */
- vec4 shadow_coordinate = shadowTransform * vec4(g_vertex, 1.0);
- vec2 shadow_uv = shadow_coordinate.xy / shadow_coordinate.w;
- shadow_uv = shadow_uv * 0.5 + vec2(0.5, 0.5);
- vec2 moments = samplerKernel(shadows, shadow_uv, kernelRadius).rg;
- float dist = distance(light, g_vertex);
- out_color = vec3(chebyshevUpperBound(moments, dist)) * lambert;
- /**
- * END TODO 5.3
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement