#### Archived

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.

## 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 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; BITMAPFILEHEADER header; BITMAPINFOHEADER info; 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 screenie_number++; sprintf( current_screenie_path, "screenshots/shot%d.bmp", screenie_number ); last_screenie_time = GetTickCount(); LogInit( "Screenshot taken.\n" ); return 1;

##### Share on other sites
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...

guille

##### Share on other sites
It could be that your getting the size and the offsets wrong, my code only works for 32bit displays mind.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631790
• Total Posts
3002367
×