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. Photos, even fractal data generated by a mathematical algorithm. Each two-dimensional texture consists of a number of small texture elements. They 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.

0 0 " to another corner ( 1 1 ). According to convention, a dimension is called s t st opengl Know which part of the texture you need to draw on each triangle.

These texture coordinates are also sometimes referred to as 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. They are usually defined as the y - axis downward, and The value of Y is added as it moves toward the bottom of the image. Just remember that if you want to see the image in the right direction, the texture coordinates will have to be considered. This will not bring us any trouble whatsoever.

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 x, D , and . The reason for this is that non- POT textures can be used in very limited situations, 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 it's usually bigger, for example 2048*2048.


2. Understanding Texture Filtering


We also need to use texture filtering to understand what happens when the size of the texture is enlarged or shrunk. 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.

When we try to squeeze a few texture elements into a fragment, narrowing occurs when we extend a texture element to many fragments. The method is happening. For each case, we were able to configure OpenGL to use a texture filter.

First of all. Two main filtering modes are described: recent neighbor filtering and bilinear interpolation. There are other filtering modes that will be explained later in the blog post. We will use the following images to illustrate each filter pattern.




Recent neighbor Filtration

This method selects recent texture elements for each fragment. When we zoom in on the texture, its jagged effect looks pretty obvious, for example, as seen.




Each texture unit is clearly displayed as a small square.

When we shrink the texture. Because there are not enough fragments to draw all the texture units, many of the details will be lost.




Bilinear filtering


Bilinear filtering uses bilinear interpolation to smooth transitions between pixels. Instead of using recent texture elements for each fragment. OpenGL four contiguous texture elements are used. and interpolation between them with a linear interpolation algorithm, the algorithm is as smooth as the previous one. We call it bilinear interpolation. is because it is interpolated along two dimensions. The following is a magnified image using a bilinear difference. It uses the same texture as the previous.






This texture looks much smoother now than it once was.

But some of the jagged edges appear, as we extend this texture too much. However, the sawtooth is not as obvious as using the recent 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 is reduced in size on the rendered surface. There will be more texture elements crowding into each fragment.

Because OpenGL 's bilinear filtering only uses four texture elements for each fragment. We're going to lose very much detail.

Because each frame chooses a different texture element. This can also cause noise and flicker in moving objects.

In order to overcome these shortcomings. The ability to use MIP mapping technology, which 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 in each fragment.

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



Figure 2 The texture of the MIP map



Use MIP maps. Consumes a lot of other memory, but the rendering is also faster, 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. Use bilinear filtering to reduce the size of the texture element to its original 12.5%. For example, with:



Figure 3 narrowing with bilinear filtering



In this way, the quality may not be as recent as the adjacent filter. Take a look at what we get when we add MIP maps. For example, with:



Figure 4 using MIP mapping to zoom Out



With the use of MIP maps. OpenGL will choose the most appropriate texture level. Then use the optimized texture to make bilinear interpolation. Each level of texture is built with information from all the texture elements. So the resulting graphs look better. Keep a lot of other details.

Tri-Linear Filtration

Suppose OpenGL switches back and forth between different MIP-map levels when we use the MIP map with bilinear interpolation . In its rendered scene, at different levels of MIP map switching. There are times when we can see the obvious jumps or lines. We are able to switch to the three-wire sex interpolation. Such A total of 8 texture element interpolation is used for each fragment.

This helps to eliminate the transition between each MIP mapping level and to get a smoother image.


3. Texture tables in the program

Method Gles20.gltextparameteri (Gl_texture_2d,gl_texture_max_filter. "Texture filtering mode"). The second parameter refers to the amplification situation.

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

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

Recent neighbor Filtration

Gl_nearest_mipmap_nearest

near-neighbor filtering using MIP mapping

Gl_nearest_mipmap_linear

near-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 agreed 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

Applications for the next texture such as the following:




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.