Sign in to follow this  
manton

screen shot using glReadPixels produces garbled image

Recommended Posts

I am having some with screen shots. The result I get looks like this: http://img147.imageshack.us/img147/4997/imagexx7.png The screen image is fine at run time and the objects are drawn with display lists - I mention this as the output is fine with only the skybox which at this stage is drawn in immediate mode. The code that produced this soup is:
GLubyte* image;
image = new GLubyte[display_width * display_height * 3];
glReadBuffer(GL_BACK);
glReadPixels(0,0, display_width, display_height, GL_BGR, GL_UNSIGNED_BYTE, image);

FILE   *out = fopen((image_dir+screen_shot_name + ".tga").c_str(), "w");
short  TGAhead[] = {0, 2, 0, 0, 0, 0, display_width, display_height, 24};
fwrite(&TGAhead, sizeof(TGAhead), 1, out);
fwrite(image, 3*display_width*display_height, 1, out);


All suggestions appreciated.

Share this post


Link to post
Share on other sites
Since you're reading from the back buffer, are you calling this code BEFORE swapping buffers? After a buffer swap, the content of the back buffer is undefined, and you should then read from the rfont buffer.

Share this post


Link to post
Share on other sites
Then I think you have to show more code. Can you produce a complete, MINIMAL, compilable and runnable example demonstrating the error? Remove anything that isn't relevant to the problem. Reduce the object to a single tringle, remove user interaction, don't use textures, anything as long as the problem is still there. Anything to reduce the amount of code.

Share this post


Link to post
Share on other sites
Hmmm, I agree but it would be difficult to get the code down to be small enough to post as it would at least include SDL and the .obj loader that I have written.

I might play around with this a bit before trying to strip it down to that point as there are about 25 translation units...

Share this post


Link to post
Share on other sites
Did you try to read the backbuffer before swapping? It should work.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1) is needed if you will use BGR

or don't call glPixelStorei and just use BGRA

What's you video card + drivers?

Share this post


Link to post
Share on other sites
Quote:
Original post by manton
Hmmm, I agree but it would be difficult to get the code down to be small enough to post as it would at least include SDL and the .obj loader that I have written.

Why would you have to include the entire .obj loader? As I said, replace the object with a single triangle or something. That's, like, 5 small lines of code only. Handcoded coordinates; no need for dynamic loading of complex shapes in complex file formats. If the problem is still there, it doesn't matter what you draw.

SDL isn't so much code. Just open a window, draw a triangle, make the screen dump and exit the program.

Share this post


Link to post
Share on other sites
Did you intend to write as binary instead? Opening a .tga file in notepad, it looks like it is in the binary format.

Try changing
FILE *out = fopen((image_dir+screen_shot_name + ".tga").c_str(), "w");
to
FILE *out = fopen((image_dir+screen_shot_name + ".tga").c_str(), "wb");

Can you also show us a picture of what its supposed to look like? Just use print screen instead.

[Edited by - ScottC on September 27, 2006 9:18:50 PM]

Share this post


Link to post
Share on other sites
Thanks all! I am happy to say that the problem is solved.

ScottC pointed out that I was writting to the file in the default open mode, which in my case was not binary.

For you enjoyment this is what a screen shot through the app now looks like



thanks again to all :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this