我正在尝试按照教程将实时阴影映射添加到我的类似 Minecraft 的克隆中:这里
首先会发生什么:
据我所知,阴影贴图渲染正确吗?问题似乎出在最后一次传球期间。
我创建深度纹理:
glGenFramebuffers(1, &m_frameBufferId);
glBindFramebuffer(GL_FRAMEBUFFER, m_frameBufferId);
glGenTextures(1, &m_depthTexture);
glBindTexture(GL_TEXTURE_2D, m_depthTexture);
glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT16, 1024, 1024, 0,GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_depthTexture, 0);
glDrawBuffer(GL_NONE);
注意:我还尝试在着色器中使用带有 sampler2DShadow 的 GL_COMPARE_R_TO_TEXTURE。同样的结果。
然后运行两个通道:
// ==================== Shadow =====================
glBindFramebuffer(GL_FRAMEBUFFER, m_frameBufferId);
glViewport(0, 0, 1024, 1024);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ShadowShader->Bind();
VCSceneGraph::Instance->RenderGraph();
// ==================== Final =======================
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, 1280, 800);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
VoxelShader->Bind();
VCSceneGraph::Instance->RenderGraph();
阴影着色器代码:
// ==================== Vert =====================
#version 330 core\n
in vec4 vertexPosition_modelspace;
uniform mat4 depthMVP;
void main()
{
gl_Position = depthMVP * vertexPosition_modelspace;
}
// ==================== Frag =====================
#version 330 core\n
out float fragmentdepth;
void main()
{
fragmentdepth = gl_FragCoord.z;
}
Final-Pass(体素着色器)代码:
// ==================== Vert =====================
#version 330 core\n
in vec3 position;
in int normal;
in vec4 color;
out vec4 colorVarying;
out vec4 ShadowCoord;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 DepthBiasMVP;
void main()
{
gl_Position = modelViewProjectionMatrix * vec4(position, 1);
ShadowCoord = DepthBiasMVP * vec4(position, 1);
colorVarying = color;
}
// ==================== Frag =====================
#version 330 core\n
in vec4 colorVarying;
in vec4 ShadowCoord;
out vec4 color;
uniform sampler2D shadowMap;
void main()
{
float visibility = 1.0;
if ( texture(shadowMap, ShadowCoord.xy).z < ShadowCoord.z)
{
visibility = 0.5;
}
color.xyz = colorVarying.xyz * visibility;
color.w = colorVarying.w;
}
我怀疑我的问题出在更新制服以进行最终通行证的代码中:
// Re-create the exact same MVP matrix that was used for the shadow pass
glm::mat4 depthProjectionMatrix = glm::ortho<float>( -30, 30, -30, 30, -100, 100);
glm::mat4 depthViewMatrix = glm::lookAt(glm::vec3(0.5f, 2, 2), glm::vec3(0,0,0), glm::vec3(0,1,0));
depthViewMatrix = glm::translate(depthViewMatrix, -15.0f, 30.0f, 0.0f);
glm::mat4 depthMVP = depthProjectionMatrix * depthViewMatrix;// * modelMatrix;
// Multiply it be the bias matrix
glm::mat4 biasMatrix
(
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0
);
glm::mat4 depthBiasMVP = biasMatrix * depthMVP;
// Create Camera's MVP
VCCamera* currentCamera = VCSceneGraph::Instance->CurrentRenderingCamera;
glm::mat4 ProjectionMatrix = currentCamera->ProjectionMatrix;
glm::mat4 ViewMatrix = currentCamera->ViewMatrix;
glm::mat4 MVP = ProjectionMatrix * ViewMatrix;// * modelMatrix;
// Update uniforms
glUniformMatrix4fv(m_unifMVP, 1, GL_FALSE, &MVP[0][0]);
glUniformMatrix4fv(m_unifDepthMVP, 1, GL_FALSE, &depthBiasMVP[0][0]);
编辑:
我通过使阴影纹理与窗口具有相同的分辨率来解决问题。但是,我无法解释为什么会解决它。如果其他人想破解它,我会接受你的回答。预期的输出是这样的:
我还应该注意到完整的源代码可以在:GitHub 上找到。