glReadPixels corrupting the image?

Hi guys

I am trying to read the contents of an FBO, but the resulting image is corrupted (see the images).


img_data = (unsigned char*)malloc(width*height*3);
glReadPixels(0,0,width,height, GL_RGB, GL_UNSIGNED_BYTE,img_data);
FILE* f = fopen("tmp.raw","w+");
fwrite(img_data,(width)*(height)*3,1,f);
fclose(f);

Does anyone have a clue what is wrong?

Thanks,
Roy.

result

screen framebuffer

Use glPixelStore(GL_PACK_ALIGNMENT, 1); before calling glReadPixels.

i guess the problem is the FBO itself, since glReadPixels on the main framebuffer produced correct results.

using glPixelStore(GL_PACK_ALIGNMENT, 1); did not yield better or different results in any way…

another question, what is the best way to render offscreen scenes?
I tried to do it but it seems the scene is not rendering.
I tried once to initialize OpenGL using HWND - HDC & wglCurrent, and once initialize using GLUT

nothign works

the best way is actually FBO. :slight_smile: there are also pbuffers but they are platform dependend in implementation and somewhat painful to implement. Try some samples on your machine see how the work and how they do it.

I use FBO to render to texture and then glGetTexImage instead of glReadPixels. Works fine everywhere I tried.