Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL: Framebuffer, incomplete texture attachment

I've searched for couple hours for a solution to my problem. First of all. I have HD 7800 series amd GPU, newest drivers.

I'm trying to create framebuffer class which has all the framebuffer functions I will need. It seems I got renderbuffers to work, but drawing to texture is causing really weird errors.

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 returns GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT at both checks. The same happens with the createDepthTextureAttachment() Neither one seems to not work

createDepthBufferAttachment() works fine.

More info: Yes, indeed I'm actually giving width and height to the function, they are both 1024. I've tried changing the GL_RGBA to GL_RGB, and Tried different sizes of GL_DEPTH_COMPONENT too.

At the moment I'm intending on using only 1 texture, not all 3 functions.

I don't know if it matters but SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32); is set.

I've also tried glFramebufferTexture() function instead.

Thank you in advance!

like image 287
Trinedy Avatar asked Oct 04 '15 18:10

Trinedy


1 Answers

The order of arguments in your glTexImage2D() calls is wrong. Looking at the first one:

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

Comparing that to the function definition:

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

you can see that the 6th argument is the border width, and the 7th is the format. You have those two inverted. The call should be:

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

The same thing applies to the other call.

Anytime you have problems getting your OpenGL code working, I strongly recommend to call glGetError(). It would quickly tell you that there are errors from these calls.

like image 122
Reto Koradi Avatar answered Nov 06 '22 20:11

Reto Koradi