added shader programs for variance shadow mapping with blur

This commit is contained in:
Paolo Cignoni cignoni 2009-07-20 13:59:17 +00:00
parent c46fd9ef92
commit 2ea650959c
6 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,17 @@
uniform sampler2D scene;
uniform vec2 scale;
void main() {
vec4 color = vec4(vec3(0.0), 1.0);
color += texture2D( scene, gl_TexCoord[0].st + vec2( -3.0 * scale.x, -3.0 * scale.y ) ) * 0.015625;
color += texture2D( scene, gl_TexCoord[0].st + vec2( -2.0 * scale.x, -2.0 * scale.y ) )*0.09375;
color += texture2D( scene, gl_TexCoord[0].st + vec2( -1.0 * scale.x, -1.0 * scale.y) )*0.234375;
color += texture2D( scene, gl_TexCoord[0].st + vec2( 0.0 , 0.0) )*0.3125;
color += texture2D( scene, gl_TexCoord[0].st + vec2( 1.0 * scale.x, 1.0 * scale.y ) )*0.234375;
color += texture2D( scene, gl_TexCoord[0].st + vec2( 2.0 * scale.x, 2.0 * scale.y ) )*0.09375;
color += texture2D( scene, gl_TexCoord[0].st + vec2( 3.0 * scale.x, -3.0 * scale.y ) ) * 0.015625;
if(color.x == 1.0 && color.y == 1.0 && color.z == 1.0)
discard;
gl_FragColor = color;
}

View File

@ -0,0 +1,4 @@
void main(){
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
}

View File

@ -0,0 +1,16 @@
varying vec4 point;
void main(void)
{
float depth = point.z;// / point.w;
depth = depth * 0.5 + 0.5;
float moment1 = depth;
float moment2 = depth * depth;
float dx = dFdx(depth);
float dy = dFdy(depth);
moment2 += 0.25 * (dx * dx + dy * dy);
gl_FragColor = vec4(moment1, moment2 , 0.0, 1.0);
}

View File

@ -0,0 +1,6 @@
varying vec4 point;
void main(){
gl_Position = ftransform();
point = gl_Position;
}

View File

@ -0,0 +1,40 @@
uniform mat4 mvpl;
uniform sampler2D shadowMap;
uniform float texSize;
varying vec4 shadowCoord;
vec4 shadowCoordPostW;
float chebyshevUpperBound( float distance) {
// We retrive the two moments previously stored (depth and depth*depth)
vec2 moments = texture2D(shadowMap,shadowCoordPostW.xy).rb;
// Surface is fully lit. as the current fragment is before the light occluder
if (distance <= moments.x)
return 1.0 ;
float variance = moments.y - (moments.x*moments.x);
//variance = max(variance,0.00002);
variance = max(variance,0.000195);
float d = distance - moments.x;
float p_max = variance / (variance + d*d);
return p_max;
}
void main() {
shadowCoordPostW = shadowCoord / shadowCoord.w;
shadowCoordPostW = shadowCoordPostW * 0.5 + 0.5;
float shadow = chebyshevUpperBound(shadowCoordPostW.z);
//vec4 ka = gl_LightSource[0].ambient;
vec4 kd = gl_LightSource[0].diffuse;
//vec4 ks = gl_LightSource[0].specular;
//vec4 color = ka + kd + ks;
//vec4 color = vec4(vec3(0.5), 1.0);
//gl_FragColor = vec4(shadow ) * color;
if (shadow > 0.4){
//discard;
gl_FragColor = vec4(vec3(kd), 0.0);
}
else
gl_FragColor = vec4(vec3(shadow), 1.0);
}

View File

@ -0,0 +1,6 @@
uniform mat4 mvpl;
varying vec4 shadowCoord;
void main() {
shadowCoord= mvpl * gl_Vertex;
gl_Position = ftransform();
}