1. Create a texture image OpenGL requires that the texture height and width must both be the Npower of 2. This texture image is valid only when this condition is met. Once the pixel value is obtained, we can pass the data to OpenGL to generate a texture map for OpenGL: Glgentextures (1, @ texture ); Glbindtexture (gl_texture_2d, texture ); Glteximage2d (gl_texture_2d, 0,3, bit. Width, bit. Height, 0, gl_rgb, gl_unsigned_byte, pixels ); The glgentextures and glbindtexture functions are used to create and bind texture objects. The glteximage2d function transmits the pixel values in the pixels array to the currently bound texture object, and then creates the texture. The parameters of the glteximage function are Texture type, texture level, number of bytes per pixel, width and height of the texture image, border size, and pixel data format, the data type of the pixel value. 2. Paster mode in OpenGL OpenGL provides three textures: gl_texture_1d, gl_texture_2d, and gl_texture_3d. They represent one-dimensional textures, two-dimensional textures, and three-dimensional textures respectively. No matter which texture is used, the usage is the same: Create a texture object and an n-dimensional array that stores texture data, and call glteximageND function to input the corresponding texture data. In addition, we can use some functions to set other texture features. 2.1 set the texture Mode OpenGL provides three different Paster modes: gl_modulate, gl_decal, and gl_blend. By default, the texture mode is gl_modulate. In this mode, OpenGL adjusts the color and brightness of an object based on the current illumination system. The second mode is gl_decal. In this mode, all illumination effects are invalid. OpenGL will only draw the surface of an object based on Texture textures. Finally, gl_blend. This mode allows us to use a mixed texture. In this mode, we can mix the current texture with the same color to get a new texture. We can call the gltexenvi function to set the current texture mode: Gltexenvi (gl_texture_env, gl_texture_env_mode,Texturemode); Texturemode is the texture mode you want to set. It can be gl_modulate, gl_decal, or gl_blend. In addition, for the gl_blend mode, we can call Gltexenvfv (gl_texutre_env, gl_texture_env_color ,@Colorrgba); Colorrgba is a four-dimensional array that represents the rgba color. 2.2 texture Filter During texture ing, if the size of the element is not equal to the texture size, OpenGL scales the texture to adapt to the size of the element. We can set a texture filter to determine the algorithm used by OpenGL to zoom in or out a texture. Call gltexparameter to set the texture filter. For example: Gltexparameteri (gl_texture_2d, gl_texture_mag_fileter,Magfilter); // Sets the zoom filter. Gltexparameteri (gl_texture_2d, gl_texture_min_filter,Minfilter); // Sets the zoom-out filter. In the above call, the first parameter indicates the texture to be set, and the second parameter indicates whether to set the zoom filter or zoom filter. The third parameter indicates the filter used. It can be one of the following values:
Table 6.3-1 available texture Filters |
Filter |
Description |
Gl_nearest |
Nearest Pixel |
Gl_linear |
Linear internal Interpolation |
Gl_nearest_mipmap_nearest |
Nearest pixel of the latest multi-texture level |
Gl_nearest_mipmap_linear |
Linear interpolation within the latest multi-texture level |
Gl_linear_mipmap_nearest |
External linear interpolation with recent multi-texture levels |
Gl_linear_mipmap_linear |
External and internal linear interpolation with recent multi-texture levels |
3. Texture ing 3.1 texture coordinates To use the current texture to draw elements, we must specify texture coordinates for each vertex before drawing. You only need to call Gltexcoord2d (S: Double; T: Double ); Function. S and T are the S and T coordinates of 2D textures. For any texture, its texture coordinates are shown in Figure 6.4-1:
|
Figure 6.4-1 texture coordinates |
For any texture, regardless of the true size of the texture, the texture coordinate at the top (upper left corner) is always (), and the texture coordinate at the bottom right is always ). That is to say, the texture coordinate should be a decimal point between 0 and 1. For example, the following code uses the current texture to draw a triangle: Glbindtexture (Tex ); Glbegin (gl_triangles ); Gltexcoord2d (0, 0 ); Glvertex3f (-10,-10, 0 ); Gltexcoord2d (0, 1 ); Glvertex3f (-10, 10, 0 ); Gltexcoord2d (1, 1 ); Glvertex3f (10, 10, 0 ); Glend (); |