glReadPixels

I am having trouble reading the stencil bits. I am not sure where the problem is.
All I get is zeros.

glColorMask( false, false, false, false);
glEnable(GL_STENCIL_TEST);
glClear(GL_STENCIL_BUFFER_BIT);
glStencilFunc( GL_ALWAYS, 1, 1);
glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE);

PointList::iterator x;
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(1,0,0);
glBegin(GL_POLYGON);
for( x=region.begin(); x!=region.end(); x++)
glVertex3fv( x->dim );
glEnd();
glPopMatrix();
glDisable(GL_STENCIL_TEST);
int dims = widthheight4;
GLubyte * stencils = new GLubyte[ dims/4 ];
glReadPixels( x1, y1, width, height, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, stencils );

What is my mistake? Thanks.

[This message has been edited by maximian (edited 03-09-2004).]

  • Do you see something drawn at all if the color mask is true.
  • Make sure x1, y1 are correct.
  • Check the pixelformat with DescribePixelFormat or glGetIntegerv(GL_STENCIL_BITS, &i), if you actually have stencil bits allocated.
  • Try a 32 bits color depth if you didn’t get some.
  • Check the pixel pack alignment parameters. Default is 4.

[This message has been edited by Relic (edited 03-09-2004).]

i think u have to select the buffer before using Readpixels(). bcoz the default buffer settings for the buffer are not working for me. if u done so … r u able to see any out put? otherwise first check weather u r making current RC (usually will be over looked if rendering routine and glReadPixels routine r different) ; sorry i confused u! but i also got the same problem but it is solved now. and use this code for pixel 4 byte packing before read pixels

glFinish();
glPixelStorei(GL_PACK_ALIGNMENT,4);
glPixelStorei(GL_PACK_ROW_LENGTH,0);
glPixelStorei(GL_PACK_SKIP_ROWS,0);
glPixelStorei(GL_PACK_SKIP_PIXELS,0);

more over using that glFinish(); is important ! otherwise GL might not complete the drawing instructions before u use glReadPixels();

-indra

Thank you for your check list. Now everything works, ie stencil bits, correct drawing,

The problem is with glReadPixels and color buffer.

glClear( GL_DEPTH_BUFFER_BIT);//get rid of mask

glColorMask( true, true, true, true);
GenerateRegionSelection(selectionMode);
//SwapBuffers( hDC );

unsigned int id=0;
GLubyte * pixels = new GLubyte[ dims ];

glReadPixels( x1, y1, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels );

Using glColor4ub I have encoded information
with each triangle. How the glReadPixels with GL_RGBA always return 0 0 0 255. (r,g,b,a).

My encoding starts at 0 0 0 0, 0 0 0 1…
0 0 0 255, 0 0 1 0 and so on. I check the encoding and it is fine. However, when I read back I get always 0 0 0 255.

Thanks indra. I posted right as you were posting. I solved the previous problem, but now I seem to be unable to extract the information from the color buffer. Thanks to both of you.

Originally posted by kingindra:
i think u have to select the buffer before using Readpixels().

Not for the stencil or depth buffer, there’s only one each.

more over using that glFinish(); is important ! otherwise GL might not complete the drawing instructions before u use glReadPixels();

Sorry, this is not true. glReadPixels is synchronous.
If you have an implementation where this is not working, update your driver and/or report a bug to the vendor.

You should almost never need to call glFinish in a standard OpenGL program, except for benchmarking or synchronization with other graphics drawing.

Originally posted by maximian:
The problem is with glReadPixels and color buffer.

Using glColor4ub I have encoded information
with each triangle. How the glReadPixels with GL_RGBA always return 0 0 0 255. (r,g,b,a).

My encoding starts at 0 0 0 0, 0 0 0 1…
0 0 0 255, 0 0 1 0 and so on. I check the encoding and it is fine. However, when I read back I get always 0 0 0 255.
[/b]

Same checks as above plus some tips:

  • Do you have a destination alpha buffer in your pixelformat?
  • glGetIntegerv(GL_ALPHA_BITS, &a); What’s a?
    If there is no destination alpha, OpenGL returns 1.0 for the alpha which is 255 in unsigned byte format.
    You method will not work in in highcolor.
  • If you have encodings per triangle make sure you use flat shading and no lighting.
  • This time check glReadBuffer(), front or back?
  • Do not rely on the back buffer contents if you have called SwapBuffers. Pixelformats which support PFD_SWAP_EXCHANGE have undefined back buffer contents after.
  • Do not glReadPixels from areas which are overlapped by other windows. The result is undefined.

I feel so stupid. Yes alpha bits were off.

I misunderstood the pfd, so I set 32 for color bits, thinking I was allocating for alpha also.

Thank you very much to all.

Now I will go to the back of the room and suck on my thumb.