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;
}
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: