问题
I saw many examples on the web (for example) which do the following
- Create and Bind FBO
- Create and Bind BUFFERS (texture, render, depth, stencil)
- Then, UnBind BUFFERS
- To work with FBO- Bind FBO, do the work then UnBind FBO
- However, also Bind texture BUFFER for read, write etc. with texture BUFFER
- BUT NEVER EVER SEEN re-Bind of other BUFFERS (render, depth, stencil), Why?
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
回答1:
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.
回答2:
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 );
来源:https://stackoverflow.com/questions/6144845/frame-buffer-object-fbo-and-render-depth-buffers-relation