Jump to content
  • Advertisement
Sign in to follow this  
flyjason

OpenGL Capture & Save a rendered OpenGL scene

This topic is 4155 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I want to know how to capture and save a rendered OpenGL scene in JPEG/GIF format. I mean after drawing some objects (3D models) in an OpenGL window, I need to save that image in JPEG/GIF format. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
easy option: press the print screen button on your keyboard then open up photoshop or MS paint or whatever and paste it in. This should definitely work in windowed mode and I *think* will work in fullscreen.

other option:
read the data from the framebuffer in openGL and save it as a jpeg (you'll need to either find an image library or learn the file formats from wotsit.org

-me

Share this post


Link to post
Share on other sites
Agree with Palidine,

Unless you need this for some other reason than just a simple screen shot.

But I would hit ALT & PRINT SCREEN.

This will capture only your ACTIVE window.

Which means you wont take a screenshot of your whole desktop, and have to edit it in your paint package to cut the image you need.

Share this post


Link to post
Share on other sites
Take a look at the DevIL image library. It has functions to save images in a variety of formats. What you'll probably need to do is get a copy of the pixels in the frame buffer and pass that data to the appropriate function that will save the image in the format you want. Hope that helps.

Jon

Share this post


Link to post
Share on other sites
I use the following code to save a screenshot out in TIFF format (using the libtiff library, available at www.libtiff.org)


int SaveScreenshot(char *filename, char *description, int x, int y, int width, int height)
{
TIFF *file;
int i;
GLubyte *image, *p;

file = TIFFOpen(filename, "w");
if (file == NULL) {
return 1;
}

/* OpenGL's default 4 byte pack alignment would leave extra bytes at the
end of each image row so that each full row contained a number of bytes
divisible by 4. Ie, an RGB row with 3 pixels and 8-bit componets would
be laid out like "RGBRGBRGBxxx" where the last three "xxx" bytes exist
just to pad the row out to 12 bytes (12 is divisible by 4). To make sure
the rows are packed as tight as possible (no row padding), set the pack
alignment to 1. */


image = (GLubyte *) malloc(width * height * sizeof(GLubyte) * 3);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, image);

TIFFSetField(file, TIFFTAG_IMAGEWIDTH, (uint32) width);
TIFFSetField(file, TIFFTAG_IMAGELENGTH, (uint32) height);
TIFFSetField(file, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(file, TIFFTAG_COMPRESSION, 0);
TIFFSetField(file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(file, TIFFTAG_SAMPLESPERPIXEL, 3);
TIFFSetField(file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(file, TIFFTAG_ROWSPERSTRIP, 1);
TIFFSetField(file, TIFFTAG_IMAGEDESCRIPTION, description);

p = image;
for (i = height - 1; i >= 0; i--) {
if (TIFFWriteScanline(file, p, i, 0) < 0) {
free(image);
TIFFClose(file);
return 1;
}
p += width * sizeof(GLubyte) * 3;
}
TIFFClose(file);
return 0;
}



As you can see there are only four lines that are OpenGL specific, and one of them is a variable declaration. Shouldn't be too hard to convert to another file format.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!