samedi 27 juin 2015

Shadow mapping: my shadow is correct but cut

this is my first time on SO so I may miss a few things, tell me if that's the case. Also I'm from Belgium so my English may not be perfect.

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: Screenshot
(the white lines are there to check if the shadow is at the right location, they are projection from the spot light origin towards the cube's vertices).

As you can see, the shadow is cut, as it should starts 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 ultimatly 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: https://youtu.be/sisbOOml_cg

Am I missing something?

Aucun commentaire:

Enregistrer un commentaire