OpenGL:Depth Attachment打破Framebuffer

时间:2016-12-28 12:26:10

标签: opengl framebuffer depth-buffer

我需要一双新鲜的眼睛。在重写我的引擎时,我在编写Deferred Rendering路径时偶然发现了这个问题。帧缓冲区仅在我不使用深度附件时显示,这意味着渲染有问题,但如果我这样做,则所有输出都是空白的。我写了很多图形处理类,但我在这里分解了代码:

初​​始化:

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glClearColor(0, 0, 0, 1);
glViewport(0, 0, 1024, 768);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ShaderPreparation();

numBuffer = 3;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
numBuffers = numBuffer;
targetBuffer = 0;
textures = new unsigned int[numBuffers];
glGenTextures(numBuffers, textures);

这样做了三次:

glBindTexture(GL_TEXTURE_2D, textures[targetBuffer]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, colorType, width, height, 0, colorFormat, colorDataType, NULL);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + targetBuffer, GL_TEXTURE_2D, textures[targetBuffer], 0);
targetBuffer++;

然后我创建深度附件:

glGenTextures(1, &renderBuffer);
glBindTexture(GL_TEXTURE_2D, renderBuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, renderBuffer, 0);

编辑2:忘记了FBO的最后一位:

GLenum *DrawBuffers = new GLenum[numBuffers];
for (size_t i = 0; i < numBuffers; i++)
    DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;

glDrawBuffers(numBuffers, DrawBuffers);

// Report errors
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
    fprintf(stderr, "Framebuffer Error. Status 0x%x\n", status);
}

// Unbind
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

最后,我的绘图(几何着色器的所有计算都在此之前发生):

geometryShader->Use();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
graphicsWrapper->render(Geometry);

int val[3] = { 0,1,2 };
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
deferredShader->Use();

glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, textures[1]);
glActiveTexture(GL_TEXTURE0 + 2);
glBindTexture(GL_TEXTURE_2D, textures[2]);

// I use this system so it's compatible with Uniform Buffer
//       Objects and the rendering code of other graphics languages
deferredShader->PassData(&val);
deferredShader->SetInteger();
deferredShader->SetInteger();
deferredShader->SetInteger();

glClear(GL_COLOR_BUFFER_BIT);

vaoQuad->Bind();
graphicsWrapper->DrawVertexArray(4);
vaoQuad->Unbind();

请注意,我的代码比面向对象更加面向对象,我不得不从上下文中删除大量代码。我的问题是,为什么将深度附件附加到帧缓冲区导致帧缓冲区空白,同时删除它可以工作,我该如何修复它?

编辑:我知道它不是渲染缓冲区,我只是调用深度纹理,因为它被用作渲染缓冲区,我忘了更改名称。

1 个答案:

答案 0 :(得分:2)

如果没有深度缓冲区,每个片段都会自动通过深度测试。现在添加深度缓冲区后,深度测试将生效(假设您已启用它)。但是,你忘了清除你的深度缓冲区:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);

清除默认帧缓冲区的颜色和深度缓冲区(或另一个FBO,当时绑定的那个),但不清除此处fbo的深度缓冲区...

您的深度纹理最有可能全部为零。使用默认深度约定,这意味着它已经是最接近的值,因此深度测试可能会因您绘制的任何内容而失败。