OpenGL:Framebuffer,不完整的纹理附件

时间:2015-10-04 18:01:14

标签: c++ opengl framebuffer

我已经搜索了几个小时来解决我的问题。 首先。我有HD 7800系列和GPU,最新的驱动程序。

我正在尝试创建具有我需要的所有帧缓冲函数的framebuffer类。看起来我有渲染缓冲区工作,但绘制到纹理导致非常奇怪的错误。

FrameBuffer::FrameBuffer(int width, int height)
{
    createFrameBuffer();
//  createDepthBufferAttachment(width, height); //commented out for testing (THIS WORKS)
    createTextureAttachment(width, height);
//  createDepthTextureAttachment(width, height); //commented out for testing
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
        std::cerr << glCheckFramebufferStatus(GL_FRAMEBUFFER) << "\nFramebuffer failed to generate! 0\n";
    }
    unbindFrameBuffer();


}

FrameBuffer::~FrameBuffer()
{
    if (fbo != NULL)
        glDeleteFramebuffers(1, &fbo);
    if (colorTexture != NULL)
        glDeleteTextures(1, &colorTexture);
    if (depthBuffer != NULL)
        glDeleteRenderbuffers(1, &depthBuffer);
    if (depthTexture != NULL)
        glDeleteTextures(1, &depthTexture);
}

void FrameBuffer::createFrameBuffer(){
    glGenFramebuffers(1, &fbo);

}

void FrameBuffer::createTextureAttachment(int width, int height){
    glGenTextures(1, &colorTexture);
    glBindTexture(GL_TEXTURE_2D, colorTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, GL_RGBA, 0, GL_UNSIGNED_BYTE, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glBindTexture(GL_TEXTURE_2D, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0);
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
        std::cerr << glCheckFramebufferStatus(GL_FRAMEBUFFER) << "\nFramebuffer failed to generate! 1\n";
    }
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

}
void FrameBuffer::createDepthTextureAttachment(int width, int height){
    glGenTextures(1, &depthTexture);
    glBindTexture(GL_TEXTURE_2D, depthTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, width, height, GL_DEPTH_COMPONENT, 0, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glBindTexture(GL_TEXTURE_2D, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
        std::cerr << glCheckFramebufferStatus(GL_FRAMEBUFFER) << "\nFramebuffer failed to generate! 2\n";
    }
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

}

void FrameBuffer::createDepthBufferAttachment(int width, int height){
    glGenRenderbuffers(1, &depthBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
        std::cerr << glCheckFramebufferStatus(GL_FRAMEBUFFER) << "\nFramebuffer failed to generate! 3\n";
    }
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void FrameBuffer::bindFrameBuffer( int width, int height){
    glBindTexture(GL_TEXTURE_2D, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glViewport(0, 0, width, height);
}

void FrameBuffer::unbindFrameBuffer(){
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glViewport(0, 0, DisplayManager::getWidth(), DisplayManager::getHeight());
}

glCheckFrameBufferStatus返回GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT 在两个检查。 createDepthTextureAttachment()也是如此 似乎都没有工作

createDepthBufferAttachment()工作正常。

更多信息: 是的,实际上我实际上给了函数的宽度和高度,它们都是1024。 我尝试将GL_RGBA更改为GL_RGB,并尝试了不同大小的GL_DEPTH_COMPONENT。

目前我打算只使用1个纹理,而不是所有3个功能。

我不知道它是否重要但是SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32);已设定。

我也尝试过glFramebufferTexture()函数。

提前谢谢!

1 个答案:

答案 0 :(得分:5)

glTexImage2D()来电中的参数顺序错误。看第一个:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, GL_RGBA, 0, GL_UNSIGNED_BYTE, NULL);

将其与功能定义进行比较:

void glTexImage2D(
    GLenum target,
    GLint level,
    GLint internalFormat,
    GLsizei width,
    GLsizei height,
    GLint border,
    GLenum format,
    GLenum type,
    const GLvoid* data);

你可以看到第6个参数是边框宽度,第7个参数是格式。你把那两个倒了。电话应该是:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

同样的事情适用于另一个电话。

如果您在使用OpenGL代码时遇到问题,我强烈建议您致电glGetError()。它会很快告诉你这些电话有错误。