PDA

View Full Version : MSAA with FBO



Mukund
09-28-2011, 01:45 AM
Hello Everyone,

I am trying MSAA with FBOs. I use two FBOs. One that reads from the other FBO. I am following the ideas presented here:

LINK (http://www.stefanlienhard.ch/2008/08/27/gl_ext_framebuffer_object-with-multisampling/#more-22)

And this is where i do the drawing:



// mfbo: MSAA FBO
// fbo: Normal FBO

void drawFBO()
{

glBindFramebuffer(GL_FRAMEBUFFER, mfbo);
glPushAttrib(GL_VIEWPORT_BIT);

// Draw scene.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

drawScene();

glPopAttrib();
glBindFramebuffer(GL_READ_FRAMEBUFFER, mfbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_LINEAR);

// ############## Draw to screen #########################
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glGenerateMipmap(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


// Draw Some scene.( i am drawing a quad)

}

Now, what i do not understand is, this does not work if do not give :


glGenerateMipmap(GL_TEXTURE_2D);

Why do we need to generate Mipmaps? Can't we do a render to texture without it? When i do texture mapping, i use glTexImage2D() and give the texel data and it works. So, in a render to texture, i thought that the texel data would be obtained from the texture attachment. But, it doesn't seem to work until i generate Mipmaps.

Can anyone please explain where i am going wrong?

ZbuffeR
09-28-2011, 03:18 AM
It depends if you define the texture filtering as being mipmaped or not.
If you don't need it, use GL_LINEAR as filtering for both mag and min.

Mukund
09-28-2011, 03:41 AM
Thanks Zbuffer,

Ah, yeah got it. Now, i changed the filter to GL_LINEAR and removed the glGenerateMipmap().


glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


IMAGE (http://imageshack.us/photo/my-images/204/unledmhx.png/)

But now, i am getting the following output: (A black teapot).
Can you please tell me what i'm missing?
Thanks.

Dark Photon
09-28-2011, 04:26 AM
But now, i am getting the following output: (A black teapot).
Your basic Blit and Blit setup looks fine.
- Check for GL errors (link (http://www.opengl.org/wiki/FAQ#glGetError_.28or_.22How_do_I_check_for_GL_erro rs.3F.29))
- Try changing glClearColor to red before your glClear, comment out drawScene(), and see what you get in the end.
- glReadPixels a pixel from the fbo COLOR attachment texture and see what color is in the texture.
- Kick the tires.

All else fails, just post more code, like your FBO creation and setup, and the code to render your quad. I suspect you've just got some state set up wrong like the viewport, modelview, or projection transforms such that your quad isn't showing up.

Mukund
09-28-2011, 08:02 AM
Thanks for the reply Dark Photon.

>> Check for GL errors
Well, i am not getting any error reported.

>> Try changing glClearColor to red before your glClear, comment out drawScene(), and see what you get in the end.

i did that, and i am getting this:

PIC (http://imageshack.us/photo/my-images/90/unledweq.png/)

>> glReadPixels a pixel from the fbo COLOR attachment texture and see what color is in the texture

That seems to report an error: error 1282.

Here is how i set up my FBOs:


void createFBO()
{
// multi sampled color buffer
glGenRenderbuffers(1, &colorBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorBuffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4 /* 4 samples */, GL_RGBA8, width, height);

// multi sampled depth buffer
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4 /* 4 samples */, GL_DEPTH_COMPONENT, width, height);

// create fbo for multi sampled content and attach depth and color buffers to it
glGenFramebuffers(1, &mfbo);
glBindFramebuffer(GL_FRAMEBUFFER, mfbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);

// create texture
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );


// create final fbo and attach texture to it
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture1, 0);


}

Mukund
09-29-2011, 08:50 AM
Hi,

It was just a problem with enabling and disabling textures. Its solved now.

Thanks.