Android OpenGL ES (vii)----understanding texture and texture filtering

Source: Internet
Author: User

1. Understanding Textures

the textures in OpenGL can be used to represent images, photographs, and even fractal data generated by a mathematical algorithm. Each two-dimensional texture consists of a number of small texture elements, which are small pieces of data, similar to the fragments and pixels we discussed earlier. The most common way to use textures is to load data directly from an image file.

each two-dimensional texture has its own coordinate space, ranging from a corner (0 0 ) to another corner ( 1 1 ). By convention, a dimension is called s t st texture coordinates so that opengl uv texture coordinates.



Figure 1 OpenGL two-dimensional texture coordinates

For an OpenGL texture, it has no intrinsic directionality, so we can use different coordinates to direct it to whatever direction we like. However, most computer images have a default orientation, which is usually defined as the y - axis downward,and the value ofy increases as it moves toward the bottom of the image. As long as we remember, if we want to look at the image in the right direction, the texture coordinates have to be considered, which will not cause us any trouble.

In standard OpenGL ES 2.0 , textures do not have to be squares, but each dimension should be a power of 2 (POT). This means that each dimension is such a number, such as : The reason for this is that non- POT textures can be used in very limited occasions, and POT textures are used in various situations.

The size of the texture also has a maximum value, which varies depending on the implementation, but is usually larger, such as 2048*2048.


2. Understanding Texture Filtering


When the size of the texture is enlarged or shrunk, we also need to use texture filtering to clearly indicate what will happen. When we draw a texture on the rendered surface, the texture element of that texture may not be able to accurately map to the OpenGL -generated fragment. There are two things: Zoom out and zoom in. The narrowing occurs when we try to squeeze a few texture elements into a fragment, and the method occurs when we extend a texture element to many fragments. For each case, we can configure OpenGL to use a texture filter.

First, two basic filtering modes are described: Nearest neighbor filtering and bilinear interpolation. There are other filtering modes, which will be explained later in the blog post. We will use the image below to illustrate each filter pattern.




Nearest Neighbor Filter

This method selects the closest texture element for each fragment. When we zoom in on the texture, its jagged effect looks pretty obvious, as shown in.




Each texture unit is clearly displayed as a small square.

When we shrink the texture, many details are lost because there are not enough fragments to draw all the texture units.




Bilinear filtering


Bilinear filtering uses bilinear interpolation to smooth transitions between pixels instead of using the nearest texture element for each fragment,OpenGL uses four contiguous texture elements and interpolates between them with a linear interpolation algorithm, This algorithm sits in the same color as the one previously described in smoothing. The reason we call it bilinear interpolation is because it is interpolated along a two-dimensional dimension. The following is a magnified image using bilinear difference, which uses the same texture as the previous one.






This texture now looks much smoother than before. However, some of the jagged edges appear because we extend the texture too much, but the sawtooth is not as obvious as using the nearest neighbor filter.


MIP Map



Although bilinear filtering is ideal for processing amplification, it is not useful when zoomed out to a certain size. The more a texture reduces the size of the rendered surface, the more texture elements will be crowded onto each fragment. Because OpenGL 's bilinear filtering only uses four texture elements for each fragment, we will lose a lot of detail. Because each frame chooses a different texture element, it can also cause noise and flicker in moving objects.

To overcome these drawbacks, MIP mapping techniques can be used to generate a set of optimized textures of different sizes. When this set of textures is generated,OpenGL uses all the texture elements to generate each level of texture, and when filtering the textures, make sure that all the texture elements are used. When rendering,OpenGL chooses the most appropriate level for each fragment based on the number of texture elements per fragment.

is a set of MIP -map textures that combine them on a single graph for easy comparison.



Figure 2 The texture of the MIP map



Using MIP maps consumes more memory, but renders more quickly, because smaller textures occupy less space in the GPU's texture cache.

To better understand how MIP Maps Improve the quality of the downsizing, we compare that cute robot by using bilinear filtering to narrow the size of the texture element to its original 12.5%, such as:



Figure 3 narrowing with bilinear filtering



This quality may not be as good as the nearest neighbor filter. Take a look at what we get when we add MIP maps. such as:



Figure 4 using MIP mapping to zoom Out



With the use of MIP maps,OpenGL chooses the most appropriate texture level and then uses the optimized texture to do bilinear interpolation. Each level of texture is built with information from all the texture elements, so the resulting graph looks better and retains more detail.

Tri-Linear Filtration

If OpenGL switches back and forth between different MIP map levels, when we use a MIP map with bilinear interpolation , in the scene in which it is rendered, at different levels when MIP maps are switched on, we sometimes see noticeable jumps or lines. We can switch to the three-wire interpolation so that each fragment will have a total of 8 texture element interpolation. This helps to eliminate transitions between each MIP map level and to get a smoother image.


3. The texture parameter table in the program

Method Gles20.gltextparameteri (Gl_texture_2d,gl_texture_max_filter, "Texture filtering mode"), and the second parameter refers to the magnified case.

method Gles20.gltextparameteri (gl_texture_2d,gl_texture_min_filter, "Texture filtering mode"), and the second parameter refers to the case of narrowing.

The first parameter is to tell OpenGL that this should be treated as a two-dimensional texture.


Table 1 OpenGL Texture filtering mode

Gl_nearest

Nearest Neighbor Filter

Gl_nearest_mipmap_nearest

Nearest neighbor filtering using MIP maps

Gl_nearest_mipmap_linear

Nearest neighbor filtering using interpolation between MIP mapping levels

Gl_linear

bilinear interpolation

Gl_linear_mipmap_nearest

bilinear interpolation using MIP maps

Gl_linear_mipmap_linear

Tri-linear interpolation ( bilinear filtering with interpolation between MIP mapping levels)



Table 2 Texture filtering patterns allowed in each case

Narrow Gl_nearest
Gl_nearest_mipmap_nearest
Gl_nearest_mipmap_linear
Gl_linear
Gl_linear_mipmap_nearest
Gl_linear_mipmap_linear
Amplification Gl_nearest
Gl_linear

The next texture is applied as follows:




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android OpenGL ES (vii)----understanding texture and texture filtering

Related Article

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.