PDA

View Full Version : Problem with glReadPixels



Amx90
06-30-2009, 10: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] ;
glReadBuffer(GL_BACK);
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;

}

ZbuffeR
07-01-2009, 12:54 AM
"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)

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

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

http://img22.imageshack.us/img22/6624/picture1tqc.png

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

http://img139.imageshack.us/img139/1851/picture2ora.png

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

_NK47
07-02-2009, 03: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.

dletozeun
07-02-2009, 07: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 );