启用depth_test时,帧缓冲区对象为黑色

时间:2019-05-28 13:49:40

标签: c++ opengl buffer fbo

我只是想在遍历世界的同时将FBO创建的纹理输出到屏幕,因此有效地制作了2个场景视图。问题是glEnable(GL_DEPTH_TEST)时呈现的任何内容;在上显示为黑色,如果我禁用深度测试,那么它可以按我的方式工作,但是显然我没有任何深度测试。

这些是我的FBO的附件

unsigned int WaterFBO::CreateTextureAttachment(int width, int height)
{
    unsigned int texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0);
    return texture;
}

unsigned int WaterFBO::CreateDepthTextureAttachment(int width, int height)
{
    unsigned int texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, texture, 0);
    return texture;
}

unsigned int WaterFBO::CreateDepthBufferAttachment(int width, int height)
{
    unsigned int depthBuffer;
    glGenRenderbuffers(1, &depthBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
    return depthBuffer;
}

以及我如何创建FBO

void WaterFBO::InitRefracBuffer()
{
    refractionFrameBuffer = CreateFrameBufferObject();
    refractionTexture = CreateTextureAttachment(REFRACTION_WIDTH, REFRACTION_HEIGHT);
    refractionDepthTexture = CreateDepthTextureAttachment(REFRACTION_WIDTH, REFRACTION_HEIGHT);
    UnBindCurrentFrameBuffer();
}

只要发现我注释掉深度缓冲区附件的创建,那么它也可以像禁用深度测试一样工作,所以我想我不正确地创建了深度缓冲区?

void WaterFBO::InitRefracBuffer()
{
    refractionFrameBuffer = CreateFrameBufferObject();
    refractionTexture = CreateTextureAttachment(REFRACTION_WIDTH, REFRACTION_HEIGHT);
    //refractionDepthTexture = CreateDepthTextureAttachment(REFRACTION_WIDTH, REFRACTION_HEIGHT);
    UnBindCurrentFrameBuffer();
}

0 个答案:

没有答案