read stencil buffer


I try to read the values of the stencil buffer for all pixels of the viewport, so I do:

unsigned char * data = new unsigned char[w*h];

Where w and h are the width and heigth of the viewport.

However, I have a segmentation fault when calling glReadPixels. Why?

unsigned char * data = new unsigned char[screenw*screenh];
delete [] data;

this doesn’t crash in my case. so check your allocation and deallocation, check “w” and “h” values twice, if it’s ok - something is wrong with surrounding code(and you, probably, should submit some to get help).

It’s very weird, it seems that when I call glReadPixels, it flushes all variables.

So before calling the function, sizeof(data)>0 but after, sizeof(data) is 0…

You might need some padding for the width, or adjust the packing with glPixelStorei. Data returned by glReadPixels isn’t byte-packed by default.

Read item 7, “Watch Your Pixel Store Alignment” here.

And as usual when a question about stencil not working is asked, we should always ask: did you ensure you created a Window which has stencil capabilities ?