Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


2 questions: glMakeCurrent and screenshot

This topic is 6110 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi people! First, sorry for my grammar. I have two questions. 1) I have created within one thread four windows, each one with its device and rendering context, and I have a scene that I want to visualize. In my Draw(idView) function I do wglMakeCurrent(visualization_windows[idView].context.hDC, visualization_windows[idView].context.hRC); This function always returns 1, so it seems to work . The problem is that only draws my stuff in the first window that I have created. I do not get any errors, but seems to ignore the OpenGL calls in the other windows. This function, Draw(idView), is mainly called from the WndProc of the windows. 2) How can I take a screenshot (and sve in a .bmp, .jpg or whatever) of my OpenGL scene? Thankx. =] guille

Share this post

Link to post
Share on other sites
Well, I can answer your second question, taking a screenshot is simple. You just need to get the pixels of the screen, and put them into a bitmap and save, for instance, this is what I use for a 32 bit display:

GLubyte *temp_bits; // holds RGBA data

GLubyte *bitmap_bits; // holds RGB data

int viewport[4];
int bmp_width;
int bmp_height;

glGetIntegerv( GL_VIEWPORT, viewport );

bmp_width = viewport[2];
bmp_height = viewport[3];

temp_bits = new GLubyte[ ( bmp_width * bmp_height ) * 4 ];
bitmap_bits = new GLubyte[ ( bmp_width * bmp_height ) * 3 ];

// we need to copy the data from the frame buffer into the temp_bits

// then copy the RGB info from that into bitmap_bits

glReadPixels( 0, 0, bmp_width, bmp_height, GL_RGBA, GL_UNSIGNED_BYTE, temp_bits );

int b = 0;
for( int t = 0; t < bmp_width * bmp_height * 4; t += 4, b += 3 )
// eliminate A and reverse RGB to BGR

bitmap_bits[b] = temp_bits[t+2];
bitmap_bits[b+1] = temp_bits[t+1];
bitmap_bits[b+2] = temp_bits[t];

delete[] temp_bits;


header.bfType = ''MB'';
header.bfOffBits = sizeof( header ) + sizeof( info );
header.bfSize = sizeof( header ) + sizeof( info ) + ( bmp_width * bmp_height * 3 );
header.bfReserved1 = 0;
header.bfReserved2 = 0;

info.biBitCount = 24;
info.biClrImportant = 0;
info.biClrUsed = 0;
info.biCompression = 0;
info.biHeight = bmp_height;
info.biWidth = bmp_width;
info.biPlanes = 1;
info.biSize = sizeof( info );
info.biSizeImage = bmp_width * bmp_height * 3;
info.biXPelsPerMeter = 2952;
info.biYPelsPerMeter = 2952;

OFSTRUCT file_data;
int file_handle;

if( ( file_handle = OpenFile( current_screenie_path, &file_data, OF_WRITE | OF_CREATE ) ) == -1 )
Error( "ERROR: cannot create new screenshot file." );
return -1;

// write data to bitmap

_lwrite( file_handle, (char*)&header, sizeof( header ) );
_lwrite( file_handle, (char*)&info, sizeof( info ) );
_lwrite( file_handle, (char*)bitmap_bits, bmp_width * bmp_height * 3 );

_lclose( file_handle );

delete[] bitmap_bits;

// now update current_screenie_path for the next time this function is called

sprintf( current_screenie_path, "screenshots/shot%d.bmp", screenie_number );
last_screenie_time = GetTickCount();
LogInit( "Screenshot taken.\n" );

return 1;

Share this post

Link to post
Share on other sites
Thanks for your code.
I have tried, but seems to copy not all the pixels values (the image is in poor black and white, and a little translated), and I dont know why...


Share this post

Link to post
Share on other sites
Guest Anonymous Poster
ScreenHunter: http://www.wisdom-soft.com/downloads/downloadfiles.htm

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!