0
\$\begingroup\$

This is my first attempt in implementing shadow mapping. I am using right hand coordinate system and my projection matrices are producing z values from 0 to 1. I've decided first to use orthogonal projection matrix for shadows:

mat4 LightProj = Ortho(-10, 10, -10, 10, -10, 20);
mat4 LightView = LookAt(-4, 4, -2);

This is how I produce my data for shadows. Vertex Shader:

mat4 Bias = mat4
    (0.5, 0.0, 0.0, 0.0,
     0.0, 0.5, 0.0, 0.0,
     0.0, 0.0, 1.0, 0.0,
     0.5, 0.5, 0.0, 1.0
     );
    Out.ShadowPos  = Bias * WorldUpdate.LightProj * WorldUpdate.LightView * Out.Pos;

Fragment shader:

float GetShadow(vec4 PositionInLightSpace, vec3 Normal, vec3 LightDir)
{
    float  Bias = 0.0; //05; //max(0.05 * (1.0 - dot(Normal, LightDir)), 0.005);
    vec3   ProjPos     = PositionInLightSpace.xyz / PositionInLightSpace.w;
    float  ShadowDepth = texture(ShadowMap, ProjPos.xy).x;
    float  ObjectDepth = ProjPos.z;
    float  Result      = (ObjectDepth - Bias) > ShadowDepth ? 0.0 : 1.0;
    return Result;
}

    float Shadow  = GetShadow(In.ShadowPos, Normal, LightDir);
    vec3 Ambient  = vec3(1.0, 1.0, 1.0) * 0.2;

    OutputColor = vec4(Ambient + Shadow * Light, 1.0) * In.Col;

At first glance everything looks fine. Even generated shadow map: enter image description here But the results looks really way off the correct solution: enter image description here How can I solve this issue? Thanks in advance

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

So, as I was using vulkan, it should be like this:

mat4 Bias = mat4
    (0.5,  0.0, 0.0, 0.0,
     0.0, -0.5, 0.0, 0.0,
     0.0,  0.0, 1.0, 0.0,
     0.5,  0.5, 0.0, 1.0);

The thing is that in vulkan everything being rendered from bottom to top and therefor in my renderer Y is reversed. That's it.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This answer would be even better if it explained why this change fixes the problem. \$\endgroup\$
    – DMGregory
    Jul 22, 2023 at 11:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .