OpenGL Multiple Rendering Targets

Multiple Rendering Targets (MRT) are often useful for screenspace approaches such as deferred shading and fast ambient occlusion approximation (SSAO), which requires you outputing a number of Graphics Buffers a.k.a G-Buffers from your shader in rendering pass.

A G-Buffer is typically used to store information of objects such as RGBA color, normal, or depth information in the scene. The deferred shading algorithm makes use of this advantage by using MRT to store these information in G-buffers, and combines them later at a second rendering pass by performing shading algorithms such as Phong/Blinn-Phong using these information captured.

There are 3 stages in setting up MRT:

  1. Initialize the FrameBuffer Object (FBO)
  2. Initialize Textures/RenderBuffers
  3. Bind relevant Textures/RenderBuffers to the FBO

You also have to specify which buffer you have to draw to if you still use immediate mode, which can be set up as:

GLenum* buffers = new GLenum[2];

This will be called in the rendering pass, after binding your FBO:

glDrawBuffers(2, buffers);

However, if you use shaders, you simply specify which buffer to draw to inside your shader. Instead of using the conventional gl_FragColor, you’d opt to use the gl_FragData[i] instead, where i is the index corresponding to the number of GL_COLOR_ATTACHMENT*_EXT you specified in the initialization step.

After drawing to FBO, perform another pass of rendering with another shader that uses the buffers, and render to a final texture, which is used to render to the screen.

I have uploaded an example to demonstrate MRT in OpenGL, which only uses immediate mode to write to the buffers. This is not a deferred renderer, and it simply renders the diffuse buffer onto the screen. You can change it around to render the depth buffer instead, and since the whole setup is basically the same with MRT using shader, you can easily extend it to render normal easily with shader.

You can find the example in this link: ParametricSurface(MRT). You should be able to compile it in Visual Studio 2010 without the need to import any external libraries.

1 thought on “OpenGL Multiple Rendering Targets

Leave a Reply

Your email address will not be published. Required fields are marked *