Skip to content

Instantly share code, notes, and snippets.

@jzrake
Created May 1, 2012 18:10
Show Gist options
  • Save jzrake/2570161 to your computer and use it in GitHub Desktop.
Save jzrake/2570161 to your computer and use it in GitHub Desktop.
PPM screenshot with GLUT
void TakeScreenshot(const char *fname)
{
printf("writing a screenshot to %s\n", fname);
int dimx = glutGet(GLUT_WINDOW_WIDTH);
int dimy = glutGet(GLUT_WINDOW_HEIGHT);
size_t imsize = 3*dimx*dimy;
char *pixels = (char*) malloc(imsize*sizeof(char));
glReadPixels(0, 0, dimx, dimy, GL_RGB, GL_UNSIGNED_BYTE, pixels);
FILE *fp = fopen(fname, "wb");
fprintf(fp, "P6\n%d %d\n255\n", dimx, dimy);
fwrite(pixels, sizeof(char), imsize, fp);
fclose(fp);
free(pixels);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment