[Transfer] About Mip Maps

1. Mip concept

1. Understanding of Mip

When the viewpoint of the camera moves back and forth, such as a character in a game moves, if the image texture that is far away from the character uses the same texture size as the texture that is closer to the character, then we will not be able to distinguish the distance.

To solve such a problem, it is necessary to scale down the graphic textures that are far away from the viewpoint. At this time, we need a technique for shrinking textures, which is the requirement of Mip.

In order to reduce the impact of this problem, we can filter the texture in advance, and store the filtered image as a continuous, low-resolution version (the original image is full resolution), this action is the Mip map.

The actual problem solved by Mip map is to improve the performance of texture rendering and improve the display quality of texture. Its main use scenario is to use it when there is a texture scene that needs to shrink the texture display.

2. The solution in OpenGL.

In OpenGL, a concept called: multi-level gradual distance texture (Mipmap) is proposed to solve this problem. The method is mainly to reduce the resolution of a texture, and the resolution of the texture is reduced every time the distance from the viewpoint reaches a threshold value. It becomes 1/2 of the previous one, so that the texture map with small resolution can be used on the long-distance view, and it will not be so obtrusive.

Then the fundamental solution is to load all the Mip maps of this series into the corresponding single texture state. The size of the Mip map is 1/2 smaller than the previous one, until the size of the last image is 1* 1 texture unit.

3. About the loading of Mip layer

glTexImage2D (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels);

glTexImage2D函数中This levelis to specify which Mip layer the image data is to be used for. The first layer is 0, and then gradually +1. If Mip is not used, then only layer 0, the original texture, is loaded. By default, in order to use all Mip maps, we need to load all Mip maps. If there are 6 layers of Mip textures, then the following functions must be used to set the Mip base layer and Mip maximum layer:


//设置Mip贴图基层
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_BASE_LEVEL,0);

//设置Mip贴图最大层
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAX_LEVEL,6);

You can also use Min Tiers GL_TEXTURE_MIN_LODand Max Tiers GL_TEXTURE_MAX_LODto limit how widely texture levels are used.

4. When do you need a Mip map?

Only use Mip when you need to zoom out, and you don't need Mip when zooming in, that is, you only minFilterneed to use Mip when using the minimum filtering method! ! !

About minFilterthe four modes:

  • GL_NEAREST_MIPMAP_NEAREST: It has very good performance and can also reduce the problem of flickering, but the visual effect is poor.

  • GL_LINEAR_MIPMAP_NEAREST: Uses a high-quality linear filter, often used for game acceleration, and a fast selection method.

  • GL_LINEAR_MIPMAP_LINEARAnd GL_NEAREST_MIPMAP_LINEAR: They perform some additional linear interpolation between mip layers, eliminating traces of transformations between different mip layers, at the cost of some performance.

  • GL_LINEAR_MIPMAP_LINEAR: Highest precision, trilinear mipmap.

The texture filtering mode table is as shown in Figure 1.1:

5. glGenerateMipmap function

void glGenerateMipmap (GLenum target);

This function will generate a complete set of mipmaps for the texture.

Generate all Mipmaps:


     if(minFilter == GL_LINEAR_MIPMAP_LINEAR  ||
       minFilter == GL_LINEAR_MIPMAP_NEAREST ||
       minFilter == GL_NEAREST_MIPMAP_LINEAR ||
       minFilter == GL_NEAREST_MIPMAP_NEAREST)
       {
          //纹理生成所有的mip层
          //݇参数:GL_TEXTURE_1D,GL_TEXTURE_2D,GL_TEXTURE_3D
          glGenerateMipmap(GL_TEXTURE_2D);
       }

2. Anisotropic filtering

Anisotropic filtering is mainly used to improve the quality of texture filtering operations.

When we texture the geometry, if the viewing direction and the viewing point are exactly vertical, then there will be no problem, but if the viewing direction has an angular deviation or inclination, then the surrounding texture units During conventional texture sampling, for example, only the most conventional two filtering methods GL_NEARESTand GL_LINEARfilter sampling are used at this time, then some texture information will be lost, resulting in blurred texture rendering of geometric figures.

In order to solve this blurring phenomenon, we must consider the angle of observation when filtering textures, and use a method to solve the deviation of the observation angle. This method is anisotropic filtering.

Anisotropic filtering can be used in mip map texture filtering, or in regular texture filtering.

The two steps of anisotropic filtering are as follows:

(1). Obtain the maximum number of anisotropic filtering that can be supported


    //存储这个最大的数
    GLfloat flargest;
    //获取可以获得支持的最大的各向异性过滤的数量
    glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT,&fLargest);

(2). When setting texture parameters, use GL_TEXTURE_MAX_ANISOTROPY_EXTparameters to set anisotropic data

    //各向异性采样
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAX_ANISOTROPY_EXT,fLargest)
    //恢复到各相同性采样
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f);

The higher the value of anisotropic filtering, the more texture units are sampled along the direction of maximum change. Anisotropic filtering will increase the cost of performance, but the performance of current hardware devices is also redundant, so the impact on performance is not great.

Guess you like

Origin blog.csdn.net/u012861978/article/details/130727940