Skip to content

Instantly share code, notes, and snippets.

@kovrov
Created May 12, 2011 00:46
Show Gist options
  • Save kovrov/967714 to your computer and use it in GitHub Desktop.
Save kovrov/967714 to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment