PDA

View Full Version : Getting screenshot of OpenGL output ...



tmason
12-18-2014, 01:21 AM
Hello,

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: http://stackoverflow.com/questions/5844858/how-to-take-screenshot-in-opengl

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
FreeImage_Unload(image);


And here is the result:

http://s2.postimg.org/hdyc771zt/test.png

carsten neumann
12-19-2014, 09:31 AM
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.

uwi2k2
12-20-2014, 03:54 AM
hi,

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.

cu
uwi2k2

sueyllam
12-20-2014, 07:33 PM
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