FBO feeedbackbuffer

This is a continuation of the first FBO tutorial, this time we will use two of them, one for rendering and the other will be used as a feedback buffer, witch if you have seen a disco music video from the seventies you will know what i mean.
Basically a feedback buffer allows you to overdraw this frames rendering with all the previous frames renderings using an alpha blending.
Now normally one would think, why not do it directly into the back buffer, well that’s because you can’t be sure that you will get the previous rendering there after swapping the buffer, and if your triple buffering your in trouble.
A feedback FBO is a comfortable way of getting around that, since data is not shuffled around in them when you swap the buffers.
Although this is a neat effect, but because of all the low precision errors we get we will actually start using a little bit of HDR rendering, int’s not a lot, but it will remove all those artifacts we get but, it’s not full HDR rendering, that is a whole other tutorial with.

So what do you need them,
1.in the init function double up on the number of frame buffers with a simple copy paste and then just add a “2” after each FBO variable.
And because we don’t want any precision down sampling errors we set the internal format on the FBO#2 color buffer texture to GL_RGBA16F_ARB instead of GL_RGBA, this will make the blur look as smooth as ever.

2. just render to FBO#1 as you did in the first tutorial.

3. move the contents of FBO#1 to FBO#2 with an alpha blending like this

glViewport (0, 0, 512, 512);
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb2);
checkfbo();
glClear (GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
ViewOrtho(512,512);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, color_tex);

glBegin(GL_QUADS);
glColor4f(1.0f, 1.0f, 1.0f, 0.1f);
glTexCoord2f(0,1);
glVertex3f(0,0,0);
glTexCoord2f(0,0);
glVertex3f(0,512,0);
glTexCoord2f(1,0);
glVertex3f(512,512,0);
glTexCoord2f(1,1);
glVertex3f(512,0,0);
glEnd();

glDisable(GL_BLEND);
ViewPerspective();

Now about ViewOrtho() and ViewPerspective(), you should already be familiar with them if you did the NeHe tutorials, they enable and disable orthographic rendering, very handy indeed.

4. Finally, render FBO#2 to the frame buffer using most of the same stuff besides the blending part that is, note that i also draw a cube instead and that the full screen render has no blue component, this is because i want the cube to be visible.

glViewport (0, 0, (GLsizei)(g_window->init.width), (GLsizei)(g_window->init.height));
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, o);
checkfbo();
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
ViewOrtho(512,512);

glBindTexture(GL_TEXTURE_2D, color_tex2);

glBegin(GL_QUADS);
glColor4f(1.0f, 1.0f, 0.0f, 0.1f);
glTexCoord2f(0,1);
glVertex3f(0,0,0);
glTexCoord2f(0,0);
glVertex3f(0,512,0);
glTexCoord2f(1,0);
glVertex3f(512,512,0);
glTexCoord2f(1,1);
glVertex3f(512,0,0);
glEnd();

glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);

glLoadIdentity ();
glTranslatef (6.0f, -4.0f, -16.0f);
glRotatef(angle,0.0f,1.0f,0.0f);
glRotatef(angle,1.0f,0.0f,0.0f);
glRotatef(angle,0.0f,0.0f,1.0f);
glColor3f(1,1,1);
drawBox();

ViewPerspective();
glDisable(GL_TEXTURE_2D);

There are many other uses for FBO buffers, using them for post processing or advanced blending is not unheard of, multi pass rendering likes them and for HDR it’s almost must.
The double FBO method i use here is good way of doing things, first render to buffer 1 and then post process them in the second one, it’s fairly fast and leaves few artifacts, but this is only if your GPU can take it that is, there are a few new extensions that deals directly with transferring and blending data between two FBOs, but i won’t include them here, not until we get gl 2.1 where these and the fbo extension is merged.
Sit tight, because the next one is about GLSL shaders, and as always check the source for more info and comments on the code.
Download this tutorial for MSVCPP 6.0


2 Comments

  • By Peter Wallström, May 4, 2010 @ 16:59

    -old comments-

    Comment by overlord on 2006:05:05 18:09
    Just a quick note, since this tutorial uses GL_RGBA16F_ARB it may run slow on older hardware, if it does then try changing it to GL_RGBA.

    Comment by Desert on 2009:12:10 22:11
    Hi,

    Some time ago i wondered how to emulate the accumulation buffer using FBO, now i see how.

    Sadly the code does not port to Linux as smooth as i would like.

    But thanks a lot for the hints!.

  • By Franticfreddy, March 18, 2011 @ 15:18

    thanks it help me a lot !

Other Links to this Post

RSS feed for comments on this post. TrackBack URI

Leave a comment

*


WordPress Themes