opengl - Taking picture of screen with glut and open gl and outputting to tga (SB5 issue) -


i reading opengl superbible. , encountering issue of source code shown in book. code in particular code meant take screenshot of current scene. code provided:

glint gltwritetga(const char *szfilename)   {   file *pfile;                // file pointer   tgaheader tgaheader;        // tga file header   unsigned long limagesize;   // size in bytes of image   glbyte  *pbits = null;      // pointer bits   glint iviewport[4];         // viewport in pixels   glenum lastbuffer;          // storage current read buffer setting    // viewport dimensions   glgetintegerv(gl_viewport, iviewport);    // how big image going (targas tightly packed)   limagesize = iviewport[2] * 3 * iviewport[3];      // allocate block. if doesn't work, go home   pbits = new glbyte[limagesize]; if(pbits == null)       return 0;   // read bits color buffer   glpixelstorei(gl_pack_alignment, 1);   glpixelstorei(gl_pack_row_length, 0);   glpixelstorei(gl_pack_skip_rows, 0);   glpixelstorei(gl_pack_skip_pixels, 0);  // current read buffer setting , save it. switch   // front buffer , read operation. finally, restore   // read buffer state    glgetintegerv(gl_read_buffer, &lastbuffer);   glreadbuffer(gl_front);   glreadpixels(0, 0, iviewport[2], iviewport[3], gl_bgr_ext, gl_unsigned_byte, pbits);   glreadbuffer(lastbuffer);      // initialize targa header   tgaheader.identsize = 0;   tgaheader.colormaptype = 0;   tgaheader.imagetype = 2;   tgaheader.colormapstart = 0;   tgaheader.colormaplength = 0;   tgaheader.colormapbits = 0;   tgaheader.xstart = 0;   tgaheader.ystart = 0;   tgaheader.width = iviewport[2];   tgaheader.height = iviewport[3];   tgaheader.bits = 24;   tgaheader.descriptor = 0;    // attempt open file   pfile = fopen(szfilename, "wb");   if(pfile == null)       {       free(pbits);    // free buffer , return error       return 0;       }    // write header   fwrite(&tgaheader, sizeof(tgaheader), 1, pfile);    // write image data   fwrite(pbits, limagesize, 1, pfile);    // free temporary buffer , close file   free(pbits);       fclose(pfile);    // success!   return 1;   }   

the issues code spews out file of correct size, containing kind of junk data. not produce openable .tga file. issue fact "glgetintegerv" function takes glint, source code above provides glenum.

when tried tga_header found on internet failed first, because colormapstart , colormaplength bytes when should shorts. here's modifies code should work:

struct tga_header {     glbyte  identsize;     glbyte  colormaptype;     glbyte  imagetype;     // color map specifications     glshort colormapstart;     glshort colormaplength;     glbyte colormapbits;      //image specification     glshort xstart;     glshort ystart;     glshort width;     glshort height;     glbyte  bits;     glbyte descriptor; }__attribute__((packed));  glint gltwritetga(const char *szfilename) {     file *pfile;                // file pointer     struct tga_header tgaheader;        // tga file header     unsigned long limagesize;   // size in bytes of image     glint iviewport[4];         // viewport in pixels     glint lastbuffer;          // storage current read buffer setting      // viewport dimensions     glgetintegerv(gl_viewport, iviewport);      // how big image going (targas tightly packed)     limagesize = iviewport[2] * 3 * iviewport[3];     // allocate block. if doesn't work, go home     glbyte  pbits[limagesize];     if(pbits == null)         return 0;     // read bits color buffer     glpixelstorei(gl_pack_alignment, 1);     glpixelstorei(gl_pack_row_length, 0);     glpixelstorei(gl_pack_skip_rows, 0);     glpixelstorei(gl_pack_skip_pixels, 0);      // current read buffer setting , save it. switch     // front buffer , read operation. finally, restore     // read buffer state     glgetintegerv(gl_read_buffer, &lastbuffer);     glreadbuffer(gl_front);     glreadpixels(0, 0, iviewport[2], iviewport[3], gl_bgr_ext, gl_unsigned_byte, pbits);     glreadbuffer(lastbuffer);      // initialize targa header     tgaheader.identsize = 0;     tgaheader.colormaptype = 0;     tgaheader.imagetype = 2;     tgaheader.colormapstart = 0;     tgaheader.colormaplength = 0;     tgaheader.colormapbits = 0;     tgaheader.xstart = 0;     tgaheader.ystart = 0;     tgaheader.width = iviewport[2];     tgaheader.height = iviewport[3];     tgaheader.bits = 24;     tgaheader.descriptor = 0;      // attempt open file     pfile = fopen(szfilename, "wb");     if(pfile == null)         {         return 0;         }      // write header       fwrite(&tgaheader, sizeof(tgaheader), 1, pfile);      // write image data     fwrite(pbits, limagesize, 1, pfile);      fclose(pfile);      // success!     return 1; } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -