View Full Version : Faster way to read pixels

Darren Taylor
05-11-2007, 03:49 AM
Hi again,

Part of my modelling program requires me to find out which vertexes are actually visible on screen, and which ones are backfacing/behind the model.

The best way I can see to do this is by running a quick scan over the back buffer, using glReadPixels to test each pixel. If a vertex is found, it's obviously drawn in front of the model and visible to the viewer.

However, I've found that using glReadPixels in this way is VERY slow. Even if I use a simple loop to read each pixel on a 640*480 window, it takes TEN seconds to complete the scan!! :eek:

I know glReadPixels has always been a slow function call, but surely it shouldn't take this long?

If so, is there another way of simply reading the color of pixels in the frame buffer? Or maybe by rendering to a texture and testing that instead - would that make it any easier?

Many thanks,


05-11-2007, 04:13 AM
You could just read the whole screen with a single glReadPixels call, and then iterate over the array. This will be a lot faster than reading every single pixel.

But it would be even faster if you just calculate everything on the CPU (or use transform feedback on modern cards). You could also use occlusion query.

Darren Taylor
05-11-2007, 05:01 AM
Oh, cool. I completely forgot you can read large areas with glReadPixels, not just single pixels!!

It's infinitely faster. Thanks!

05-16-2007, 06:47 AM
Darren: Search for oclusion query. Nearly non cost operation.

09-04-2007, 03:56 AM
Use Hardware Occlution Query, render bounding boxes of your mesh, GPU will return how many fragments will pass the test

Nicolai de Haan Brøgger
09-04-2007, 06:25 AM
The article in GPU Gems 2, chapter 6 presents a useful technique for Hardware Occlusion Query "Hardware occlusion Queries made Useful".

kind regards,
Nicolai de Haan Brøgger

09-07-2007, 01:06 PM
Hi Darren,

As others have suggested use Occlusion query if you want pixel level information (Spec: http://oss.sgi.com/projects/ogl-sample/registry/ARB/occlusion_query.txt).

You may also want to look into other object level visibility tests - painters algorithm or bound box based etc. As you mentioned that you are doing modelling work, where it might be easier to identify visibility for parts of model very cost effectively.