I. Preface
I have found a lot of materials. To draw images, I have to deal with them first. I need to reference other units. I don't have them in Delphi. I need to download Gl. Pas. It can be seen from the Internet that the built-in OpenGL unit is encapsulated in Version 1.0, and this function is not declared. You can find the Gl. Pas unit online. In addition, a Glaux. Pas unit and Glaux. dll are required, which are said to be auxiliary libraries. At the end of this article, we will provide downloads. Thanks to all the materials provided by the authors.
Ii. Process
The following steps are required for painting images. The drawing of the window itself is based on bitmap, PNG, JPG, etc. During painting, it can be converted into BMP and then painted.
1. Load BMP images: auxdibimageloada or Other Functions
2. Convert to texture: glgentextures-> glbindtexture-> glteximage2d. gltexparameteri is used to set relevant parameters.
3. Draw the texture: glbindtexture-> glbegin (gl_quads)-> gltexcoord2f-> glvertex2f-> glend
3. Use gldrawpixels function plot
Gldrawpixels has five parameters: width of the table image. Height: height of the table image. Format: Data Storage Format of the table image. Atype: Unknown pixels: pointer to the dib data.
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 continuously stored in the memory in inverted order through tbitmap. scanline [tbitmap. height-1] You can obtain the first address, that is, the image buffer address. // the color of the BMP image is stored by B G R. Therefore, you need to select gl_bgr_ext as the 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 ing to draw images using the above method. You can use the glpixelzoom function to zoom in and zoom in the image. The position is displayed in the lower left corner of the window. I do not know how to change the image position for the moment.
3. Texture plotting
What should I do if I want to display the position and zoom in and out of a custom image? You can use the following methods.
1. According to the process, we first load the image into the program to obtain the relevant image information.
The image is loaded into the texture, there are several methods, some people wrote online, it is recommended to refer to learning: http://www.cnblogs.com/IamEasy_Man/archive/2009/12/14/1624062.html
Loading a bitmap in Delphi is very easy. You can load it in the following ways:
1) load the image by using the auxdibimageloada function of the auxiliary database. A ptaux_rgbimagerec Data Pointer is returned, and the dib data format is RGB. I have not found a way to release the memory after using it.
// Structure of RGB data: taux_rgbimagerec = record sizex, sizey: glint; Data: pointer; end; ptaux_rgbimagerec = ^ taux_rgbimagerec;
VaR P: ptaux_rgbimagerec; begin P: = auxdibimageloada (pansichar (extractfilepath (paramstr (0) + '1.bmp '));
// How to release P? Neither dispose nor freemem can operate this pointer end;
2) load the image through tbitmap. loadfromfile. Delphi comes with the same performance as auxdibimageloada in terms of efficiency, 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 after use. free; end;
2. Create a texture. glgentextures and glbindtexture are included in Gl. Pas.
// Create a texture region glgentextures (1, @ texture); // bind a texture region 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 details are 0 by default 3, // the score of the data. Because the image is composed of three types: Red, green, and blue, the default value is 3 BMP. width, // The width of the texture. BMP. height, // The texture height is 0, // The Border Value defaults to 0 gl_bgr_ext, // the data format BMP uses BGR gl_unsigned_byte, // The data that makes up the image is a BMP of the unsigned byte type. scanline [BMP. height-1] // Dib Data Pointer); // The following two lines let OpenGL enlarge the original texture (gl_texture_mag_filter) or reduce the original texture (gl_texture_min_filter) openGL adopts the filtering method. // Gl_linear uses linear filtering to smooth the image processing, but requires more memory and CPU gltexparameteri (gl_texture_2d, cosine, gl_linear); // linear filtering gltexparameteri (gl_texture_2d, cosine, cosine, gl_linear); // linear filtering
3. Draw texture
Before creating a texture, you must notify OpenGL to enable the texture ing gl_texture_2d ). When it is enabled, non-texture rendering does not work. When you use it, remember to close it.
// The following is a drawing. Use a quadrilateral to draw an image // enable texture ing if glisenabled (gl_texture_2d) = 0 then glable (gl_texture_2d ); // clear the buffer glclear (gl_color_buffer_bit or gl_depth_buffer_bit); L: = 10; T: = 10; W: = 200; // enlarge the image to 200*200. // select the texture. If multiple textures are used in the scenario, the glbindtexture (gl_texture_2d, texture) Texture cannot be bound between glbegin () and glend ); glbegin (gl_quads); // The first parameter of gltexcoord2f 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 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 painting is over. The following is the complete code in draw. You can reference the auxiliary library Glaux. Pas without referencing it.
Procedure tform1.draw; var BMP: tbitmap; texture: gluint; L, t, W: integer; begin BMP: = tbitmap. create; BMP. loadfromfile (extractfilepath (paramstr (0) + '1.bmp '); // create the texture region glgentextures (1, @ texture); // bind the texture region 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 details defaults to 0 3, // The data score. Because the image is composed of three types: Red, green, and blue, the default value is 3 BMP. width, // The width of the texture. BMP. height, // The texture height is 0, // The Border Value defaults to 0 gl_bgr_ext, // the data format BMP uses BGR gl_unsigned_byte, // The data that makes up the image is a BMP of the unsigned byte type. scanline [BMP. height-1] // Dib Data Pointer); // The following two lines let OpenGL enlarge the original texture (gl_texture_mag_filter) or reduce the original texture (gl_texture_min_filter) openGL adopts the filtering method. // Gl_linear uses linear filtering to smooth the image processing, but requires more memory and CPU gltexparameteri (gl_texture_2d, cosine, gl_linear); // linear filtering gltexparameteri (gl_texture_2d, cosine, cosine, gl_linear); // linear filtering // The following is a drawing. Use a quadrilateral to draw an image // enable texture ing if glisenabled (gl_texture_2d) = 0 then glable (gl_texture_2d ); // clear the buffer glclear (gl_color_buffer_bit or gl_depth_buffer_bit); L: = 10; T: = 10; W: = 200; // enlarge the image to 200*200. // select Texture if multiple textures are used in a scenario, the texture glbindtexture (gl_texture_2d, texture) cannot be bound between glbegin () and glend (); glbegin (gl_quads ); // The first parameter of gltexcoord2f 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 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 effect is as follows:
Source code download: opengl_05.zip, including Gl. PAS and Glaux. Pas
2014-07-10