1. Foreword:
Delphi supports OpenGl by default. You can use the uses OpenGL unit to reference it, and then you can use OpenGL functions. OpenGl is cross-platform, and Windows has long supported and integrated it into the system. Opengl32.dll exists in system32 and does not require additional installation. Although Windows itself has d3d, its capabilities are limited and there are relatively few related learning material pages.
Usually OpenGL only supports the following basic geometric shapes: points, lines and polygons . No surfaces or higher-level graphics (such as spheres) can be drawn as basic graphics elements. But they can be perfectly simulated using polygons. Take a casual look at modern 3D games and you'll see that they are built almost entirely out of triangles. Therefore, we are not bound by this limitation.
2. Initialization
Before using OpenGL, you need to set some related parameters. The general process is:
"Set matching pixel format" (ChoosePixelFormat, SetPixelFormat)
"Create a new OpenGL rendering context" (wglCreateContext)
"Set OpenGL related parameters", "Drawing" (glBegin, glEnd)
"Delete OpenGL rendering context" (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 is RGBA glClearColor(0, 0, 0, 0); //Set the view projection transformation matrix orthographic projection glMatrixMode(GL_PROJECTION); //Reset the currently specified matrix to the identity matrix glLoadIdentity; //Specify OpenGL to draw in this area glViewPort(0, 0, ClientWidth, ClientHeight); //Set The range of the world coordinate system gluOrtho2D(0, ClientWidth, ClientHeight, 0); // Switch the matrix transformation object to the model view transformation glMatrixMode(GL_MODELVIEW); //Reset the currently specified matrix to the identity matrix glLoadIdentity;end;
3. Drawing
The basic primitives of OpenGL are points, lines, polygons, etc. Each time you draw, you need to use glBegin() and glEnd(). For example, the following drawing function procedure Draw;
procedure TForm1.Draw;begin // Clear the buffer glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); // Clear the buffer glColor3ub(0, 255, 0); glBegin(GL_TRIANGLES); // Tell OpenGL to draw triangles glVertex2f(200, 300) ; //Transmit the three vertex coordinates of the triangle to OpenGL glVertex2f(400, 300); glVertex2f(300, 150); glEnd; //End the drawing of primitives. SwapBuffers(FDC); //Swap the contents of the double buffer, which will copy the graphics just drawn to the screen. end;
4. Finally, remember to release . The entire code is as follows:
unit Unit1;interfaceuses 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 // Clear the buffer glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); // Clear the buffer glColor3ub(0, 255, 0); glBegin(GL_TRIANGLES);//Tell OpenGL that the triangle is going to be drawn glVertex2f(200, 300); //Transfer the three vertex coordinates of the triangle to OpenGL glVertex2f(400, 300); glVertex2f(300, 150); glEnd; //End the picture yuan drawing. SwapBuffers(FDC); //Swap the contents of the double buffer, which will copy the graphics just drawn 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 background The color is black and the parameter is RGBA glClearColor(0, 0, 0, 0); //Set the view projection transformation matrix orthographic projection glMatrixMode(GL_PROJECTION); //Reset the currently specified matrix to the identity matrix glLoadIdentity; //Specify OpenGL to draw in this area glViewPort(0, 0, ClientWidth, ClientHeight); //Set the range of the world coordinate system gluOrtho2D(0, ClientWidth, ClientHeight, 0); // Switch the matrix transformation object to the model view transformation glMatrixMode(GL_MODELVIEW); //Reset the currently specified matrix to the identity 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);begin //Respecify the drawing area when the window changes size glClearColor(0, 0, 0, 0); glMatrixMode(GL_PROJECTION) ; glLoadIdentity; glViewPort(0, 0, ClientWidth, ClientHeight); gluOrtho2D(0, ClientWidth, ClientHeight, 0); glMatrixMode(GL_MODELVIEW); glLoadIdentity;end;end.
Click here to download the complete code