Using Delphi to do OpenGL programming learning experience

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.