[ODE] screenshots?

Steve Baker sjbaker1 at airmail.net
Fri Dec 5 14:11:16 MST 2003


Chris Calef wrote:
> does anyone know how to make openGL save a screenshot?
>  it would be very useful for debugging. thanks!!

Snipped out of my PLIB library:

Just call:

   ssgaScreenDump ( filename, xsize, ysize ) ;

(xsize and ysize are the size of the OpenGL window in pixels)

It writes out the file in uncompressed 'SGI' format (aka RGB format,
aka IRIX format, aka Haeberli format!)

---------------------------------------------------------------
static const int _ulEndianTest = 1;
#define ulIsBigEndian    (*((char *) &_ulEndianTest ) == 0)

static inline void _ulEndianSwap(unsigned int *x) {
   *x = (( *x >> 24 ) & 0x000000FF ) |
     (( *x >>  8 ) & 0x0000FF00 ) |
     (( *x <<  8 ) & 0x00FF0000 ) |
     (( *x << 24 ) & 0xFF000000 ) ;
}

static inline void _ulEndianSwap(unsigned short *x) {
   *x = (( *x >>  8 ) & 0x00FF ) |
     (( *x <<  8 ) & 0xFF00 ) ;
}

inline unsigned short ulEndianBig16(unsigned short x) {
   if (ulIsBigEndian) {
     return x;
   } else {
     _ulEndianSwap(&x);
     return x;
   }
}

inline unsigned int ulEndianBig32(unsigned int x) {
   if (ulIsBigEndian) {
     return x;
   } else {
     _ulEndianSwap(&x);
     return x;
   }
}


static void writeByte ( FILE *fd, unsigned char x )
{
   fwrite ( & x, sizeof(unsigned char), 1, fd ) ;
}


static void writeShort ( FILE *fd, unsigned short x )
{
   x = ulEndianBig16 ( x ) ;
   fwrite ( & x, sizeof(unsigned short), 1, fd ) ;
}


static void writeInt ( FILE *fd, unsigned int x )
{
   x = ulEndianBig32 ( x ) ;
   fwrite ( & x, sizeof(unsigned int), 1, fd ) ;
}

void ssgaScreenDump ( char *filename, int xsize, int ysize )
{
   FILE *fd = fopen ( filename, "wb" ) ;

   if ( fd == NULL )
   {
     fprintf ( stderr, "Failed to open '%s' for writing screendump.\n",
                        filename ) ;
     return ;
   }

   unsigned char *buffer = new unsigned char [ xsize * ysize * 3 ] ;
   unsigned char *row    = new unsigned char [ xsize ] ;

   glReadBuffer ( GL_FRONT ) ;
   glReadPixels( 0, 0, xsize, ysize, GL_RGB, GL_UNSIGNED_BYTE,
                                                        (void *) buffer ) ;
   glReadBuffer ( GL_BACK ) ;

   char  type  =   0 /* RGB_IMG_VERBATIM */ ;
   short dim   =   3 ;
   short zsize =   3 ;
   char  bpp   =   1 ;
   int   min   =   0 ;
   int   max   = 255 ;
   short magic = 0x01DA /* RGB_IMG_MAGIC */ ;
   int   colormap = 0 ;
   int   i ;

   writeShort ( fd, magic ) ;
   writeByte  ( fd, type  ) ;
   writeByte  ( fd, bpp   ) ;
   writeShort ( fd, dim   ) ;
   writeShort ( fd, xsize ) ;
   writeShort ( fd, ysize ) ;
   writeShort ( fd, zsize ) ;
   writeInt   ( fd, min   ) ;
   writeInt   ( fd, max   ) ;
   writeInt   ( fd, 0 ) ;  /* Dummy field */

   for ( i = 0 ; i < 80 ; i++ )
     writeByte ( fd, '\0' ) ;         /* Name field */

   writeInt ( fd, colormap ) ;

   for ( i = 0 ; i < 404 ; i++ )
     writeByte ( fd, 0 ) ;         /* Dummy field */

   for ( int z = 0 ; z < 3 ; z++ )
     for ( int y = 0 ; y < ysize ; y++ )
     {
       for ( i = 0 ; i < xsize ; i++ )
         row [ i ] = buffer [ ( y * xsize + i ) * 3 + z ] ;

       fseek ( fd, ( z * ysize + y ) * xsize + 512, SEEK_SET ) ;
       fwrite ( row, 1, xsize, fd ) ;
     }

   fclose ( fd ) ;

   delete row ;
   delete buffer ;
}

---------------------------- Steve Baker -------------------------
HomeEmail: <sjbaker1 at airmail.net>    WorkEmail: <sjbaker at link.com>
HomePage : http://www.sjbaker.org
Projects : http://plib.sf.net    http://tuxaqfh.sf.net
            http://tuxkart.sf.net http://prettypoly.sf.net
-----BEGIN GEEK CODE BLOCK-----
GCS d-- s:+ a+ C++++$ UL+++$ P--- L++++$ E--- W+++ N o+ K? w--- !O M-
V-- PS++ PE- Y-- PGP-- t+ 5 X R+++ tv b++ DI++ D G+ e++ h--(-) r+++ y++++
-----END GEEK CODE BLOCK-----




More information about the ODE mailing list