opengl--Texture Map __opengl

Source: Internet
Author: User

1. Create Texture Images

OpenGL requires the height and width of the texture must be 2 of the n-th square size, only to meet this condition, this texture picture is valid. Once we get the pixel value, we can pass the data to OpenGL and let OpenGL generate a texture map:

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, and the Glteximage2d function passes the pixel values in the pixels array to the currently bound texture object, creating the texture. The parameters of the Glteximage function are the type of texture, the level of texture, the number of bytes per pixel, the width and height of the texture image, the border size, the format of the pixel data, the data type of the pixel value, and the pixel data.

2. The mapping method in OpenGL

OpenGL provides us with three textures--gl_texture_1d, gl_texture_2d and gl_texture_3d. They represent 1-D textures, 2-dimensional textures, and 3-dimensional textures, respectively. Regardless of which texture is used, the method is the same: first create a Texture object and an n-dimensional array of stored texture data, and call the Glteximagen D function to pass in the corresponding texture data. In addition, there are functions to set the other properties of the texture.

2.1 Setting the texture mode

OpenGL offers 3 different mapping modes: Gl_modulate,gl_decal and Gl_blend. By default, the texture mode is gl_modulate, in which OpenGL adjusts the color and tone of the object according to the current lighting system. The second pattern is gl_decal, where all the lighting effects are invalid, and OpenGL will only draw the surface of the object based on the texture map. The last is Gl_blend, which allows us to use mixed textures. In this mode, we can mix the current texture with one color and get a new texture. We can call the Gltexenvi function to set the current mapping mode:

Gltexenvi (Gl_texture_env,gl_texture_env_mode,texturemode);

Where Texturemode is the texture pattern you want to set, which can be any of the gl_modulate,gl_decal and Gl_blend.

In addition, for Gl_blend mode, we can call

GLTEXENVFV (Gl_texutre_env,gl_texture_env_color, @ColorRGBA);

Where Colorrgba is a 4-D array that represents the Rgba color.

2.2 Texture Filters

In the process of texture mapping, if the size of the entity is not equal to the size of the texture, OpenGL scales the texture to fit the dimensions of the entity. We can use the texture filter to determine the zoom and shrink algorithm that OpenGL uses for a texture.

Call Gltexparameter to set the texture filter. Such as:

Gltexparameteri (Gl_texture_2d,gl_texture_mag_fileter, magfilter);//Set up the zoom filter
Gltexparameteri (Gl_texture_2d,gl_texture_min_filter, Minfilter); Set Zoom Filter

In the preceding call, the first parameter indicates which texture to set, and the second parameter indicates whether you want to set the magnification filter or shrink the filter. The third parameter represents the filter used. Can be one of the following values:

Table 6.3-1 The texture filters you can use

Filter

Describe

Gl_nearest

Take the nearest pixel

Gl_linear

Linear internal interpolation

Gl_nearest_mipmap_nearest

Nearest pixel of the most recent multi-map level

Gl_nearest_mipmap_linear

Internal linear interpolation in the recent multi-map hierarchy

Gl_linear_mipmap_nearest

External linear interpolation in the recent multi-map hierarchy

Gl_linear_mipmap_linear

The external and internal linear interpolation of the recent multi-map level

3 Texture Mapping

3.1 Texture coordinates

To use the current texture draft entity, we must specify the texture coordinates for the vertex before drawing each vertex. Just call

GLTEXCOORD2D (s:double;t:double);

function can be. wherein, s and T are the s and T coordinates for the 2D textures. For any texture, its texture coordinates are as shown in Figure 6:


For any texture, regardless of the true size of the texture, its top (upper left corner) of the texture coordinates constant (0,0), the lower right corner of the texture coordinates (1,1). In other words, the texture coordinate should be a decimal number between 0 and 1.

For example, the following code draws a triangle using the current texture:

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 ();

3.2 Texture Winding

As mentioned earlier, texture coordinates should be located between 0-1. So what happens when the texture coordinates are larger than this value?

We can set OpenGL to determine what action should be taken when the texture coordinates are not in this interval. We can specify two kinds of operations: Gl_clamp and Gl_repeat. For Gl_clamp, areas beyond the texture coordinates are replaced with the boundary color of the texture image, as shown in the figure.


The Gl_repeat method is to reset the texture coordinates and get the duplicate image. Observation, you can easily spot this.

You can call Gltexparameter to set the winding mode:

Gltexparameteri (Gl_texture_2d,gl_texture_wrap_s,wrapmode)//Winding mode in S direction
Gltexparameteri (Gl_texture_2d,gl_texture_wrap_t,wrapmode)//Winding mode in T direction

Among them, WrapMode desirable gl_clamp or gl_repeat.

4. Texture Object

Create and use a liberal arts object

In OpenGL, we use glgentextures to create a texture object:

Glgentextures (Count:integer; Texobjs:pointer);

Where count is the number of textures we want to create, when we just want to create a texture, just call the

var texture:gluint;
...
Glgentextures (1, @Texture);

Thus, the ID number of the texture we created is stored in the texture variable.

Once created, we use Glbindtexture to bind the created texture to the current texture. So all the texture functions will be for the current texture.

Glbindtexture (Texture:gluint);

In this way, we can call the Gltexparameter, glteximage2d and other functions to set the texture object.

Delete a Texture object

After the texture resource is used (usually when the program exits or the scene is converted), be sure to delete the texture object and release the resource.

Call

Gldeletetextures (Count:integer; Texobj:pointer);

To delete the texture object. For example

Gldeletetextures (1, @Texture);

5. Multi-Texture texture

Multi-texture textures (Mip Mapping) generate images of different sizes for a texture object. When needed, the texture level is determined according to the size of the drawing graph or the linear interpolation between different texture levels is used. The advantage of using multiple texture textures is to eliminate texture agitation. This situation often occurs when the scene is drawn farther away from the observer (Figure 6.6-1 and 6.6-2). Since the texture of multiple textures is now so fast that it doesn't differ from normal textures, we now generally use multiple textures.

Using multiple textures doesn't bother you. First, we need to create texture pictures of different levels (sizes). We need to call the n-th glteximage2d function to generate different levels of texture mapping. For example:
Glteximage2d (Gl_texture_2d,0,3,8,8,0,gl_rgb,gl_unsigned_byte,pixels);
Glteximage2d (Gl_texture_2d,1,3,4,4,0,gl_rgb,gl_unsigned_byte,pixels);
Glteximage2d (Gl_texture_2d,2,3,2,2,0,gl_rgb,gl_unsigned_byte,pixels);
Glteximage2d (Gl_texture_2d,3,3,1,1,0,gl_rgb,gl_unsigned_byte,pixels);

The second parameter of these function calls represents the level of the current texture. The resolution at level 0 is the largest. After that, the resolution at each level is half the level of the previous resolution. Such a function call should continue until the image has a height and width of 1.

But sometimes it's not always convenient to do so. We can use a Glu function to help us generate these multiple textures automatically. You only need to change the function call that generates the texture image from glteximage2d to Glubuild2dmipmaps:

Glubuild2dmipmaps (Gl_texture_2d,3,bit.width,bit.height,0,gl_rgb,gl_unsigned_byte,pixels);

In addition, you must change the texture filter to the MIP_MAP filter. For example:

Gltexparameteri (gl_texture_2d,gl_texture_min_filter,gl_linear_mipmap_linear);
Gltexparameteri (gl_texture_2d,gl_texture_mag_filter,gl_linear);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.