]> git.mxchange.org Git - simgear.git/blob - simgear/screen/screen-dump.cxx
Collapsed the init() method into the constructor.
[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 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.
9 //
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.
14 //
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.
18 //
19 // $Id$
20
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #ifdef HAVE_WINDOWS_H
27 #  include <windows.h>                     
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <limits.h>
33
34 #include <GL/glut.h>
35 #include <simgear/xgl/xgl.h>
36
37 #include "screen-dump.hxx"
38
39
40 #define RGB  3                  // 3 bytes of color info per pixel
41 #define RGBA 4                  // 4 bytes of color+alpha info
42
43 void my_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
44 {
45     int i, j, k, q;
46     unsigned char *ibuffer;
47     FILE *fp;
48     int pixelSize = mode==GL_RGBA?4:3;
49
50     ibuffer = (unsigned char *) malloc(win_width*win_height*RGB);
51
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);
55     q = 0;
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);
62     fclose(fp);
63     free(ibuffer);
64
65     printf("wrote file (%d x %d pixels, %d bytes)\n",
66            win_width, win_height, RGB*win_width*win_height);
67 }
68
69
70 // dump the screen buffer to a ppm file
71 void my_glDumpWindow(const char *filename, int win_width, int win_height) {
72     GLubyte *buffer;
73
74     buffer = (GLubyte *) malloc(win_width*win_height*RGBA);
75
76     // read window contents from color buffer with glReadPixels
77     glFinish();
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 );
81     free(buffer);
82 }
83