帧缓冲区未完成 GL_DEPTH_ATTACHMENT 渲染到纹理
Framebuffer not completing on GL_DEPTH_ATTACHMENT render to texture
我正在尝试在 OpenGL 中进行阴影贴图,但无法专门 GL_DEPTH_ATTACHMENT 渲染到帧缓冲区。 glCheckFramebufferStatus 未 return 完成,但仅在针对深度附件时完成 - 它适用于颜色附件 0。
glBindFramebuffer(GL_FRAMEBUFFER, ShadowMapFrameBuffer);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glViewport(0, 0, LightMapResolution, LightMapResolution);
glClear(GL_DEPTH_BUFFER_BIT);
// Shader works fine with colour, too.
std::shared_ptr<Shader> mapShader = Shaders["shadowMap"];
glGenTextures(1, &DEBUGSHADOW);
glBindTexture(GL_TEXTURE_2D, DEBUGSHADOW);
// I've tried GL_UNSIGNED_INT in the type and GL_DEPTH_COMPONENT16 as the internal format as per my search results to no avail.
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, LightMapResolution, LightMapResolution, 0, GL_FLOAT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, DEBUGSHADOW, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
clog::Error(CLOGINFO, "Framebuffer broke!", false);
}
// Drawing code omitted. Irrelevant to the issue and functions fine.
glViewport(0, 0, windowWidth, windowHeight);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return;
为了完整起见,着色器取自 LearnOpenGL,如下所示:
#version 460 core
layout (location = 0) in vec3 aPos;
uniform mat4 lightMatrix;
uniform mat4 modelMatrix;
void main()
{
gl_Position = lightMatrix * modelMatrix * vec4(aPos, 1.0);
}
#version 460 core
void main()
{
}
程序在条件“Framebuffer broke!”中输出错误消息。任何时候为 GL_DEPTH_ATTACHMENT 配置缓冲区,我不确定为什么。据我所知,我的代码与前面提到的 LearnOpenGL 文章 (https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping).
没有任何不同。
事实证明这是一个双重问题。在某些时候,我在 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, LightMapResolution, LightMapResolution, 0, GL_FLOAT, GL_FLOAT, NULL);
中创建了一个类型 GL_FLOAT 的第一个实例应该是 GL_DEPTH_COMPONENT。这导致帧缓冲区不完整。
第二个错误是通过glGetError()
发现的。我在帧缓冲区有附件(大概是?)之前调用 glClear()
并且它正在报告 GL_INVALID_FRAMEBUFFER_OPERATION(十进制为 1286 以供参考)。
修复上述问题后,帧缓冲区的行为符合预期。
我正在尝试在 OpenGL 中进行阴影贴图,但无法专门 GL_DEPTH_ATTACHMENT 渲染到帧缓冲区。 glCheckFramebufferStatus 未 return 完成,但仅在针对深度附件时完成 - 它适用于颜色附件 0。
glBindFramebuffer(GL_FRAMEBUFFER, ShadowMapFrameBuffer);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glViewport(0, 0, LightMapResolution, LightMapResolution);
glClear(GL_DEPTH_BUFFER_BIT);
// Shader works fine with colour, too.
std::shared_ptr<Shader> mapShader = Shaders["shadowMap"];
glGenTextures(1, &DEBUGSHADOW);
glBindTexture(GL_TEXTURE_2D, DEBUGSHADOW);
// I've tried GL_UNSIGNED_INT in the type and GL_DEPTH_COMPONENT16 as the internal format as per my search results to no avail.
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, LightMapResolution, LightMapResolution, 0, GL_FLOAT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, DEBUGSHADOW, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
clog::Error(CLOGINFO, "Framebuffer broke!", false);
}
// Drawing code omitted. Irrelevant to the issue and functions fine.
glViewport(0, 0, windowWidth, windowHeight);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return;
为了完整起见,着色器取自 LearnOpenGL,如下所示:
#version 460 core
layout (location = 0) in vec3 aPos;
uniform mat4 lightMatrix;
uniform mat4 modelMatrix;
void main()
{
gl_Position = lightMatrix * modelMatrix * vec4(aPos, 1.0);
}
#version 460 core
void main()
{
}
程序在条件“Framebuffer broke!”中输出错误消息。任何时候为 GL_DEPTH_ATTACHMENT 配置缓冲区,我不确定为什么。据我所知,我的代码与前面提到的 LearnOpenGL 文章 (https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping).
没有任何不同。事实证明这是一个双重问题。在某些时候,我在 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, LightMapResolution, LightMapResolution, 0, GL_FLOAT, GL_FLOAT, NULL);
中创建了一个类型 GL_FLOAT 的第一个实例应该是 GL_DEPTH_COMPONENT。这导致帧缓冲区不完整。
第二个错误是通过glGetError()
发现的。我在帧缓冲区有附件(大概是?)之前调用 glClear()
并且它正在报告 GL_INVALID_FRAMEBUFFER_OPERATION(十进制为 1286 以供参考)。
修复上述问题后,帧缓冲区的行为符合预期。