Opengl 阴影映射和深度值混淆

Opengl 阴影映射和深度值混淆,opengl,shadow,shadow-mapping,Opengl,Shadow,Shadow Mapping,我正在延迟OpenGL 4.3渲染器中为聚光灯进行阴影映射 我一直在尝试跟随一些关于这个主题的教程,并在之后用片段着色器建模,但我不理解的是计算阴影因子的最终比较。我从深度贴图(“unifShadowTexture”)中采样的值在[0,1]的范围内,因为它直接来自深度缓冲区,但是projCoords.z如何钳制到[0,1]?除法是否被.w钳制为[0,1]? 我面临的问题是,在下图中,场景的主要部分被阴影遮挡,即使它不应该被遮挡,例如底层(忽略灯光瑕疵,因为缺少偏移-重要的一点是模型被照亮,但底层

我正在延迟OpenGL 4.3渲染器中为聚光灯进行阴影映射

我一直在尝试跟随一些关于这个主题的教程,并在之后用片段着色器建模,但我不理解的是计算阴影因子的最终比较。我从深度贴图(“
unifShadowTexture
”)中采样的值在[0,1]的范围内,因为它直接来自深度缓冲区,但是
projCoords.z
如何钳制到[0,1]?除法是否被
.w
钳制为[0,1]? 我面临的问题是,在下图中,场景的主要部分被阴影遮挡,即使它不应该被遮挡,例如底层(忽略灯光瑕疵,因为缺少偏移-重要的一点是模型被照亮,但底层没有):

const std::string gDirLightFragmentShader=
“#版本430\n\
\n\
布局(std140)统一;\n\
\n\
均匀光照\n\
{\n\
mat4 mWVPMatrix;\n\
mat4 MVP矩阵;//灯光视图投影矩阵,预乘以偏移矩阵\n\
vec4 mLightColor;\n\
vec4 mLightDir;\n\
vec4-mGamma;\n\
vec2 mScreenSize;\n\
}UnifDirLightPass;\n\
\n\
布局(绑定=2)统一采样器2D unifPositionTexture;\n\
布局(绑定=3)统一采样器2D unifNormalTexture;\n\
布局(绑定=4)统一采样器2D unifDiffuseTexture;\n\
布局(绑定=5)统一采样器2D unifShadowTexture;\n\
\n\
输出vec4 fragColor;\n\
\n\
void main()\n\
{\n\
vec2 texcoord=gl_FragCoord.xy/UnifDirLightPass.mScreenSize;\n\
\n\
vec3 worldPos=纹理(UnifiPositionTexture,texcoord).xyz;\n\
vec3 normal=规格化(纹理(unifNormalTexture,texcoord).xyz);\n\
vec3漫反射=纹理(unifDiffuseTexture,texcoord).xyz;\n\
\n\
vec4 lightClipPos=UnifDirLightPass.mVPMatrix*vec4(worldPos,1.0);\n\
vec3 projCoords=lightClipPos.xyz/lightClipPos.w;\n\
\n\
浮点深度值=纹理(unifShadowTexture,projCoords.xy).x;\n\
浮动可见性=1.0;\n\
如果(深度值<(projCoords.z))\n\
可见性=0.0;\n\
\n\
浮动角度法线=钳制(点(法线,UnifDirLightPass.mLightDir.xyz),0,1);
const std::string gDirLightFragmentShader =
"#version 430                                                                                                                   \n \
                                                                                                                                \n \
layout(std140) uniform;                                                                                                         \n \
                                                                                                                                \n \
uniform UnifDirLight                                                                                                            \n \
{                                                                                                                               \n \
    mat4 mWVPMatrix;                                                                                                            \n \
    mat4 mVPMatrix;   // light view-projection matrix, pre-multiplied by the bias-matrix                                                                                                          \n \
    vec4 mLightColor;                                                                                                           \n \
    vec4 mLightDir;                                                                                                             \n \
    vec4 mGamma;                                                                                                                \n \
    vec2 mScreenSize;                                                                                                           \n \
} UnifDirLightPass;                                                                                                             \n \
                                                                                                                                \n \
layout (binding = 2) uniform sampler2D unifPositionTexture;                                                                     \n \
layout (binding = 3) uniform sampler2D unifNormalTexture;                                                                       \n \
layout (binding = 4) uniform sampler2D unifDiffuseTexture;                                                                      \n \
layout (binding = 5) uniform sampler2D unifShadowTexture;                                                                       \n \
                                                                                                                                \n \
out vec4 fragColor;                                                                                                             \n \
                                                                                                                                \n \
void main()                                                                                                                     \n \
{                                                                                                                               \n \
    vec2 texcoord = gl_FragCoord.xy / UnifDirLightPass.mScreenSize;                                                             \n \
                                                                                                                                \n \
    vec3 worldPos = texture(unifPositionTexture, texcoord).xyz;                                                                 \n \
    vec3 normal   = normalize(texture(unifNormalTexture, texcoord).xyz);                                                        \n \
    vec3 diffuse  = texture(unifDiffuseTexture, texcoord).xyz;                                                                  \n \
                                                                                                                                \n \
    vec4 lightClipPos = UnifDirLightPass.mVPMatrix * vec4(worldPos, 1.0);                                                      \n \
    vec3 projCoords   = lightClipPos.xyz / lightClipPos.w;                                                                   \n \
                                                                                                                                \n \
    float depthValue = texture(unifShadowTexture, projCoords.xy).x;                                                           \n \
    float visibilty  = 1.0;                                                                                                   \n \
    if (depthValue < (projCoords.z))                                                                                  \n \
         visibilty = 0.0;                                                                                                    \n \
                                                                                                                                \n \
    float angleNormal = clamp(dot(normal, UnifDirLightPass.mLightDir.xyz), 0, 1);                                               \n \
                                                                                                                                \n \
    fragColor = vec4(diffuse, 1.0) * visibilty * angleNormal * UnifDirLightPass.mLightColor;                                 \n \
}                                                                                                                               \n";
float depthValue = texture(unifShadowTexture, projCoords.xy).x;
float visibilty  = 1.0;

if (depthValue < (projCoords.z))
  visibilty = 0.0;            
float visibility = texture (unifShadowTexture, projCoords);