PDA

View Full Version : glReadPixels



maximian
03-09-2004, 07:12 AM
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 = width*height*4;
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).]

Relic
03-09-2004, 07:26 AM
- 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).]

03-09-2004, 11:27 AM
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

maximian
03-09-2004, 11:27 AM
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.

maximian
03-09-2004, 11:30 AM
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.

Relic
03-09-2004, 09:56 PM
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.

Relic
03-09-2004, 10:04 PM
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.

maximian
03-10-2004, 07:16 AM
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.