draw a graphic in the memory

I want draw a graphic in the memory, needn’t to display.I use CreateCompatibleDC() to create a hDC, but while I use SetPixelFormat() to set the pixelformat for the hDC, the function fails. what is the problem? Can anyone help me?
My codes are follow:
void CGLTest::Init()
{
PIXELFORMATDESCRIPTOR pfd;
int n;
HGLRC hrc;
//SourceDC is a global variable, type is CDC
//tempHDC is a global variable, type is HDC
SourceDC.CreateCompatibleDC(NULL);
tempHDC=SourceDC.GetSafeHdc();

if(!bSetupPixelFormat())
	return;

n=::GetPixelFormat(tempHDC);
: [img]http://www.opengl.org/discussion_boards/ubb/biggrin.gif[/img]escribePixelFormat(tempHDC, n, sizeof(pfd), &pfd);
hrc=wglCreateContext(tempHDC);
wglMakeCurrent(tempHDC,hrc);

}

BOOL CGLTest::bSetupPixelFormat()
{
static PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|PFD_DRAW_TO_BITMAP|PFD_SUPPORT_OPENGL,
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
};
int pixelformat;

if((pixelformat=ChoosePixelFormat(tempHDC,&pfd))==0)
{
	MessageBox("ChoosePixelFormat falled");
	return FALSE;
}
if(SetPixelFormat(tempHDC,pixelformat,&pfd)==FALSE)
{
	MessageBox("SetPixelFormat falled");
	return FALSE;
}
return TRUE;

}

Not sure, but ChoosePixelformat sucks.
You’d better enumerate the pixelformats and select one yourself.
What happens if you don’t specify PFD_DRAW_TO_WINDOW?
OpenGL window classes need WS_CLIPCHILDREN and WS_CLIPSIBLINGS set, not sure if that is necessary for memory DCs.

Have you tried using only one of the flags PFD_DRAW_TO_WINDOW and PFD_DRAW_TO_BITMAP? I’m not sure, but since these are mutually exclusive, specifying both might be the problem.

Edit: I just noticed you said that it is SetPixelFormat (not ChoosePixelFormat) that fails. Are you sure you’re not setting the pixel format more than once for a particular window? Check GetlastError to see exactly what the problem is.

[This message has been edited by Aaron (edited 10-24-2002).]

iirc, only one of this flags can be set.
When PDF_DRAW_TO_BITMAP set, OpenGL work in software emulation mode.

Thanks.
I have tried using only PFD_DRAW_TO_BITMAP, but the problem is still there. my programm doesn’t work.
Any other suggestion?

Xue

When I need to draw a scene in memory without rendering it (in non extended OpenGL) I use almost the same init sequence as you except that I use PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL| PFD_DOUBLEBUFFER for dwFlags member. Then I draw the scene, don’t do SwapBuffers and use:

glReadBuffer(GL_BACK);
// read the pixels into the image
glReadPixels(0, 0, img->width, img->height,
	GL_BGR_EXT,
	GL_UNSIGNED_BYTE,
	img->data);

to read the image from the backbuffer. Seems to work for me (if you need more speed you can use some specific extensions)…

There are annoying problems with the window being occluded though. A screesaver, any window or tooltip will screw up the rendering. I noticed just yesterday that if I minimise the window (even during rendering) the rendering comes out just fine. It’s a bit hacky but I think you should be able to render off-screen by rendering to a minimised window. Perhaps something else along the same lines is possible.

Cheers,
Madoc

Draw into a pbuffer and get the bitmap with glReadPixels. All current OpenGL implementations support the pbuffer extention.
There is a very easy to use pbuffer C++ class at http://www.cfxweb.net/~delphigl/downloads.htm