Rudi
Rudi

Reputation: 750

Frame Buffer Object (FBO) and Render & Depth Buffers Relation

I saw many examples on the web (for example) which do the following

Example of BUFFERS creation and bind/unbind (Below code is just for example only to show what I explained and works perfectly),

// create a framebuffer object, you need to delete them when program exits.
glGenFramebuffersEXT(1, &fboId);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboId);

// create color buffer object and attached to fbo
glGenRenderbuffersEXT(1, &rboId);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rboId);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, TEXTURE_WIDTH, TEXTURE_HEIGHT);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0); //UnBind

if(useDepthBuffer) {
  glGenRenderbuffersEXT(1, &rboIdDepth);
  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rboIdDepth);
  glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, TEXTURE_WIDTH, TEXTURE_HEIGHT);
  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0); //UnBind
}

glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, rboId);
if(useDepthBuffer)
  glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, rboIdDepth);


// check FBO status
printFramebufferInfo();
bool status = checkFramebufferStatus();
if(!status)
  fboUsed = false;
.
//then,
.
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboId);
// Do the work
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

1. Why dont we need to bind all the BUFFERS again (I mean while working-with/drawing-objects-to FBO)?

2. What is going on under-the-hood here?

EDIT: attach-> Bind and deattach-> UnBind

Upvotes: 3

Views: 6186

Answers (2)

datenwolf
datenwolf

Reputation: 162164

I think by

  • Then, deattach BUFFERS

You refer to this

glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rboIdDepth);

However this is not a detach, this is a unbind, which means something different. The renderbuffer is still attached to the FBO. The binding however selects the buffer object on which the following buffer object operations are to be performed on. It's kinda like the with statement found in some languages.

The actual attaching of a buffer object, that doesn't have to be bound, happens here

glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
                             GL_COLOR_ATTACHMENT0_EXT, 
                             GL_RENDERBUFFER_EXT, 
                             rboId ); // rboID != 0

It would be detached by a matching

glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
                             GL_COLOR_ATTACHMENT0_EXT, 
                             GL_RENDERBUFFER_EXT, 
                             0 );

Upvotes: 3

Christian Rau
Christian Rau

Reputation: 45948

I don't know if I completely understood you, but the renderbuffers bound to the attachment points (GL_COLOR_ATTACHMENT...) are per-FBO state and this FBO state remains unchanged you only need to bind the FBO to tell OpenGL that this FBO is now used and all its state (that you set earlier) will take effect.

Upvotes: 8

Related Questions