1. Foreword:
For Delphi, if you want to draw pictures, you need to process them first, and you need to reference other units, which are not included in Delphi, so you need to download Gl.pas separately. The OpenGl unit commonly found on the Internet is encapsulated in version 1.0, and this function is not declared. Gl.pas units can be found online. In addition, a Glaux.pas unit and glaux.dll are required, which are auxiliary libraries. A download will be provided at the end of this article.
2. Implementation process:
Painting pictures requires the following processes. Window's own drawing is based on bitmaps, such as png, jpg, etc. When drawing, it can be converted to bmp and then drawn.
1. Load bmp images: use auxDIBImageLoadA or other functions
2. Convert to texture: glGenTextures -> glBindTexture -> glTexImage2D, glTexParameteri is used to set related parameters
3. Draw texture: glBindTexture -> glBegin(GL_QUADS) -> glTexCoord2f -> glVertex2f -> glEnd
3. Draw using glDrawPixels function
glDrawPixels has the following 5 parameters:
width: width of table image
height: height of the table image
format: data storage format of table images
atype: unknown
pixels: pointer to DIB data
The sample code is as follows:
procedure TForm1.Draw;var Bmp: TBitmap;begin Bmp := TBitmap.Create; Bmp.LoadFromFile(ExtractFilePath(ParamStr(0)) + '1.bmp'); // Clear the buffer glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); / / The image data of TBitmap is stored continuously in the memory in reverse row order. The first address, which is the image buffer address, can be obtained through TBitmap.ScanLine[TBitmap.Height-1] // The color of the bmp image is stored in bgr, so you need to select GL_BGR_EXT as parameter glDrawPixels(Bmp.Width, Bmp.Height, GL_BGR_EXT, GL_UNSIGNED_BYTE, Bmp.ScanLine[Bmp.Height - 1]); SwapBuffers(FDC); Bmp.Free;end;
You do not need to enable texture mapping to draw pictures using the above method. You can use the glPixelZoom function to zoom the picture. The display position is in the lower left corner of the window (I don’t know how to change the image position for the time being.)
3. Use texture drawing
If you want to control the display position and zoom of the picture, you can use the following methods.
1. According to the process, we first load the image into the program and obtain the relevant image information .
To load images into textures, please refer to this site://www.VeVB.COm/article/52125.htm
Loading a bitmap in Delphi is very simple and can be loaded in the following ways:
(1) Load the image through the auxDIBImageLoadA function of the auxiliary library. The return is a PTAUX_RGBImageRec data pointer, and the DIB data format is RGB.
// RGB data structure TAUX_RGBImageRec = record size .bmp')); // How to release p? Neither Dispose nor Freemem can operate this pointer end;
(2) Load images through TBitmap.LoadFromFile. Delphi comes with it. From an efficiency comparison, the performance is the same as auxDIBImageLoadA, but the DIB data format is BGR and the DIB pointer is TBitmap.ScanLine[Bmp.Height - 1]
var Bmp: TBitmap;begin Bmp := TBitmap.Create; TBitmap.LoadFromFile(ExtractFilePath(ParamStr(0)) + '1.bmp'); // do something // Release Bmp.Free;end;
2. Create a texture , including glGenTextures and glBindTexture, in Gl.pas.
// Create a texture area glGenTextures(1, @texture); // Bind the texture area glBindTexture(GL_TEXTURE_2D, texture); // Use a bitmap to create an image texture glTexImage2D( GL_TEXTURE_2D, // The texture is a 2D texture GL_TEXTURE_2D 0, // The image detail level defaults to 0 3, // The number of components of the data. Because the image is composed of red, green and blue, the default is 3 Bmp.Width, // The width of the texture Bmp.Height, // The height of the texture is 0, // The value of the border is 0 by default. GL_BGR_EXT, // The data format bmp uses bgr GL_UNSIGNED_BYTE, // The data that makes up the image is unsigned byte type Bmp.ScanLine[Bmp.Height - 1] // DIB data pointer); // The following two lines let opengl use the filtering method OpenGL uses when enlarging the original texture (GL_TEXTURE_MAG_FILTER) or reducing the original texture (GL_TEXTURE_MIN_FILTER). // GL_LINEAR uses linear filtering to smooth the image processing, but requires more memory and CPU glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear filtering
3. Draw texture
Before drawing a texture, OpenGL must be notified to enable texture mapping glEnable(GL_TEXTURE_2D). When turned on, non-texture drawing will not work. Just remember to close it when you're done using it.
// The following is drawing, using a quadrilateral to draw a picture // Enable texture mapping if glIsEnabled(GL_TEXTURE_2D) = 0 then glEnable(GL_TEXTURE_2D); // Clear the buffer glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); l := 10; t := 10; w := 200; // Enlarge the picture to 200*200 // Select texture. If multiple textures are used in the scene, the texture cannot be bound between glBegin() and glEnd() glBindTexture(GL_TEXTURE_2D, texture); glBegin(GL_QUADS); // The third of glTexCoord2f One parameter is the X coordinate. // 0.0 is the left side of the texture. 0.5 is the midpoint of the texture and 1.0 is the right side of the texture. // The second parameter of glTexCoord2f is the Y coordinate. // 0.0 is the bottom of the texture. 0.5 is the midpoint of the texture and 1.0 is the top of the texture. glTexCoord2f(0, 1); glVertex2f(l, t); glTexCoord2f(1, 1); glVertex2f(l + w, t); glTexCoord2f(1, 0); glVertex2f(l + w, t + w); glTexCoord2f( 0, 0); glVertex2f(l, t + w); glEnd();
The above drawing is over. The following is the complete code in Draw. You don’t need to reference the auxiliary library Glaux.pas.
procedure TForm1.Draw;var Bmp: TBitmap; texture: GLuint; l, t, w: Integer;begin Bmp := TBitmap.Create; Bmp.LoadFromFile(ExtractFilePath(ParamStr(0)) + '1.bmp'); / / Create texture area glGenTextures(1, @texture); // Bind the texture area glBindTexture(GL_TEXTURE_2D, texture); // Use a bitmap to create an image texture glTexImage2D( GL_TEXTURE_2D, // The texture is a 2D texture GL_TEXTURE_2D 0, // The detail level of the image defaults to 0 3, // The number of components of the data. Because the image is composed of red, green and blue, the default is 3 Bmp.Width, // The width of the texture Bmp.Height, // The height of the texture is 0, // The value of the border defaults to 0 GL_BGR_EXT, // The data format bmp uses bgr GL_UNSIGNED_BYTE, // The data that makes up the image is the unsigned byte type Bmp.ScanLine[Bmp .Height - 1] // DIB data pointer); // The following two lines let opengl use the filtering method OpenGL uses when enlarging the original texture (GL_TEXTURE_MAG_FILTER) or reducing the original texture (GL_TEXTURE_MIN_FILTER). // GL_LINEAR uses linear filtering to smooth the image processing, but requires more memory and CPU glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear filtering// The following is drawing, using a quadrilateral to draw a picture // Enable texture mapping if glIsEnabled(GL_TEXTURE_2D) = 0 then glEnable(GL_TEXTURE_2D); // Clear the buffer glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); l := 10; t := 10; w := 200; // Enlarge the picture to 200*200 // Select texture. If multiple textures are used in the scene, the texture cannot be bound between glBegin() and glEnd() glBindTexture(GL_TEXTURE_2D, texture); glBegin(GL_QUADS); // The third of glTexCoord2f One parameter is the X coordinate. // 0.0 is the left side of the texture. 0.5 is the midpoint of the texture and 1.0 is the right side of the texture. //The second parameter of glTexCoord2f is the Y coordinate. // 0.0 is the bottom of the texture. 0.5 is the midpoint of the texture and 1.0 is the top of the texture. glTexCoord2f(0, 1); glVertex2f(l, t); glTexCoord2f(1, 1); glVertex2f(l + w, t); glTexCoord2f(1, 0); glVertex2f(l + w, t + w); glTexCoord2f( 0, 0); glVertex2f(l, t + w); glEnd(); Bmp.Free; SwapBuffers(FDC);end;
The complete code for this example can be downloaded here.