In the development of graphics programs, especially the three-dimensional graphics program, because the feeling of OpenGL without DirectX so complex, so the choice of Delphiopengl, by feeling, Delphi is not C + + so complex and more humane, so choose delphi+ OpenGL to work. In the process, encounter (certainly) some problems, here to hope and friends can communicate.
First of all, initialization. When initializing, there are several tasks that need to be done: ① Create a Device description table (Device context). (Note: With regard to DC, different kinds of data translations are different, such as equipment environment, equipment description table, equipment context, etc., but it seems not very appropriate.) It would be nice to have a translator like Li Shanlan. The RC case is the same below) ② sets the corresponding pixel format (PixelFormat descriptor). ③ Create a coloring description table (Rendering context). There are several ways to get or create a Device description table in Delphi. The simplest is to get the handle property (Handle) of the canvas object (Tcanvas) directly, such as:
DC:HDC;
DC:=Canvas.Handle;
You can also use the API function GetDC to obtain the device description table. Such as:
DC:=GetDC(Handle,DC);
You can also use the function CreateCompatibleDC or beginpaint. EndPaint (Pay attention to the difference between them) to obtain the device description table. But after using the device description table, remember to release or delete it to liberate the resource. After you have the right to use the device description table, you can set the corresponding pixel format. Pixel formats are record types, some of which are of little use (at least for now). When the pixel format description is complete, the Choosepixelformat and Setpixelformat functions are invoked to match and set up the device description table. Like the following code:
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;