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);
}
4

1 回答 1

1

您试图使用GL_TEXTURE_COMPARE_MODEset to对深度纹理进行采样GL_COMPARE_R_TO_TEXTURE。这对于实际使用深度纹理执行阴影映射非常有用,但它会尝试使用sampler2Dundefined 对其进行采样。由于您希望将实际深度值存储在深度纹理中,而不是通过/失败深度测试的结果,因此您需要先设置GL_TEXTURE_COMPARE_MODEGL_NONE

当您想要在可视化深度缓冲区和绘制阴影之间切换时,基于每个纹理设置此状态非常不方便。我建议对执行阴影映射的着色器使用GL_TEXTURE_COMPARE_MODE设置为GL_COMPARE_R_TO_TEXTURE(兼容sampler2DShadow)的采样器对象,以及使用GL_NONE(兼容sampler2D)来可视化深度缓冲区的另一个采样器对象。这样,您所要做的就是根据着色器实际使用深度纹理的方式替换绑定到纹理图像单元 5 的采样器对象。

于 2014-01-23T20:58:15.860 回答