Search code examples
c++openglfbodepth-buffer

Incomplete FBO when attaching RBO


I'm trying to attach a RenderBuffer Object to a FrameBuffer object, and glCheckFramebufferStatus(GL_FRAMEBUFFER) keeps returning me the error code : GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT

(I'm trying attach a DepthBuffer)

Here is my code (simplified):

// Create and bind the new FBO (1920x1080)

GLuint gBuffer; 
glGenFramebuffers(1, &gBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);


// Create, initialize, bind and attach the new RBO (purpose : depth buffering)

GLuint rbo;
glGenRenderbuffers(1, &rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo);


// Create, bind, and initialize, and attach a texture (the FBO's Color Buffer)

GLuint idMap;
glGenTextures(1, &idMap);
glBindTexture(GL_TEXTURE_2D, idMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, idMap, 0);

std::cout << std::to_string(glCheckFramebufferStatus(GL_FRAMEBUFFER)) << std::endl; // Gives me 36054 ( incomplete ???)


// Re-binding orignal FBO and RBO

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);

What am I doing wrong ?


Things I noticed

I accidentally wrote

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_COMPONENT16, GL_RENDERBUFFER, rbo);

Instead of

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo);

...And there was no error, but the depth test wasn't working at all.

EDIT : It is simply because GL_DEPTH_ATTACHMENT is necessary to attach the buffer. It was just out of the framebuffer...

When rendering in the FBO, I do :

glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); // Use secondary FBO
glBindRenderbuffer(GL_RENDERBUFFER, rbo);   // Use secondary DepthBuffer
glDepthMask(GL_TRUE);                       // Allow depth writing
glEnable(GL_DEPTH_TEST);                    // Enable Depth testing
glDepthFunc(GL_LESS);                       // Pass if fragment is nearer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers

Render(); 

glBindFramebuffer(GL_FRAMEBUFFER, 0);       // Go back to original FBO  
glBindRenderbuffer(GL_RENDERBUFFER, 0);     // to original Depth Buffer


Solution

  • I followed step by step this tutorial : http://www.opengl-tutorial.org/fr/intermediate-tutorials/tutorial-14-render-to-texture/

    The errror occurs when this block of code is missing :

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    

    It's the parameters of the texture (color buffer). In particular, only the second line is necessary to avoid the GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT error.

    It seems that adding a depth buffer without setting some of the texture filtering parameters won't work. (However, it works when there is only a color buffer, surprinsing)