我正在为延迟的 OpenGL 4.3 渲染器中的聚光灯做阴影映射。
我一直在尝试遵循有关该主题的一些教程,并在其后通过片段着色器进行建模,但我不明白的是计算阴影因子的最终比较。我从深度图(“”)中采样的值unifShadowTexture
在 [0, 1] 的范围内,因为它直接来自深度缓冲区,但如何projCoords.z
钳制到 [0, 1]?.w
是否通过将其钳位到 [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";