First, the foreword:
Delphi defaults to support OpenGL, you can use the uses OpenGL unit for reference, then you can use the function of OpenGL. OpenGL is Cross-platform, and Windows is very early supported and integrated in the system, in the System32 Opengl32.dll, no additional installation required. Although Windows itself has D3D, it has limited capacity and relatively few related learning data pages.
usually OpenGL supports only the following basic geometries: dots, lines, and polygons . No surface or more advanced graphics, such as spherical graphics, can be drawn as basic graphic elements. But they can be perfectly imitated with polygons. Take a look at modern 3D games and you'll find that they're almost entirely built by triangles. Therefore, we are not bound by this restriction.
Second, initialization
Before using OpenGL, you need to make some related parameter settings. The general process is:
"Set Matching pixel format" (Choosepixelformat, Setpixelformat)
"Create a new OpenGL render description table" (Wglcreatecontext)
"Set OpenGL related parameters", "Drawing" (Glbegin, Glend)
"Remove OpenGL Render Description table" (Wgldeletecontext)
Procedure Tform1.formcreate (Sender:tobject);
var pfd:tpixelformatdescriptor;
Pixelformat:integer; Begin with PFD do begin nsize: = sizeof (Tpixelformatdescriptor); Size nversion: = 1; Version dwflags: = Pfd_support_opengl or Pfd_draw_to_window or pfd_doublebuffer; Support double-buffering Ipixeltype: = Pfd_type_rgba; Color type ccolorbits: = 24;
Preferred color depth Credbits: = 0; Credshift: = 0;
Color bits (ignored) cgreenbits: = 0;
Cgreenshift: = 0;
Cbluebits: = 0;
Cblueshift: = 0;
Calphabits: = 0; Calphashift: = 0;
No alpha buffer caccumbits: = 0; Caccumredbits: = 0; No accumulation buffer, caccumgreenbits: = 0;
Accum bits (ignored) caccumbluebits: = 0;
Caccumalphabits: = 0; Cdepthbits: = 16; Depth buffer cstencilbits: = 0; No stencil buffer cauxbuffers: = 0; No auxiliary buffers Ilayertype: = Pfd_main_plane;
Main layer breserved: = 0;
Dwlayermask: = 0;
Dwvisiblemask: = 0; Dwdamagemask: = 0;
End
FDC: = GetDC (Handle);
PixelFormat: = Choosepixelformat (FDC, @pfd);
If PixelFormat = 0 then Exit;
If not Setpixelformat (FDC, PixelFormat, @pfd) then Exit;
FHRC: = Wglcreatecontext (FDC);
Wglmakecurrent (FDC, FHRC);
Set the background color to the black parameter RGBA glclearcolor (0, 0, 0, 0);
Set the view projection transformation matrix positive projection Glmatrixmode (gl_projection);
Resets the currently specified matrix to the unit matrix glloadidentity;
Specifies OpenGL to draw glviewport in this area (0, 0, clientwidth, clientheight);
Set the scope of the world coordinate system gluortho2d (0, clientwidth, clientheight, 0);
Switching the matrix transformation object into a Model view transformation Glmatrixmode (Gl_modelview);
Resets the currently specified matrix to the unit matrix glloadidentity;
End
Third, drawing
OpenGL's basic entities are dots, lines, polygons, and so on, each drawing needs to use Glbegin () and Glend (). such as the following rendering Function procedure Draw;
Procedure Tform1.draw;
Begin
//empty buffer
glclear (gl_color_buffer_bit or gl_depth_buffer_bit);
Empty buffer
glcolor3ub (0, 255, 0);
Glbegin (Gl_triangles)//tells OpenGL to draw the Triangle
glvertex2f (200, 300);/////(three vertex coordinates of the transport triangle to OpenGL
glvertex2f);
GLVERTEX2F (in);
Glend; Ends the drawing of an entity.
swapbuffers (FDC)//Swap dual buffer content, which will reprint the newly-drawn graphic to the screen. End
;
four, finally remember to release . All the code is as follows:
Unit Unit1;
Interface uses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, OpenGL;
Type TFORM1 = Class (Tform) procedure formcreate (sender:tobject);
Procedure Formdestroy (Sender:tobject);
Procedure Formpaint (Sender:tobject);
Procedure Formresize (Sender:tobject);
Private {private declarations} FDC:HDC;
FHRC:HGLRC;
Procedure Draw;
Public {public declarations} end;
var Form1:tform1;
Implementation {$R *.DFM} procedure Tform1.draw;
Begin//Empty buffer Glclear (gl_color_buffer_bit or gl_depth_buffer_bit);
Empty buffer Glcolor3ub (0, 255, 0); Glbegin (gl_triangles);//Tell OpenGL to draw the Triangle glvertex2f (200, 300);
The three vertex coordinates of the transmission triangle are given to OpenGL GLVERTEX2F (400, 300);
GLVERTEX2F (300, 150); Glend;
Ends the drawing of an entity. Swapbuffers (FDC);
Swap double buffer content, which will reprint the newly-drawn graphic to the screen.
End
Procedure Tform1.formcreate (Sender:tobject);
var pfd:tpixelformatdescriptor;
Pixelformat:integer; Begin with PFD do begin nsize: = sizeof (Tpixelformatdescriptor); Size Nversion : = 1; Version dwflags: = Pfd_support_opengl or Pfd_draw_to_window or pfd_doublebuffer; Support double-buffering Ipixeltype: = Pfd_type_rgba; Color type ccolorbits: = 24;
Preferred color depth Credbits: = 0; Credshift: = 0;
Color bits (ignored) cgreenbits: = 0;
Cgreenshift: = 0;
Cbluebits: = 0;
Cblueshift: = 0;
Calphabits: = 0; Calphashift: = 0;
No alpha buffer caccumbits: = 0; Caccumredbits: = 0; No accumulation buffer, caccumgreenbits: = 0;
Accum bits (ignored) caccumbluebits: = 0;
Caccumalphabits: = 0; Cdepthbits: = 16; Depth buffer cstencilbits: = 0; No stencil buffer cauxbuffers: = 0; No auxiliary buffers Ilayertype: = Pfd_main_plane;
Main layer breserved: = 0;
Dwlayermask: = 0;
Dwvisiblemask: = 0;
Dwdamagemask: = 0;
End
FDC: = GetDC (Handle);
PixelFormat: = Choosepixelformat (FDC, @pfd);
If PixelFormat = 0 then Exit;
If not Setpixelformat (FDC, PixelFormat, @pfd) then Exit; FHRC: = WglcreaTecontext (FDC);
Wglmakecurrent (FDC, FHRC);
Set the background color to the black parameter RGBA glclearcolor (0, 0, 0, 0);
Set the view projection transformation matrix positive projection Glmatrixmode (gl_projection);
Resets the currently specified matrix to the unit matrix glloadidentity;
Specifies OpenGL to draw glviewport in this area (0, 0, clientwidth, clientheight);
Set the scope of the world coordinate system gluortho2d (0, clientwidth, clientheight, 0);
Switching the matrix transformation object into a Model view transformation Glmatrixmode (Gl_modelview);
Resets the currently specified matrix to the unit matrix glloadidentity;
End
Procedure Tform1.formdestroy (Sender:tobject);
Begin Wglmakecurrent (FDC, FHRC);
Wgldeletecontext (FHRC);
ReleaseDC (Handle, FDC);
End
Procedure Tform1.formpaint (Sender:tobject);
Begin Draw;
End
Procedure Tform1.formresize (Sender:tobject);
Re-assign the drawing area glclearcolor (0, 0, 0, 0) when the Begin//window changes size;
Glmatrixmode (gl_projection);
glloadidentity;
Glviewport (0, 0, clientwidth, clientheight);
gluortho2d (0, clientwidth, clientheight, 0);
Glmatrixmode (Gl_modelview);
glloadidentity;
End End.
Complete code point this download