Hello to everybody,

I try to implement a soft shadow with PCF so, I’m writing a glsl test to visualize the z value:

"void main(void)

"

"{

"

" gl_Position = ftransform();

"

" gl_TexCoord[0] = gl_MultiTexCoord0;

"

" vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;

"

" vectLighttoVertex = lightPos - gl_Position;

"

" projCoord .s = dot(ecPosition, gl_EyePlaneS[3]);

"//multiply light view matrix

" projCoord .t = dot(ecPosition, gl_EyePlaneT[3]);

"

" projCoord .p = dot(ecPosition, gl_EyePlaneR[3]);

"

" projCoord .q = dot(ecPosition, gl_EyePlaneQ[3]);

"

"}

";

// fragment shader program for bump mapping in surface local coordinates

static std::string _fp_program =

"uniform sampler2D sampler2d;

"

"uniform sampler2D shadowMap;

"

"const float c = 1.0f/128.0f;

"

"varying vec4 projCoord;

"

"varying vec4 vertexNormal;

"

"varying vec4 vectLighttoVertex;

"

"void main (void)

"

"{

"

" vec3 finalColor;

"

" float actShadow;

"

" vec3 color = vec3(texture2D(sampler2d, vec2 (gl_TexCoord[0])));

"

" vec3 projectiveBiased = (projCoord.xyz / projCoord.w);

"

//get the 4 z value from the shadow map

" vec4 shadowValue0 = texture2D(shadowMap, projectiveBiased.xy );

"

" vec4 shadowValue1 = texture2D(shadowMap, projectiveBiased.xy + vec2( c,0 ) );

"

" vec4 shadowValue2 = texture2D(shadowMap, projectiveBiased.xy + vec2( c,c ) );

"

" vec4 shadowValue3 = texture2D(shadowMap, projectiveBiased.xy + vec2( 0,c ) );

"

//perform the shadow map test if pass

" actShadow = 0.0f;

"

" if ( shadowValue0.z > projectiveBiased.z )

"

" actShadow += 1.0f;

"

" if ( shadowValue1.z > projectiveBiased.z )

"

" actShadow += 1.0f;

"

" if ( shadowValue2.z > projectiveBiased.z )

"

" actShadow += 1.0f;

"

" if ( shadowValue3.z > projectiveBiased.z )

"

" actShadow += 1.0f;

"

//perform the PCF

" actShadow /= 1.0f;

"

//assign the final color

" gl_FragColor = vec4( projectiveBiased.z-shadowValue0.z,projectiveBiased.z-shadowValue0.z,projectiveBiased.z-shadowValue0.z,1);

"

"}

";

this is my shader,it show on sceen the z difference between the actual z value and the z value stored in shadow map. It works fine: it show me white color( or a grey) where there is shadow and black where not.

Now I want to see the difference between shadowvalue1/2/3 ( the value taken from 3 near texel) and actual z value

so i switch this line of code:

" gl_FragColor = vec4( projectiveBiased.z-shadowValue0.z,projectiveBiased.z-shadowValue0.z,projectiveBiased.z-shadowValue0.z,1);

"

"}

";

with:

" gl_FragColor = vec4( projectiveBiased.z-shadowValue1.z,projectiveBiased.z-shadowValue1.z,projectiveBiased.z-shadowValue1.z,1);

"

"}

";

but now there are many problems:

It show me everything black!!!That is shadowValue1.z is always greater than actual z (projectiveBiased.z). It seems that I don’t take the correct z value from the shadow map…but…the dimension is 128x128 and this line of code seems correct:

" vec4 shadowValue1 = texture2D(shadowMap, projectiveBiased.xy + vec2( c,0 ) );

"

where c= 1.0f/128.0f

where I make mistake???

Thanks Davide