glReadPixels

Hello, I was wondering if glReadPixels works with an fbo.

In my program, during typical operation, the user clicks on the terrain background and I use gluUnProject in combination with glReadPixels to get 3d coordinates.

In a new situation, I draw the scene to an offscreen fbo with a color and depth texture rectangle attachment. I render the fbo as a texture with a polygon to the standard draw buffer. ( similar to the example with the fbo class from gpgpu.org ) Problem is, the depth does not copy … well its just the depth of the single polygon with the texture. Thus, glReadPixels is going to read the wrong depth. Now when the user clicks I get bad behavior. :frowning:

My motivating factor to draw into the fbo is to down sample from a larger buffer into a smaller buffer and get anti aliasing or maybe implement a Gaussian filter. I’d assume the size of the fbo should be 2x of the default draw buffer?

I suppose either I can copy this depth somehow or have glReadPixels work on the fbo’s depth texture?

Hi

To read the correct depth value, you can bind the fbo and do the glReadPixels for GL_DEPTH_COMPONENT at the scaled coordinates (2x, 2y).

Thanks for the tip. I got it to work with mouse clicking. :slight_smile:

Though, I am wondering what size of the fbo would be adequate for the down sampling method? I just chose 2x my window size. Unfortunately, the copy from the fbo really slows things down.

Initially, the rendered image had a chunky appearance. But then I realized I had filtering set to GL_NEAREST. Hmm, I use the same fbo mechanism for shadow mapping which needs GL_NEAREST because of ati/nvidia differences. Once I changed it to GL_LINEAR, I got a anti aliased picture. Turn off multisampling and the picture is still pretty good.