Stencil bitplanes/glReadPixels() - pixel per byte or pixel per bit ?


If I read an area of the stencil buffer back using glReadPixels(), and specify that I only wish to read back a single bitplane (and specify the right packing info), are the bits that come back packed into bytes:

  • 1 bit per pixel, ie. 8 pixels to a byte


  • 1 bit per byte, ie. 1 pixel to a byte



You specify the user side layout of the read back data by using the glReadPixels “type” parameter.
With format GL_STENCIL_INDEX and type GL_UNSIGNED_BYTE you would read the stencil data into 8 bits per pixel. Most OpenGL implementations offer 8 stencil bits per pixel, which matches nicely.
With GL_BITMAP you would get one bit per pixel tightly packed into bytes.

The masking you want doesn’t work for glReadPixels, the manual says the readback value is masked with the index mask matching the output size, 255 for unsigned byte, 1 for bitmaps.
Means only if your stencil values are only 0 or 1 you can pack them into 8 bits per byte using GL_BITMAP. (Not tried myself.)
A higher bit can not be extracted during glReadPixels this way, read GL_UNSIGNED_BYTE and do your own masking.

Thanks Relic!

So glReadPixels() can take GL_BITMAP as a param ?
I’ll investigate that.


Yippee, on paper that seems to do what I want.
Can’t believe I missed that in the Spec.
Thanks Relic.