Capturing the OpenGL output to a image file

Can anyone give me an idea to convert OpenGL output to an image file of some format.

I can read the OpenGL output through glReadPixel() and store into a buffer but how can i write into a image file format.

Originally posted by kedar:
[b]Can anyone give me an idea to convert OpenGL output to an image file of some format.

I can read the OpenGL output through glReadPixel() and store into a buffer but how can i write into a image file format.[/b]
Use libpng, libjpg, libtif, etc. There are plenty of those libraries and they are installed on most Linux systems. It wouldn’t very hard follow its documentation and they’ll save you from many troubles. You can also use SDL_Image if you are using SDL in order to save the buffer directly from .jpg or .png files.

You can also read the documentation about those files in a place like www.wotsit.org and do it for yourself :wink: but I recommend using libraries.

quite simple. the following code creates an uncompressed, true-color tga file. you have to pass the desired file name and the viewport width/height.

void Screendump(char *tga_file, short W, short H) {
 FILE   *out = fopen(tga_file, "w");
 char   pixel_data[3*W*H];
 short  TGAhead[] = {0, 2, 0, 0, 0, 0, W, H, 24};

 glReadBuffer(GL_FRONT);
 glReadPixels(0, 0, W, H, GL_BGR, GL_UNSIGNED_BYTE, pixel_data);
 fwrite(&TGAhead, sizeof(TGAhead), 1, out);
 fwrite(pixel_data, 3*W*H, 1, out);
 fclose(out); }

Wow, thank you for the targa function! I was just about to ask this same question myself.

Thanks a lot!

Thanks AZDO and RigidBody, I will try those stuff and get back to you if i need any help regarding the same.

Do we have any function to screendump that into a JPG file.

you could just use the ‘convert’ tool to convert the tga to jpg.
it comes with image magick, which is probably included in your distribution.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.