I'm currently working on a shadow mapping implementation for my game engine, it seems to be rendering and casting the shadow map well but if the shadow is too close from the emitter, it seems to be cut, as shown in this screenshot:
(the white lines are there to check if the shadow is at the right location, they are projected from the spot light origin towards the cube's vertices).
As you can see, the shadow is cut, as it should start from the cube's edge on the floor.
I'm using a 256*256 depth16 shadow map rendered from the light point of view with a perspective matrix:NzMatrix4f::Perspective(lightComponent.GetOuterAngle()*2.f, 1.f, 2.f, lightComponent.GetRadius())
Which ultimately gives us the following projection matrix:
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, -1.02041, -1,
0, 0, -2.04082, 0
I found out that reducing the zFar value was a little bit improving the shadow:
zNear = 0.1, zFar = 1000
zNear = 0.1, zFar = 500
I think the problem comes from the test, although I have no idea what I'm doing wrong.
Here's the shader code (when projecting the shadow):
Vertex Shader:
vLightSpacePos = LightProjMatrix * LightViewMatrix * WorldMatrix * vec4(VertexPosition, 1.0);
Fragment Shader:
#if SHADOW_MAPPING
if (vLightSpacePos.w > 0.0)
{
vec3 ProjectionCoords = vLightSpacePos.xyz / vLightSpacePos.w;
vec2 UVCoords;
UVCoords.x = 0.5 * ProjectionCoords.x + 0.5;
UVCoords.y = 0.5 * ProjectionCoords.y + 0.5;
float Depth = texture(ShadowMap, UVCoords).x;
if (Depth < ProjectionCoords.z)
{
lightDiffuse *= 0.5;
lightSpecular = vec3(0.0);
}
}
#endif
Here's a video I made to show the bug, with a spotlight casting a shadow from a cube (both are not moving) and where I'm making the floor going down, the shadow seems to fix itself once the distance is great enough:
Am I missing something?
I fixed my problem, it was caused by the bias (it should be applied before the perspective division, not after).