]> git.mxchange.org Git - simgear.git/blob - simgear/screen/screen-dump.cxx
Add a bunch of extensions in preparation of render-to-texture support.
[simgear.git] / simgear / screen / screen-dump.cxx
1 // screen-dump.cxx -- dump a copy of the opengl screen buffer to a file
2 //
3 // Contributed by Richard Kaszeta <bofh@me.umn.edu>, started October 1999.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the
17 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 // Boston, MA  02111-1307, USA.
19 //
20 // $Id$
21
22
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #if defined(__CYGWIN__)  /* && !defined(USING_X) */
28 #define WIN32
29 #endif
30
31 #if defined(WIN32)  /* MINGW and MSC predefine WIN32 */
32 # include <windows.h>
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <limits.h>
38
39 #include <simgear/compiler.h>
40
41 #include SG_GL_H
42
43 #include "screen-dump.hxx"
44
45
46 #define RGB3 3                  // 3 bytes of color info per pixel
47 #define RGBA 4                  // 4 bytes of color+alpha info
48
49 bool sg_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
50 {
51     int i, j, k, q;
52     unsigned char *ibuffer;
53     FILE *fp;
54     int pixelSize = mode==GL_RGBA?4:3;
55
56     ibuffer = (unsigned char *) malloc(win_width*win_height*RGB3);
57
58     if ( (fp = fopen(filename, "wb")) == NULL ) {
59         printf("Warning: cannot open %s\n", filename);
60         return false;
61     }
62
63     fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
64             win_width, win_height, UCHAR_MAX);
65     q = 0;
66     for (i = 0; i < win_height; i++)
67         for (j = 0; j < win_width; j++)
68             for (k = 0; k < RGB3; k++)
69                 ibuffer[q++] = (unsigned char)
70                     *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
71     fwrite(ibuffer, sizeof(unsigned char), RGB3*win_width*win_height, fp);
72     fclose(fp);
73     free(ibuffer);
74
75     printf("wrote file (%d x %d pixels, %d bytes)\n",
76            win_width, win_height, RGB3*win_width*win_height);
77     return true;
78 }
79
80
81 // dump the screen buffer to a ppm file
82 bool sg_glDumpWindow(const char *filename, int win_width, int win_height) {
83     GLubyte *buffer;
84     bool result;
85
86     buffer = (GLubyte *) malloc(win_width*win_height*RGBA);
87
88     // read window contents from color buffer with glReadPixels
89     glFinish();
90     glReadPixels(0, 0, win_width, win_height, 
91                  GL_RGBA, GL_UNSIGNED_BYTE, buffer);
92     result = sg_glWritePPMFile( filename, buffer, win_width, win_height,
93                                 GL_RGBA );
94     free(buffer);
95
96     return result;
97 }
98