OpenGL programming experience with Delphi

zhaozj2021-02-11  205

OpenGL programming experience with Delphi

---- In developing graphics programs, especially three-dimensional graphics programs, because OpenGL has no DirectX, then Delphiopengl, with feelings, Delphi does not have C so complicated and more user-friendly, so choose Delphi OpenGL works. In this process, some issues encountered (affirmative), which presents hope and friends can communicate.

---- The first is initialization. When initialization, there are several works to be processed: 1 Create a device description table (Device Context). (Note: About DC, various data translations are not like equipment environment, equipment description table, equipment context, but seems to be less appropriate. If there is a translation like Li Shanlan, it is good. The following RC situation is the same below. 2 Set the corresponding pixel format (Pixelformat Descriptor). 3 Create a coloring context. There are several ways to obtain or create a device description table in Delphi. The simplest is to directly get the handle attribute of the Canvas Object (Tcanvas), such as:

DC: HDC;

DC: = canvas.handle;

You can also obtain the device description table with the API function GETDC. Such as:

DC: = GetDC (Handle, DC);

---- You can also obtain the device description table with a function createCompaNDC or BeginPaint..endpaint (requirement between them). However, after the device description is used, remember to release or delete it to liberate resources. Once you have the right to use the equipment description table, you can set the corresponding pixel format. The pixel format is a record type, where some fields or domains are not used (at least 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 follows:

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;

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 this below:

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_DOUBERBUFFER;

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 if the pixel format is set correctly

Describepixelformat (DC, NPIXELFORMAT, SIZEOF (PFD), @ PFD);

IF (pfd.dwflags and pfd_need_palette)

<> 0 THENSETUPPALETTE (DC, PFD);

// setuppalette is a custom function

} END;

---- After the above work is completed, it is best to run again and check the value of npixelformat. Normal, this value should be greater than 0, otherwise there is a problem. The same code, I can get the correct value of the correct than 0 value on the NT machine, but there is no correct value on the PWIN97 or 98 machine, but there is no problem when compiling, and after the NT is compiled, it can also be able to Run correctly. You can now create a colored description table (RC). Call the function wglcreateContext, WGLmakecurrent, as described below:

RC: hglrc;

RC: = WGLCReateContext (DC);

WGLmakecurrent (DC, RC);

Before the program is over, remember to release the resources occupied.

Wglmakecurrent (0);

IF rc <> null kil

WGLDeleteContext (RC);

IF GHDC <> null kil

ReleaseDC (Handle, DC);

---- The following code is overwritten from the example of OpenGL in C Builder 4. The compiled program size is about 300K, and the size of the program after C Builder 4 is 384K.

---- program code ZIP 3KB

---- OpenGL function and pixel format in the program have more detailed explanations in Mshelp in Delphi. This article does not dare to explain.


New Post(0)