BOOL screenshot (const char* filename)
{
Glenum Lastbuffer;
glbyte* pbits = 0; Image data
unsigned long limagesize;
Glint Iviewport[4]; View Size
Glgetintegerv (Gl_viewport, Iviewport);
Limagesize = iviewport[2] * iviewport[3] * 3;
Pbits = (glbyte*) new unsigned char[limagesize];
if (!pbits)
return false;
Reading data from the 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);
//
Glgetintegerv (Gl_read_buffer, (glint*) &lastbuffer);
Glreadbuffer (Gl_front);
Glreadpixels (0, 0, iviewport[2], iviewport[3], Gl_bgr_ext, Gl_unsigned_byte, pbits);
Glreadbuffer (Lastbuffer);
if (writebmp (filename, (unsigned char*) pbits, iviewport[2], iviewport[3]))
return true;
return false;
}
BOOL Writebmp (const char filename[], unsigned char* data, unsigned int w, unsigned int h)
{
Std::ofstream Out_file;
/** Check Data */
if (!data)
{
Std::cerr << "Data corrupted!" << Std::endl;
Out_file.close ();
return false;
}
/** Create bitmap file information and bitmap file header structure */
Bitmapfileheader header;
Bitmapinfoheader Bitmapinfoheader;
unsigned char texturecolors = 0;/**< is used to transform the image color from BGR to RGB */
/** Open the file and check for errors */
Out_file.open (filename, std::ios::out | std::ios::binary);
if (!out_file)
{
Std::cerr << "Unable to open file" << filename << Std::endl;
return false;
}
/** Filling Bitmapfileheader */
Header.bftype = bitmap_id;
Header.bfsize = w*h*3 + sizeof (bitmapfileheader) + sizeof (bitmapinfoheader);
header.bfreserved1 = 0;
Header.bfreserved2 = 0;
header.bfoffbits = sizeof (Bitmapfileheader) + sizeof (bitmapinfoheader);
/** Writing bitmap file header information */
Out_file.write (char*) &header, sizeof (Bitmapfileheader));
/** Filling Bitmapinfoheader */
bitmapinfoheader.bisize = sizeof (Bitmapinfoheader);
Bitmapinfoheader.biwidth = W;
Bitmapinfoheader.biheight = h;
Bitmapinfoheader.biplanes = 1;
Bitmapinfoheader.bibitcount = 24;
Bitmapinfoheader.bicompression = Bi_rgb; Bi_rle4 Bi_rle8
Bitmapinfoheader.bisizeimage = w * H * 3; When the compression type is BI_RGB, it can also be set to 0
Bitmapinfoheader.bixpelspermeter = 0;
Bitmapinfoheader.biypelspermeter = 0;
bitmapinfoheader.biclrused = 0;
bitmapinfoheader.biclrimportant = 0;
/** writing bitmap File information */
Out_file.write (char*) &bitmapinfoheader, sizeof (Bitmapinfoheader));
/** move the pointer to the beginning of the data */
OUT_FILE.SEEKP (Header.bfoffbits, Std::ios::beg);
/** Writing image Data */
Out_file.write ((char*) data, bitmapinfoheader.bisizeimage);
Out_file.close ();
return true;
}
opengl-Saving BMP Pictures