【发布时间】:2017-03-08 01:14:49
【问题描述】:
我正在尝试在我的延迟渲染管道中实现阴影贴图,但我遇到了一些实际生成阴影贴图,然后对像素进行阴影处理的问题——我认为应该被遮蔽的像素根本不是。
我有一个定向光,它是我引擎中的“太阳”。我已经为照明设置了延迟渲染,到目前为止它工作正常。我再次将场景渲染为阴影贴图的仅深度 FBO,使用以下代码生成视图矩阵:
glm::vec3 position = r->getCamera()->getCameraPosition(); // position of level camera
glm::vec3 lightDir = this->sun->getDirection(); // sun direction vector
glm::mat4 depthProjectionMatrix = glm::ortho<float>(-10,10,-10,10,-10,20); // ortho projection
glm::mat4 depthViewMatrix = glm::lookAt(position + (lightDir * 20.f / 2.f), -lightDir, glm::vec3(0,1,0));
glm::mat4 lightSpaceMatrix = depthProjectionMatrix * depthViewMatrix;
然后,在我的光照着色器中,我使用以下代码来确定像素是否处于阴影中:
// lightSpaceMatrix is the same as above, FragWorldPos is world position of the texekl
vec4 FragPosLightSpace = lightSpaceMatrix * vec4(FragWorldPos, 1.0f);
// multiply non-ambient light values by ShadowCalculation(FragPosLightSpace)
// ... do more stuff ...
float ShadowCalculation(vec4 fragPosLightSpace) {
// perform perspective divide
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// vec3 projCoords = fragPosLightSpace.xyz;
// Transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;
// Get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
float closestDepth = texture(gSunShadowMap, projCoords.xy).r;
// Get depth of current fragment from light's perspective
float currentDepth = projCoords.z;
// Check whether current frag pos is in shadow
float bias = 0.005;
float shadow = (currentDepth - bias) > closestDepth ? 1.0 : 0.0;
// Ensure that Z value is no larger than 1
if(projCoords.z > 1.0) {
shadow = 0.0;
}
return shadow;
}
但是,这并不能真正让我得到我想要的东西。这是阴影后输出的屏幕截图,以及在 Photoshop 中半成品转换为图像的阴影贴图:
由于定向光是我的着色器中唯一的光,看起来阴影贴图的渲染非常接近正确,因为透视/方向大致匹配。然而,我不明白的是,为什么没有一个茶壶最终会在其他茶壶上投下阴影。
我很感激任何关于我可能做错了什么的指示。我认为我的问题在于计算该光空间矩阵(我不确定如何正确计算,给定一个移动的相机,以便更新视图中的内容)或我确定的方式延迟渲染器正在着色的纹素是否处于阴影中。 (FWIW,我从深度缓冲区确定世界位置,但我已经证明这个计算是正确的。)
感谢您的帮助。
【问题讨论】:
标签: c++ opengl glsl shadow-mapping