function SetupPixelFormat(var dc:HDC):Boolean; var ppfd:PPIXELFORMATDESCRIPTOR; npixelformat:Integer; begin New(ppfd); ppfd^.nSize:=sizeof(PIXELFORMATDESCRIPTOR); ppfd^.nVersion:=1; ppfd^.dwFlags:=PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER; ppfd^.dwLayerMask:=PFD_MAIN_PLANE; ppfd^.iPixelType:=PFD_TYPE_COLORINDEX; ppfd^.cColorBits:=8; ppfd^.cDepthBits:=16; ppfd^.cAccumBits:=0; ppfd^.cStencilBits:=0; npixelformat:=ChoosePixelFormat(dc, ppfd); if (nPixelformat=0) then begin MessageBox(NULL, ’choosePixelFormat failed’, ’Error’, MB_OK); Result:=False; Exit; end; if (SetPixelFormat(dc, npixelformat, ppfd)= FALSE) then begin MessageBox(NULL, ’SetPixelFormat failed’, ’Error’, MB_OK); Result:=False; Exit; end; Result:=True; Dispose(ppfd); end; 也可以向下面这样进行设置如:
var pfd: PixelFormatDescriptor; nPixelFormat : Integer; begin FillChar(pfd,SizeOf(pfd),0); with pfd do begin nSize:=sizeof(pfd); nVersion:=1; dwFlags:=PFD_SUPPORT_OPENGL or PFD_DRAW_TO_BITMAP or PFD_DOUBLEBUFFER; iPixelType:=PFD_TYPE_RGBA; cColorBits:=32; cDepthBits:=32; iLayerType:=Byte(PFD_MAIN_PLANE); end; nPixelFormat:=ChoosePixelFormat(DC,@pfd); SetPixelFormat(DC,nPixelFormat,@pfd); { // 使用DescribePixelFormat检查象素格式是否设置正确 DescribePixelFormat(DC,nPixelFormat,SizeOf(pfd),@pfd); if (pfd.dwFlags and PFD_NEED_PALETTE) < > 0 then SetupPalette(DC,pfd); //SetupPalette是自定义函数 }end; 上述工作完成以后,最好先运行一遍,并检查nPixelFormat的值。正常的话,该值应该是大于0的,否则就有问题。同样的代码,我在NT机器上能够得到正确的大于0的值而在PWIN97或98的机器上得不到正确值,但是编译时不会有问题,而且NT上编译后在PWIN97机器上也能够正确运行。现在可以创建着色描述表(RC)了。调用函数wglCreateContext、wglMakeCurrent,如下例示:
wglMakeCurrent(0,0); if RC< >null then wglDeleteContext(RC); if ghDC< >null then ReleaseDC(Handle,DC); 以下的代码是从C++Builder 4中OpenGL的例子改写过来的。编译后的程序大小为300K左右,而在C++Builder 4下编译后程序的大小为384K。
< a href=”061403-05.zip ”>程序代码 Zip 3KB 程序中的OpenGL函数及象素格式在Delphi中的MSHelp中有比较详细的解释,本文不敢多做解释。
没有相关教程