Skip to main content
GameDev.net gamedev.net
🔒 Locked

Getting a screen shot

Started by Dark Kai Mar 11, 2004 at 9:25 PM 3 replies 750+ views
Original Post
Dark Kai
Dark Kai
Hi every1. Is there a function in OpenGL to capture a screen shot of the 3D window and save it to an image format such as BMP? Thanks in advance.
To Live Is To Die.
lemurion
lemurion

void glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels);

// to take a screenshot

void *data;
glReadPixels(0, 0, screenWidth, screenHeight, GL_RGB, GL_UNSIGNED_BYTE, data);
// now write data to a file


// NOTE: remember that 0, 0 in OpenGL is the _bottom_ left corner of the screen



hope that helps
Matt
Matt
Dark Kai
Dark Kai
hi thanks for the quick reply.
emm the data i get in are in this format right?
data(0)=r
data(1)=g
data(2)=b

can i get it in this format
data(0)= integer where equal to hex bbggrr

Thanks alot again.

[edited by - Dark Kai on March 11, 2004 12:10:58 AM]
To Live Is To Die.
CrystalClear
CrystalClear
void Engine::CaptureScreen()
{
static int captures=0;
char filename[20];

sprintf( filename, "Data/screen%.2d.bmp", captures );
captures++;

BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;

char *image = new char[Width*Height*3];
FILE *file = fopen( filename, "wb");

if( image!=NULL )
{
if( file!=NULL )
{
glReadPixels( 0, 0, Width, Height, GL_BGR_EXT, GL_UNSIGNED_BYTE, image );

memset( &bf, 0, sizeof( bf ) );
memset( &bi, 0, sizeof( bi ) );

bf.bfType = ''MB'';
bf.bfSize = sizeof(bf)+sizeof(bi)+Width*Height*3;
bf.bfOffBits = sizeof(bf)+sizeof(bi);
bi.biSize = sizeof(bi);
bi.biWidth = Width;
bi.biHeight = Height;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biSizeImage = Width*Height*3;

fwrite( &bf, sizeof(bf), 1, file );
fwrite( &bi, sizeof(bi), 1, file );
fwrite( image, sizeof(unsigned char), Height*Width*3, file );

fclose( file );
}
delete[] image;
}
}
Dark Kai
Dark Kai
Umm the return data for
glReadPixels(0, 0, screenWidth, screenHeight, GL_RGB, GL_UNSIGNED_BYTE, data);

had paddings rite ? And i''ve tried changing the width and height a few times and in result the padding size vary. Is there a solution for this or am I doing something wrong?

Thanks for the code CrystalClear, but i don''t quite understand some part of it couze I''m coding in vb.net.

I''ve no idea what does this chunk does.

memset( &bf, 0, sizeof( bf ) );
memset( &bi, 0, sizeof( bi ) );

bf.bfType = ''MB'';
bf.bfSize = sizeof(bf)+sizeof(bi)+Width*Height*3;
bf.bfOffBits = sizeof(bf)+sizeof(bi);
bi.biSize = sizeof(bi);
bi.biWidth = Width;
bi.biHeight = Height;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biSizeImage = Width*Height*3;

Thanks anyway.
To Live Is To Die.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.