Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
116 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

OpenGL FrameBuffer Object 201


MRT with FBO and GLSL

At this point if we were to render it using the standard Fixed Function Pipeline (as the examples have used thus far) then both textures would get the same data in them, however using GLSL we can write a fragment shader which allows us to send different data to the textures.

Normally when you write a GLSL fragment shader you would output the colour value to gl_FragColor, which would then be written to the frame buffer as normal. However there is a second way to write out colour information via the gl_FragData[] array.

This special variable allows us to direct where the data is going and maps directly to the values given to glDrawBuffers(). So, in the case of the glDrawBuffers() call above the buffers would map as follows:

glDrawBuffers value
FragData syntax
GL_COLOR_ATTACHMENT0_EXT
gl_FragData[0]
GL_COLOR_ATTACHMENT1_EXT
gl_FragData[1]

If we were to change the above function call however the mappings would change:

GLenum buffers[] = { GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT0_EXT };
glDrawBuffers(2, buffers);
glDrawBuffers value
FragData syntax
GL_COLOR_ATTACHMENT1_EXT
gl_FragData[0]
GL_COLOR_ATTACHMENT0_EXT
gl_FragData[1]

This is highlighted because it is the order the values are supplied to the glDrawBuffers() function, which dictates how they map to the gl_FragData[] array, not their values.

Lets say that for some reason we wanted to write green to one render target and blue to the other, then the GLSL code would look as follows:

#version 110

void main()
{
	gl_FragData[0] = vec4(0.0, 1.0, 0.0);
	gl_FragData[1] = vec4(0.0, 0.0, 1.0);
}

The first line says we need at least version 1.10 (OGL2.0) of the GLSL and the function body just writes green to the first buffer and blue to the second buffer.





The Second Example


Contents
  Introduction
  Selecting The Destination
  The first example
  Multiple Render Targets
  The Draw Buffers Extension
  MRT with FBO and GLSL
  The Second Example
  Final Thoughts

  Source code
  Printable version
  Discuss this article

The Series
  OpenGL Frame Buffer Object 101
  OpenGL Frame Buffer Object 201