OpenGL FrameBuffer Object 101
Adding a Depth BufferA FBO on its own isn’t of much use, for it to be usable you have to attach some renderable objects to it; these can be textures or the newly introduced renderbuffers. A renderbuffer are just objects which are used to support offscreen rendering, often for sections of the framebuffer which don’t have a texture format associated with them such as the stencil or depth buffer. In this case we are going to use a renderbuffer to give our FBO a depth buffer to use while rendering. Like the FBO we first of all have to get a handle to a valid renderbuffer: GLuint depthbuffer; glGenRenderbuffersEXT(1, &depthbuffer); Having successfully done this we need to bind the renderbuffer so that it is the current renderbuffer for the following operations: glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthbuffer); As with a FBO bind the first parameter is the ‘target’ you wish to bind to, which right now can only be the indicated target. The depthbuffer variable holds the handle to the renderbuffer we’ll be working with after this. At this point the renderbuffer doesn’t have any storage space associated with it, so we need to tell OpenGL how we want to use it and what size we’d like. In this case we are asking for a depth buffer of a certain size: glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height); Upon successful completion of the above code OpenGL will have allocated space for the renderbuffer to be used as a depth buffer with a given width and height. Note that renderbuffers can be used for normal RGB/RGBA storage and could be used to store stencil information. Having reserved the space for the depth buffer the next job is to attach it to the FBO we created earlier. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthbuffer); While it might look a bit imposing the function is pretty easy to understand; all it is doing is attaching the depthbuffer we created earlier to the currently bound FBO to its depth buffer attachment point. |