View Full Version : Problem with glReadPixels

06-30-2009, 09:30 PM
Hi guys

I trying to take a snapshot from the scene(which is basically a teapot) and then store it in a file on this format:
x y r g b

The program reads the pixels but the resulting picture is not coming right.

Do I have to call any functions before the glReadPixels function?
Also, If i want to take a picture of the whole scene which hight and width should I pass to the glReadPixels function: the viewport or the window ?

int WIDTH=512;
int HEIGHT=512;
float imageData[WIDTH*HEIGHT*3] ;
glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_FLOAT, imageData);
int pixelNb = WIDTH*HEIGHT;
for( int i = 0; (i< pixelNb) &amp;&amp; readFrame; ++i )
if(imageData[ i ]!=1)
myfile << i%HEIGHT <<"\t"<<i/HEIGHT<< "\t"<< int(imageData[ i ] *255 )<< "\t"<< int(imageData[ i + 1 ]*255 )<< "\t"<<int(imageData[ i + 2 ]*255) << endl;


06-30-2009, 11:54 PM
"picture is not coming right"
Is it black ?
random noise ?
mixed colors from the teapot ?
... something else ?

width,height should be the viewport size.

RGB is not 4 byte aligned, look for pitfall "7. Watch Your Pixel Store Alignment" here :
(that page is old, but parts are still true)

07-01-2009, 10:45 AM
the image comes as parallel lines in a shape of a circle.

07-01-2009, 08:44 PM
This is the actual picture


This is the picture produced by the text file resulting from the program


Ilian Dinev
07-01-2009, 09:45 PM
It could also be a problem with your non-OpenGL textfile-to-image decoder.

07-02-2009, 02:19 AM
yes, that's some weird image output.
check the image debugger http://billbaxter.com/projects/imdebug/
make sure your image is at least copied correct to system memory.

07-02-2009, 06:27 AM
Call glFinish() before glReadPixels. Be sure that packing alignment is correct (As Zbuffer said). In most cases, byte row alignment is used whereas the default one is word alignment:

glPixelStorei( GL_PACK_ALIGNMENT, 1 );