OpenGL FrameBuffer Object 201
Selecting The DestinationWell, in this case we go back to a function which has been around since the start of OpenGL; glDrawBuffer() This function, and its relative glReadBuffer(), tells OpenGL where it should write data to and where it should read data from. By default both the draw and read buffers are set as GL_FRONT for single buffered contexts and GL_BACK for double buffered ones. With the advent of the FBO extension this function has been modified to allow you to select GL_COLOR_ATTACHMENTx_EXT for rendering to and reading from (where ‘x’ is the attachment point number). When you bind an FBO, the buffers are changed behind your back to GL_COLOR_ATTACHMENT0_EXT. So if you are only rendering to the default colour attachment point you don’t have to make any changes, however when it comes to other buffers we have to tell OpenGL ourselves where we want it to render to. Thus if we want to render to GL_COLOR_ATTACHMENT1_EXT we would have to bind the FBO and set the write buffer to the correct attachment point. Assuming we have attached a texture to colour attachment point 1 for the FBO held in fbo, then rendering would look as follows: glBindFrameBuffer(GL_FRAMEBUFFER_EXT, fbo); glPushAttrib(GL_VIEWPORT_BIT | GL_COLOR_BUFFER_BIT); glViewport(0,0,width, height); // Set the render target glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT); // Render as normal here // output goes to the FBO and it’s attached buffers glPopAttrib(); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); Note the use of glPushAttrib() to save both the viewport and colour buffer configuration before we make the changes and the use of glPopAttrib() to restore them once we are done. This is because these changes affect both the FBO and main rendering context and we don’t want them active once we have completed rendering to the texture. An important point when attaching multiple textures to an FBO is that they all have to be of the same dimension and colour depth. So, you can’t attach a 512*512 32bit texture and a 256*256 16bit texture to the same FBO. However if you can stay within these limits then it is possible to use one FBO to render to multiple textures, which is faster than switching between FBOs. While this isn’t an overly slow operation, avoiding unneeded operations is often good practise. |