Friday 22 March 2013

FBOs: why and how



With the next generation of console games just around the corner, we should look back at how our games have advanced in the past few years. For this generation of games not only have they transited into the High Definition era, but also alot of special effects have been applied to our games. Specifically, we have seen a variety of full screen/post processing effects, such as from motion blur, to bloom and HDR(High Dynamic Range).

Why do we need FBO's

 

For those of you who have used Photoshop, you probably would have used a filter of some sort to make your picture look better. Now think about how would you apply that to "the game", especially a 3D, where some objects will be sticking out, while others will be "deeper" in the screen. So what we need a "screen shot" of the current screen, with that we can apply filters and other effects. Thats what the FBO is, a simple flat plane("texture") which the scene is rendered to first. After you are done, then you would apply your filters to it and display it.

Think of the scene as a stage actor getting ready to go on stage. Prior to ever show an actor must put on their custom, and usually some sort of make up, and thats what we do during the FBO stage.
HDR

Bloom


FBO (Frame buffer object)

 

To do this, we need a FBO(Frame buffer object) which stores the current scene, in a texture, sort of like a special picture. Here we store how the scene looks, the variables we need to analyize it, and sometimes we will specialize the buffer object to store a specific variable. Here is how a very plane scene will look with an FBO side by side the actual scene, WITHOUT any shaders .

 Heres how would your drawing Pipeline (order) should go with Shaders/FBO 
  1. Bind the FBO, like turning on the camera to start its recording
  2. Bind the shaders to start making the scene pretty, but not the post processing ones
  3. Set the camera position, and apply all transformations.
  4. Draw ALL THE OBJECTS 
  5. Unbind the shaders which were bound in step 4, for this update their jobs are done 
  6. Unbind the FBO, you just finished recording the whole scene  
  7. Do postprocess, start by applying the post processing shaders 
  8. Now apply the fbo's Texture to a full screen quad function, where it will project the game on to a flat screen
  9.  unbind all shaders
  10.  swap the buffers( I haven't used it for SFML, but glut most definably need this to be done 
  11.  

Creating An FBO 

 

Creating an FBO we need 3 steps: Create the FBO handle, create the colour storage, and finally depth storage using a texture. Just make sure to include Glew.h file and call the function glewinit prior  to initializing the game for this to work. 

 Binding the FBO

 

Just use the simple code below, to bind the gframe buffer before using it ANYWHERE. However don't forget to unbind when you are done with it, which is simply done with a similar code, glBindFramebudder(GL_FRAMEBUFFER,0);


  
   

The FBO Creation

 

This can be be as simple as the code below, as I am making this for an FBO class. Below is the explaination of the code. 

  • You create the FBO using glGenFrameBuffer (number of FBOs, FBO name)
  • Now you bind the FBO using glBindFrameBuffer(Gl_FRAMEBUFFER, FBOname)
  • fboWidth, and fboHeight- this is storing the size of the FBO, so you can pass in the screen size once, and not ever need to pass it again.
  • Now you unbing the  FBO using glBindFrameBuffer(Gl_FRAMEBUFFER, 0)

 

Create the Colour+ Depth storage 

 

Basically when creating a colour and depth storage, it is stored in a 2D texture, based on the camera view. When creating a color and depth storage, you can copy and paste the code below. 
  • Red is where you must bind, and unbind your frame buffer.
  • Orange is where the texture is being generated, binded, and created. If you are copy and pasting this code, make sure Colour storage uses ColourStorageTextures variables, and depth storage uses depth StorageTextures variables
  • Blue is another spot where the frame buffer data will change from Depth and colour storage
  

 

Other parameters 

 

Below are some other parameters you might want to use  when initialing.


As you can see,  there are only 3 extra variable which will affect the texture creation if you are using specific shaders.  For example if you are using HDR, your interal format and internal type wil change. In this picture I have colour coded accroding to each of the variables, so you can easily find it without me explaining. However the 2nd line of if statements may be confusing for those who haven't seen it before, so I have translated it below.

 

Conclusion 

FBOs are powerful objects which are needed for some effects, hopefully this has helped you out. Hopefully your FBO won't look like mine when I first implemented it into my game.




No comments:

Post a Comment