#include #include #include #include #include #include #ifdef UNIX #include #include #endif #ifdef WIN32 #define M_PI 3.14159265 #include #endif #include #include #include "glut.h" void DumpAndWriteWholeWindow( char * ); unsigned char * DumpPixels( int, int, int, int ); void WritePpm( char *, unsigned char *, int, int ); /** ** dump and write the whole window: **/ void DumpAndWriteWholeWindow( char *filename ) { unsigned char *array; int xdim, ydim; xdim = glutGet( GLUT_WINDOW_WIDTH ); ydim = glutGet( GLUT_WINDOW_HEIGHT ); array = DumpPixels( 0, 0, xdim, ydim ); WritePpm( filename, array, xdim, ydim ); delete [] array; } /** ** Perform an OpenGL window dump: **/ unsigned char * DumpPixels( int xl, int yb, int xdim, int ydim ) { unsigned char *array; /* array to hold the RGB dump */ /* allocate the array to hold the RGB pixel values: */ array = new unsigned char[ 3*xdim*ydim ]; if( array == NULL ) { fprintf( stderr, "DumpPpm() cannot malloc the temporary array!\n" ); return NULL; } /* setup and do the dump: */ glPixelStorei( GL_PACK_ALIGNMENT, 1 ); glPixelStorei( GL_PACK_ROW_LENGTH, xdim ); glReadBuffer( GL_FRONT ); glReadPixels( xl, yb, xdim, ydim, GL_RGB, GL_UNSIGNED_BYTE, array ); return array; } /** ** Write a PPM image file: **/ void WritePpm( char *filename, unsigned char *array, int xdim, int ydim ) { FILE *fp; /* pointer to PPM file */ unsigned char *p; /* pointer into array[] */ int i, j; /* counters */ int ipix; /* hex pixel count */ /* open the file for this image: */ fp = fopen( filename, "w" ); if( fp == NULL) { fprintf( stderr, "DumpPpm() cannot create the PPM file '%s'\n", filename ); return; } /* write the PPM header: */ fprintf( fp, "P3\n%d %d\n255\n", xdim, ydim ); /* write the pixels: */ for( i = 0; i < ydim; i++ ) { fprintf( fp, "\n\n" ); p = &array[ 3*xdim*(ydim-1-i) ]; for( j = 0, ipix = 0; j < xdim; j++, p += 3 ) { fprintf( fp, "%03d %03d %03d ", (int)*(p+0), (int)*(p+1), (int)*(p+2) ); if( ++ipix >= 5 ) { putc( '\n', fp ); ipix = 0; } } } /* end game: */ fflush( fp ); fclose( fp ); }