Hi Gents.
I'm trying to display some OpenGL drawing in a bitmap on a dialog. I can
display the bitmap on the dialog fine, but the bitmap seems determined to
stay blank.
The only example the MSDN gives is with the MFC, which I'm not
using, and otherwise my example is equivalent to one I downloaded from
Microsoft's website (GLBMP).
I create a second DC, select a bitmap into it, then try and draw OpenGL into
that. Then I BitBlt from the memory DC to the screen one. Can anybody give
me some hints on where I'm going wrong?
case WM_PAINT:
{
dc = BeginPaint(hDlg, &ps);
int width=200,height=200;
HBITMAP bitmap = NULL;
BYTE biInfo[sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD)];
BITMAPINFO *pbi = (BITMAPINFO *)biInfo;
LPVOID lpBits;
ZeroMemory(pbi, sizeof(BITMAPINFO));
/* create bitmap header */
pbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbi->bmiHeader.biWidth = width;
pbi->bmiHeader.biHeight = height;
pbi->bmiHeader.biPlanes = 1;
pbi->bmiHeader.biBitCount = GetDeviceCaps(dc, PLANES) *
GetDeviceCaps(dc, BITSPIXEL);
pbi->bmiHeader.biCompression = BI_RGB;
pbi->bmiHeader.biSizeImage = 0;
/* create bitmap and off-screen DC */
bitmap = CreateDIBSection(dc, pbi, DIB_RGB_COLORS,
&lpBits, NULL, (DWORD)0);
HDC memorydc = CreateCompatibleDC(dc);
SelectObject(memorydc,(HGDIOBJ)bitmap);
/* setup pfd for gl */
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|PFD_DRAW_TO_BITMAP| PFD_SUPPORT_OPENGL |
PFD_SUPPORT_GDI,
PFD_TYPE_RGBA,
24,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
/* do some other stuff that looks important */
pfd.cColorBits = GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc,
BITSPIXEL);
int iFormat=ChoosePixelFormat(memorydc, &pfd);
DescribePixelFormat(memorydc, iFormat, sizeof(PIXELFORMATDESCRIPTOR),
&pfd);
SetPixelFormat(memorydc, iFormat, &pfd);
/* current gl rendering context */
HGLRC glcontext=wglCreateContext(memorydc);
wglMakeCurrent(memorydc,glcontext);
/* draw a simple gl polygon into the off-screen DC */
glViewport(0,0,width,height);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,(double)width/(double)height,0.1,2000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt( 0,200,30,
0,150,0,
0,0,1);
glBegin(GL_QUADS);
glColor3f(0,1,0);
glVertex3f(-10,150,0);
glVertex3f(10,150,0);
glVertex3f(10,150,10);
glVertex3f(-10,150,10);
glEnd();
/* copy the bitmap on the off-screen DC to the on-screen DC */
BitBlt(dc,25,25,width,height,memorydc,0,0,SRCCOPY) ;
/* goodbye */
wglMakeCurrent(NULL,NULL);
wglDeleteContext(glcontext);
DeleteDC(memorydc);
DeleteObject((HGDIOBJ)bitmap);
EndPaint(hDlg, &ps);
}