Title: Frame Buffer Object
1Frame Buffer Object
- CG2 Repetitorium 2, SS08Veronika Solteszova
Institute of Computer Graphics and
Algorithms Vienna University of Technology
2What for?
- What is normally rendered to the screen
- ?
- Save as a texture (with color and depth
attachments) - Use the texture in further passes (shadow map,
mini-map, showing different views of the scene
3What is an FBO actually ?
- FBO is an encapsulation of attachments
- Attachments can be color or renderbuffers
- Renderbuffers are objects that support off-screen
rendering without an assigned texture - Depth buffer and stencil buffer
- There can be up to 8 color attachments
- Number depends on your HW
- More than one is advanced stuff
- Wont deal with it too much
4Setup FBO
- Some code you have to use for setup
- Create and validate its handle
- Bind it
GLuint fbo glGenFramebuffersEXT(1, fbo)
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo)
5Setup renderbuffer (depth buffer)
- Attaching renderable objects (like depth buffer -
DB) - Create its handle
- Bind it
GLuint depthbuffer glGenRenderbuffersEXT(1,
depthbuffer)
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,
depthbuffer)
6Creating storage space
- We define its size
- Last creators job attaching DB to FBO
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT,
GL_DEPTH_COMPONENT, width, height)
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT,
depthbuffer)
7Attaching a texture T to render to
- Assumption a handle to a texture
- NOTE width and height are the same as those of
FBO!
GLuint img glGenTextures(1, img)
glBindTexture(GL_TEXTURE_2D, img)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width,
height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL)
8Attaching a texture T to render to
- We set the target of the FBO
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, img, 0)
9Status checking
- If everything worked out correctly
GLenum status glCheckFramebufferStatusEXT(GL_FRA
MEBUFFER_EXT)
10Rendering to the texture T
- Bind render unbind
- // render scene
- Note keep the same height and width of T for the
viewport size
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo)
glPushAttrib(GL_VIEWPORT_BIT) glViewport(0,0,wid
th, height)
glPopAttrib() glBindFramebufferEXT(GL_FRAMEBUFFE
R_EXT, 0)
11Using the texture T
- Just bind it like a regular texture
- Note dont forget about texture filtering stuff!
glBindTexture(GL_TEXTURE_2D, img)
12And not only that
- With an FBO, you can render into more than 1
textures simultaneously - Check the tutorials about more color attachments
and DrawBuffers extension at www.gamedev.net - http//www.gamedev.net/reference/programming/featu
res/fbo2/
13Cleaning up (the mess?)
- Clean up, unbinding, deleting is done by
- Cleaning of render buffers
- Delete also all textures associated with FBO
- Source www.gamedev.net
glDeleteFramebuffersEXT(1, fbo)
glDeleteRenderbuffersEXT(1, depthbuffer)