---- When developing graphics programs, especially three-dimensional graphics programs, I chose DelphiOpenGL because I felt that OpenGL was not as complicated as DirectX. In terms of feeling, Delphi was not as complicated as C++ and was more user-friendly, so I chose Delphi+. OpenGL does the work. In the process, I encountered (definitely) some problems, and I would like to express my hope that I can communicate with my friends.
----The first is initialization. During initialization, several tasks need to be performed: ① Create a device description table (Device Context). (Note: Regarding DC, the translations of various materials are different, such as device environment, device description table, device context, etc., but they seem not very appropriate. It would be great if there could be a translator like Li Shanlan. The situation of RC below is the same )②Set the corresponding pixel format (PIXELFORMAT DESCRipTOR). ③Create a rendering description table (Rendering Context). There are several ways to obtain or create device context tables in Delphi. The simplest is to directly obtain the handle attribute (Handle) of the canvas object (TCanvas), 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 (you need to pay attention to the difference between them) to obtain the device description table. However, after the device description table is used up, you must remember to release or delete it to free up resources. Once you have access to the device context table, you can set the corresponding pixel format. Pixel format is a record type with some fields or fields that are of little use (at least for now). After the pixel format description is completed, call the ChoosePixelFormat and SetPixelFormat functions to match and set it with the device description table. Such as 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; You can also set it as follows: 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); { // Use DescribePixelFormat to check whether the pixel format is set correctly DescribePixelFormat(DC,nPixelFormat,SizeOf(pfd),@pfd); if (pfd.dwFlags and PFD_NEED_PALETTE) < > 0 then SetupPalette(DC,pfd);//SetupPalette is a custom function }end;----After the above work is completed, it is best to run it again and check the value of nPixelFormat. Normally, this value should be greater than 0, otherwise there will be a problem. For the same code, I can get the correct value greater than 0 on the NT machine but not the correct value on the PWIN97 or 98 machine. However, there will be no problem when compiling, and after compiling on NT, it can also be used on the PWIN97 machine. Run correctly. Now it is time to create the shading context (RC). Call the functions wglCreateContext and wglMakeCurrent, as shown in the following example:
RC: HGLRC; RC:=wglCreateContext(DC); wglMakeCurrent(DC,RC); Before the program ends, remember to release the occupied resources. wglMakeCurrent(0,0); if RC< >null then wglDeleteContext(RC); if ghDC< >null then ReleaseDC(Handle,DC);
---- The following code is adapted from the OpenGL example in C++Builder 4. The size of the compiled program is about 300K, while the size of the compiled program under C++Builder 4 is 384K.
---- < a href="061403-05.zip ">Program code Zip 3KB
---- The OpenGL functions and pixel format in the program are explained in detail in MSHelp in Delphi. This article does not dare to explain more.