glReadPixels -- Insane Problem

I have googled for hours, and read the forums for hours, and hacked my code for a couple of days now – to no avail. Info:

Goal: to calculate a triangle’s ‘face color’, by averaging the color of every pixel that is textured onto it.

I have tried two attempts, both failing, but I have a STRONG suspicion that glReadPixels is the culprit.

^-- code

Approach A)

Render the model’s skin to a quad stretching from 0,0 -> SKINWIDTH, SKINHEIGHT
Next, render a triangle onto this skin, using the texture coordinates of that face of the model, drawing only to STENCIL!
Then, scan every pixel on the entire screen, if stencil == 1, then read the color of that pixel and save it.

Problem: stencil never tests true, when reading pixels with glReadPixels
^-- code
Approach B)

Much like A, except only render the triangle derived from texture coordinates [not the large quad of the skin], and render it to color buffer not to stencil. The idea on this approach is that i can read every pixel on the entire screen, add up the color values (black = 0.0 x 3, so that will not throw anything off), and divide this by the number of pixels in the triangle.

Problem: I never hit anything except for 0 0 0 for color, from glReadPixels

***Before you think you might have an answer to this!!!

I have tried a few different data types for the last value of glReadPixels; but not much seems to really change… but maybe this is the issue?

I have tried doing the glRenderBuffer() or whatever, and siwtching from FRONT to BACK and all that. It just seemed to create a big mess [i got a lot of hits for FRONT when I moved my window around, because it was reading off of my desktop) i am using double buffering of course…

Yes, I have 8 bits of stencil available. I test when my program runs. I’m running in 24bit color…

Yes, I realize what I am doing is slow, but that does not really matter to me. This will be done upon compiling a model…

I’m running a gf4 ti on linux, while this could be a driver issue I highly doubt it

If anybody could please give me any suggestions or ideas to hack at this code with; or if anybody has another idea for how to achieve this same thing; I would really appreciate it.


Looks pretty correct except for one thing. You always call swapBuffers before glReadPixels. This might cause problems, since the contents of back buffer after swapBuffers are undefined. However, glReadPixels reads from back buffer, so it just might be that the image is not there anymore.


Tried switching that around, no change except that my buffer didn’t flip until after my lengthy check :stuck_out_tongue:

Incidentally, I changed my glClearColor to .133333333 [for blue] ; and upon running my app I get

getcolor[b] = 0.133333

which means, that at least I am grabbing some value; but it’s always the clear color… [all of my testing from this point out is probably going to be the non stencil version, seems like it ought be easier to fix]

Any more ideas from anyone?

But the image you expect still shows on the screen after you swapbuffers, right? Sounds like you’re reading from the wrong coords, but it doesn’t look like it. Have you tried reading the whole screen once? Propably doesn’t fix anything, but at least it should make it a bit faster.