Originally posted by Marco Zuliani:
[b]Thanks shinpaughp
Unfortunately the paper you suggested me seems pretty intimidating (I have no experience with windows coding…), and the pieces of code are mostly C++ whereas I need to write C code only. I wonder if someone has some something simpler written in plain C.
Thanks,
Marco[/b]
Yes, that old help page is obfuscated, and even uses 256-color mode. Here’s quick info and code examples on rendering offscreen with full color on Win2k/XP:
To be able to use OpenGL commands on a Windows image, you need to create the image as a DIB as follows:
hDc = GetDC( NULL );
memset( &g_BitmapInfo, 0, sizeof(BITMAPINFO) );
g_BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
g_BitmapInfo.bmiHeader.biWidth = g_cxImage;
g_BitmapInfo.bmiHeader.biHeight = g_cyImage;
g_BitmapInfo.bmiHeader.biPlanes = 1;
g_BitmapInfo.bmiHeader.biBitCount = 24;
g_BitmapInfo.bmiHeader.biCompression = BI_RGB;
g_hBitmapImage = CreateDIBSection( hDc, &g_BitmapInfo, DIB_RGB_COLORS,
(void **)(&g_pBitmapBits), NULL, 0 );
ReleaseDC( NULL, hDc );
Variables with the “g_” prefix are global, defined in the program’s main thread. See the compiler help for proper types. The code above creates the DIB. Now we’re ready to draw on it with a memory device context. First create the memory device context and select the DIB into it:
hDc = CreateCompatibleDC( NULL );
hBitmapPrev = (HBITMAP)SelectObject( hDc, g_hBitmapImage );
Next, set up the pixel format. This code snippet shows important format descriptor settings:
pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cRedBits = 8;
pfd.cGreenBits = 8;
pfd.cBlueBits = 8;
pfd.cDepthBits = 16;
After using ChoosePixelFormat() and SetPixelFormat() you’re ready to render:
hRenderContext = wglCreateContext( hDc );
wglMakeCurrent( hDc, hRenderContext );
// OpenGL commands go here . . .
wglMakeCurrent( hDc, NULL );
wglDeleteContext( hRenderContext );
SelectObject( hDc, hBitmapPrev ); // Unselect the DIB from hDc
DeleteDC( hDc ); // Delete the Memory Device Context
Implement your OpenGL commands at the comment placeholder.
You can now use the DIB as a regular Windows bitmap.
For example, you can BitBlt it to the screen in a WM_PAINT handler as follows:
hdcMem = CreateCompatibleDC( hDc );
hBitmapPrev = (HBITMAP)SelectObject( hdcMem, g_hBitmapImage );
BitBlt( hDc, 0, 0, g_cxImage, g_cyImage,
hdcMem, 0, 0, SRCCOPY );
SelectObject( hdcMem, hBitmapPrev );
DeleteDC( hdcMem );
Finally, before your program ends, destroy the DIB:
DeleteObject( g_hBitmapImage );
Carlos Portela