OpenGL中带有聚光灯的阴影贴图会产生不寻常的效果
我一直在尝试实现阴影映射。虽然我认为我现在很接近了,但我却被一种奇怪的效果所困扰(如下所示): 如您所见,阴影区域看起来太小。立方体本身也有一种不寻常的效果 正在渲染的几何体是尺寸为1.0的立方体,位于尺寸为100.0的正方形平面上。场景包含单个聚光灯,其角度(从一侧到另一侧)为0.5弧度,范围为100.0。该聚光灯围绕y轴旋转,并调整其旋转以查看原点 我设置帧缓冲区和深度纹理(512 x 512),如下所示:OpenGL中带有聚光灯的阴影贴图会产生不寻常的效果,opengl,glsl,shadow,shadow-mapping,Opengl,Glsl,Shadow,Shadow Mapping,我一直在尝试实现阴影映射。虽然我认为我现在很接近了,但我却被一种奇怪的效果所困扰(如下所示): 如您所见,阴影区域看起来太小。立方体本身也有一种不寻常的效果 正在渲染的几何体是尺寸为1.0的立方体,位于尺寸为100.0的正方形平面上。场景包含单个聚光灯,其角度(从一侧到另一侧)为0.5弧度,范围为100.0。该聚光灯围绕y轴旋转,并调整其旋转以查看原点 我设置帧缓冲区和深度纹理(512 x 512),如下所示: // Create and configure the depth texture
// Create and configure the depth texture.
glGenTextures(1, &m_depthTexture);
glBindTexture(GL_TEXTURE_2D, m_depthTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
GLfloat border[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, (void*)0);
// Assign the depth texture to texture channel 0.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_depthTexture);
// Create and configure the framebuffer.
glGenFramebuffers(1, &m_framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthTexture, 0);
GLenum drawBuffers[] = { GL_NONE };
glDrawBuffers(1, drawBuffers);
然后从聚光灯的角度将场景渲染到阴影贴图帧缓冲区。这似乎奏效了。使用OpenGL调试工具检查深度纹理显示以下内容:
第二次渲染场景,我为深度纹理和阴影矩阵设置制服:
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, shadowMap.depthTexture());
program->uniform("shadowMap", 1);
const M3D::Matrix4 lightViewMatrix = lightTransformComponent->transformationMatrix().inverse();
const float invTanHalfFov = 1.0f / std::tan(coneAngle * 0.5f);
const float nearClipPlane = 0.3f;
const float farClipPlane = lightLightComponent->range();
const float zRange = nearClipPlane - farClipPlane;
const Matrix4 lightProjectionMatrix(
invTanHalfFov, 0.0f, 0.0f, 0.0f,
0.0f, invTanHalfFov, 0.0f, 0.0f,
0.0f, 0.0f, -(nearClipPlane + farClipPlane) / zRange, 2.0f * nearClipPlane * farClipPlane / zRange,
0.0f, 0.0f, 1.0f, 0.0f
);
const Matrix4 shadowMatrix = lightProjectionMatrix * lightViewMatrix * modelMatrix;
program->uniform("shadowMatrix", shadowMatrix);
我在顶点着色器中计算阴影坐标:
f_shadowCoordinate = shadowMatrix * vec4(v_position, 1.0f);
然后,在片段着色器中,我投影此坐标并将其偏移到间隔[0,1]内的范围
vec2 projectedShadowCoordinates = (f_shadowCoordinate.xy / f_shadowCoordinate.w) * 0.5f + vec2(0.5f, 0.5f);
float shadowDistance = texture(shadowMap, projectedShadowCoordinates).x;
return vec4(1.0f) * shadowDistance;
编辑
令人烦恼的是,在渲染到阴影帧缓冲区时,错误地将投影矩阵统一设置为摄影机的投影矩阵(而不是灯光的投影矩阵),导致了此问题。您解决了此问题吗?如果是这样的话,你自己把它作为一个答案贴出来,接受它。投票被否决,这个问题在3年前就解决了,但没有解决。