Question

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);
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top