0

I have a working shadow map implementation for directional lights, where I construct the projection matrix using orthographic projection. My question is, how do I visualize the shadow map? I have the following shader I use for spot lights (which uses a perspective projection) but when I try to apply it to a shadow map that was made with an orthographic projection all I get is a completely black screen (even though the shadow mapping works when renderering the scene itself)

#version 430

layout(std140) uniform;

uniform UnifDepth
{
    mat4 mWVPMatrix;
    vec2 mScreenSize;
    float mZNear;
    float mZFar;
} UnifDepthPass;

layout (binding = 5) uniform sampler2D unifDepthTexture;

out vec4 fragColor;

void main()
{
    vec2 texcoord = gl_FragCoord.xy / UnifDepthPass.mScreenSize;

    float depthValue = texture(unifDepthTexture, texcoord).x;
    depthValue = (2.0 * UnifDepthPass.mZNear) / (UnifDepthPass.mZFar + UnifDepthPass.mZNear - depthValue * (UnifDepthPass.mZFar - UnifDepthPass.mZNear));

    fragColor = vec4(depthValue, depthValue, depthValue, 1.0);
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199
  • 1
    You probably forgot to disable `GL_TEXTURE_COMPARE_MODE`, which means sampling the depth texture using `sampler2D` is undefined (and will probably produce all black texels). If you use sampler objects, you can use a different sampler object with comparison enabled for the actual shadow process and one with comparison disabled when you want to visualize the depth buffer. Otherwise, this state is per-texture object. – Andon M. Coleman Jan 23 '14 at 20:35
  • Yes, that worked it, add it as an answer – KaiserJohaan Jan 23 '14 at 20:49

1 Answers1

1

You were trying to sample your depth texture with GL_TEXTURE_COMPARE_MODE set to GL_COMPARE_R_TO_TEXTURE. This is great for actually performing shadow mapping with a depth texture, but it makes trying to sample it using sampler2D undefined. Since you want the actual depth values stored in the depth texture, and not the result of a pass/fail depth test, you need to set GL_TEXTURE_COMPARE_MODE to GL_NONE first.

It is very inconvenient to set this state on a per-texture basis when you want to switch between visualizing the depth buffer and drawing shadows. I would suggest using a sampler object that has GL_TEXTURE_COMPARE_MODE set to GL_COMPARE_R_TO_TEXTURE (compatible with sampler2DShadow) for the shader that does shadow mapping and another sampler object that uses GL_NONE (compatible with sampler2D) for visualizing the depth buffer. That way all you have to do is swap out the sampler object bound to texture image unit 5 depending on how the shader actually uses the depth texture.

Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106