Screenshots

Hi, here is a little Yule gift for all you GL junkies out there, it’s a short quick and easy tool for taking a screen shot into the tga format, sure it’s uncompressed and doesn’t increment the file name, but it’s pretty slick.


void screenshot (char filename[160],int x, int y)
{

// get the image data
long imageSize = x * y * 3;
unsigned char *data = new unsigned char[imageSize];
glReadPixels(0,0,x,y, GL_BGR,GL_UNSIGNED_BYTE,data);

// split x and y sizes into bytes
int xa= x % 256;
int xb= (x-xa)/256;

int ya= y % 256;
int yb= (y-ya)/256;

//assemble the header
unsigned char header[18]={0,0,2,0,0,0,0,0,0,0,0,0,(char)xa,(char)xb,(char)ya,(char)yb,24,0};

// write header and data to file
fstream File(filename, ios::out | ios::binary);
File.write (reinterpret_cast(header), sizeof (char)*18);
File.write (reinterpret_cast(data), sizeof (char)*imageSize);
File.close();

delete[] data;
data=NULL;
}

Usage: screenshot (“test.tga”,800,600);
Just call the function with the correct window size just before SwapBuffers and it will take a screen shot of everything in the frame buffer, easy.

3 Comments

  • By yifan, December 10, 2011 @ 06:52

    very nice function. I use it to save my ray tracing picture.

  • By Peter Wallström, December 10, 2011 @ 12:47

    Tanks, and pretty soon i will release an even better version in my toolkit that can deal with compression

  • By NaliPL, February 7, 2012 @ 01:15

    reinterpret_cast
    change to reinterpret_cast

    require include :
    #include
    #include

    if not using “using namespace std;

    Then place std before; std::fstream std::ios

Other Links to this Post

RSS feed for comments on this post. TrackBack URI

Leave a comment

*


WordPress Themes