Problem with glReadPixels

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] ; 
	glReadBuffer(GL_BACK);
	glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_FLOAT, imageData);
	int pixelNb = WIDTH*HEIGHT;
	for( int i = 0; (i< pixelNb) && readFrame; ++i )
	{
		if(imageData[ i ]!=1)
		myfile  << i%HEIGHT <<"	"<<i/HEIGHT<< "	"<< int(imageData[ i ] *255 )<< "	"<< int(imageData[ i + 1 ]*255 )<< "	"<<int(imageData[ i + 2 ]*255) << endl;
	
	}

“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 :
http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/
(that page is old, but parts are still true)

the image comes as parallel lines in a shape of a circle.

This is the actual picture

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

It could also be a problem with your non-OpenGL textfile-to-image decoder.

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.

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 );