1 // screen-dump.cxx -- dump a copy of the opengl screen buffer to a file
3 // Contributed by Richard Kaszeta <bofh@me.umn.edu>, started October 1999.
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <simgear/xgl/xgl.h>
37 #include "screen-dump.hxx"
40 #define RGB 3 // 3 bytes of color info per pixel
41 #define RGBA 4 // 4 bytes of color+alpha info
43 void my_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
46 unsigned char *ibuffer;
48 int pixelSize = mode==GL_RGBA?4:3;
50 ibuffer = (unsigned char *) malloc(win_width*win_height*RGB);
52 fp = fopen(filename, "wb");
53 fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
54 win_width, win_height, UCHAR_MAX);
56 for (i = 0; i < win_height; i++)
57 for (j = 0; j < win_width; j++)
58 for (k = 0; k < RGB; k++)
59 ibuffer[q++] = (unsigned char)
60 *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
61 fwrite(ibuffer, sizeof(unsigned char), RGB*win_width*win_height, fp);
65 printf("wrote file (%d x %d pixels, %d bytes)\n",
66 win_width, win_height, RGB*win_width*win_height);
70 // dump the screen buffer to a ppm file
71 void my_glDumpWindow(const char *filename, int win_width, int win_height) {
74 buffer = (GLubyte *) malloc(win_width*win_height*RGBA);
76 // read window contents from color buffer with glReadPixels
78 glReadPixels(0, 0, win_width, win_height,
79 GL_RGBA, GL_UNSIGNED_BYTE, buffer);
80 my_glWritePPMFile( filename, buffer, win_width, win_height, GL_RGBA );