I'm trying to implement a shadow mapping shader (based on XEffects) but I don't get it running (XEffects works fine of course, but I also want something less hardware hungry ).
I want to only run 2 render passes (one for shadow map rendering, one for rendering the scene with shadows). I think I have broken the problem down to the 2nd pass pixel shader:
Code: Select all
uniform sampler2D textureSampler;
uniform sampler2D ShadowMapSampler;
uniform vec4 LightColour;
varying float lightVal;
float testShadow(vec2 smTexCoord, vec2 offset, float realDistance)
{
vec4 texDepth = texture2D(ShadowMapSampler, vec2( smTexCoord + offset));
float extractedDistance = texDepth.r;
return (extractedDistance <= realDistance) ? (1.0 / 8.0) : 0.0;
}
vec2 offsetArray[8];
void main()
{
vec4 TPos = gl_TexCoord[0];
vec4 SMPos = gl_TexCoord[1];
vec4 MVar = gl_TexCoord[2];
offsetArray[0] = vec2(0.0, 0.0);
offsetArray[1] = vec2(0.0, 1.0);
offsetArray[2] = vec2(1.0, 1.0);
offsetArray[3] = vec2(-1.0, -1.0);
offsetArray[4] = vec2(-2.0, -1.0);
offsetArray[5] = vec2( 2.0, -1.0);
offsetArray[6] = vec2(-2.0, 1.0);
offsetArray[7] = vec2(-2.0, 1.0);
SMPos.xy = SMPos.xy / SMPos.w / 2.0 + vec2(0.5, 0.5);
vec4 finalCol = vec4(0.0, 0.0, 0.0, 0.0);
// If this point is within the light's frustum.
vec2 clampedSMPos = clamp(SMPos.xy, vec2(0.0, 0.0), vec2(1.0, 1.0));
if(clampedSMPos.x == SMPos.x && clampedSMPos.y == SMPos.y && SMPos.z > 0.0 && SMPos.z < MVar.z)
{
float lightFactor = 1.0;
float realDist = MVar.x / MVar.z - 0.002;
for(int i = 0;i < 8; i++)
lightFactor -= 2.5 * testShadow(SMPos.xy, offsetArray[i] * MVar.w, realDist);
if (lightFactor < 0.05) lightFactor = 0.05;
// Multiply with diffuse.
if (MVar.y > 0.25)
finalCol = lightFactor * MVar.y;
else
finalCol = MVar.y;
}
else finalCol = 0.25;
gl_FragColor = texture2D(textureSampler, TPos.xy) * finalCol;
}
On the other hand with the "normal" first texture and the shadow map as second texture I get this:
I have put together a package with the project ready for download at http://dustbin-online.de/download/shader.zip
Does anyone have an idea what I have to change to get this running? Thanks