Getting screenshot of OpenGL output ...


I would like to get a simple screenshot of OpenGL output via glReadPixels and FreeImage.

The problem is that currently I get a messed up Black and White copy of what I am rendering. What am I doing wrong?

Consider the following example that I am using from here: c++ - How to take screenshot in OpenGL - Stack Overflow

And here is the code I am using from that example:

GLubyte* outPixels = new GLubyte[3 * CurrentWindowWidth * CurrentWindowHeight];

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glReadPixels(0, 0, CurrentWindowWidth, CurrentWindowHeight, GL_RGB, GL_UNSIGNED_BYTE, outPixels);

FIBITMAP* image = FreeImage_ConvertFromRawBits(outPixels, CurrentWindowWidth, CurrentWindowHeight, 3 * CurrentWindowWidth, 24, 0x0000FF, 0xFF0000, 0x00FF00, false);
FreeImage_Save(FIF_BMP, image, "C:/Users/Christopher/Desktop/test.bmp", 0);

// Free resources

And here is the result:

I’m not sure that it is the reason for the problem, but when asking OpenGL to write data to client memory (like glReadPixels does) the GL_PACK_* (not GL_UNPACK_*) pixel store settings are relevant.


i am pretty sure that you are reading to few channels …
baybe you final render img is in RGBA … but you are only copying data to RGB … that means all gets messd up by 1 byte … or you are using the wrong format for the color data …
if you past more of your code ( gl init part ) than its easyer to tell.


try a simple image of 3 spheres or even triangles one is red and one is green and one is blue and see how the colors get mapped when you save the scene… I had to use BGR instead of RGB in glReadPixels() for which you have to include glext.h