First, the foreword:
For Delphi, to draw pictures to be processed first, need to refer to other units, and Delphi did not take, need to download another gl.pas. The common online OpenGL unit is encapsulated in version 1.0, there is no declaration of this function. Gl.pas units can be found on the Internet. In addition, a Glaux.pas unit and a glaux.dll are needed, which is a secondary library. Downloads are available at the end of this article.
Second, the implementation process:
Painting pictures requires several processes. Window itself drawing is based on bitmaps, png,jpg, etc., when painting, can be converted to BMP painting.
1. Loading BMP pictures: Using Auxdibimageloada or other functions
2. Convert to Texture: glgentextures-> glbindtexture-> glteximage2d, gltexparameteri for setting related parameters
3. Draw textures: glbindtexture-> glbegin (gl_quads)-> gltexcoord2f--> glvertex2f-> glend
Third, the use of Gldrawpixels function drawing
Gldrawpixels has the following 5 parameters:
Width: thickness of the table image
Height: Level of the table image
format: data storage formats for table images
Atype: Unknown
pointers to Pixels: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 ');
Empty the buffer
glclear (gl_color_buffer_bit or gl_depth_buffer_bit);
Tbitmap image data in memory is continuously in reverse order, through tbitmap.scanline[tbitmap.height-1] can get the first address that image buffer address
//BMP image of the color is according to B G R storage, so 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
Using the above method to draw a picture does not need to enable texture mapping, you can use the Glpixelzoom function to scale the picture, showing the position in the lower left corner of the window (temporarily do not know how to change the image position.) )
Third, the use of texture mapping
You can use the following methods if you want to zoom in and out of the picture by making it appear.
1. According to the process, we first load the picture into the program, get the relevant picture information .
To load a picture into a texture, refer to this site: http://www.jb51.net/article/52125.htm
Loading a bitmap in Delphi is simple and can be loaded in the following ways:
(1) Loading the picture through the auxiliary library's Auxdibimageloada function, returns is a PTAUX_RGBIMAGEREC data pointer, the DIB data format is RGB.
The structure body 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 is P released? Both Dispose and Freemem cannot manipulate this pointer end
;
(2) Loading pictures through Tbitmap.loadfromfile. Delphi with, from the efficiency comparison, and Auxdibimageloada performance is the same, but the DIB data format is Bgr,dib pointer to tbitmap.scanline[bmp.height-1]
var
bmp:tbitmap;
Begin
Bmp: = tbitmap.create;
Tbitmap.loadfromfile (Extractfilepath (paramstr (0)) + ' 1.bmp ');
Do something
//run out of release
Bmp.free;
End
2. Create Textures in which the Glgentextures and Glbindtexture, in Gl.pas.
Create Texture Area
glgentextures (1, @texture);
Binding Texture Area
glbindtexture (gl_texture_2d, texture);
Use bitmaps to create image textures
glteximage2d (
gl_texture_2d, //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 made up of red, green, and blue, the default 3
bmp.width, //Texture width
bmp.height,// texture height
0,// border value default 0
Gl_ Bgr_ext,/ /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 are the filtering that OpenGL uses when OpenGL enlarges the original texture (gl_texture_mag_filter) or reduces the original texture (gl_texture_min_filter).
//Gl_linear using linear filtering, you can smooth the image processing, but need more memory and CPU
gltexparameteri (gl_texture_2d, Gl_texture_min_filter, Gl_ LINEAR); Linear filter
Gltexparameteri (gl_texture_2d, Gl_texture_mag_filter, gl_linear);//Linear filter
3. Draw Texture
Before you draw a texture, you must notify OpenGL to open the texture map glenable (gl_texture_2d). When turned on, non-textured rendering will not work. When you're done with it, close it.
The following is a drawing, using a quadrilateral, drawing a picture
//enable texture mapping
if glisenabled (gl_texture_2d) = 0 Then
glenable (gl_texture_2d);
Empty the buffer
glclear (gl_color_buffer_bit or gl_depth_buffer_bit);
L: = ten;
T: = ten;
W: = 200; Enlarge to 200*200 picture
//Select Texture if you use multiple textures in a scene, you cannot bind textures between glbegin () and Glend ()
glbindtexture (gl_texture_2d, texture);
Glbegin (gl_quads);
The first parameter of the 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 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 finished, the following is the complete code in draw, you can not 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);
Binding Texture Area glbindtexture (gl_texture_2d, texture); Use bitmaps to create image textures glteximage2d (gl_texture_2d,//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 made up of red, green, and blue, the default 3 bmp.width,//Texture width bmp.height,//Texture height 0,//Border value default 0 gl_bgr_ext,
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 are the filtering that OpenGL uses when OpenGL enlarges the original texture (gl_texture_mag_filter) or reduces the original texture (gl_texture_min_filter). Gl_linear uses linear filtering that can smooth the image processing, but requires more memory with CPU Gltexparameteri (gl_texture_2d, Gl_texture_min_filter, gl_linear); Linear filter Gltexparameteri (gl_texture_2d, Gl_texture_mag_filter, gl_linear); Linear Filter//Below is a drawing, using a quadrilateral, drawing pictures//enable texture mapping if glisenabled (gl_texture_2d) = 0 Then GLenable (gl_texture_2d);
Empty the buffer glclear (gl_color_buffer_bit or gl_depth_buffer_bit);
L: = 10;
T: = 10; W: = 200;
Enlarge to 200*200 picture//Select Texture if you use multiple textures in a scene, you cannot bind textures between glbegin () and Glend () glbindtexture (gl_texture_2d, texture);
Glbegin (gl_quads);
The first parameter of the 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 the 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 instance can be downloaded.