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.