Texture ing bilinear interpolation Filtering

Source: Internet
Author: User

Author: Fan yipeng

When you do texture ing, do you often notice the obvious sertices displayed on the screen, and the texture pixels you use are too obvious? Now we will talk about how to solve this problem, and the method we use is to filter your texture. Next, we will introduce several common filtering methods. At last, we will introduce the implementation of bilinear interpolation filtering in detail.

Bi-linear interpolation

Bilinear interpolation uses adjacent pixels in the texture to smooth out the Sawtooth between pixels in the screen output. Bilinear interpolation makes the screen output image smoother. Next, let's take a look at its basic formula.

 
 
double texture[N][M];  // x = [0, N), y = [0, M)
 
double xReal;          // xReal = [0, N - 1]
 
double yReal;          // yReal = [0, M - 1]
int x0 = int(xReal), y0 = int(yReal);
double dx = xReal - x0, dy = yReal - y0,
omdx = 1 - dx, omdy = 1 - dy;
double bilinear = omdx * omdy * texture[x0][y0] +
omdx * dy * texture[x0][y0+1] +
dx * omdy * texture[x0+1][y0] +
dx * dy * texture[x0+1][y0+1];
 

Observe this formula and you will see that we effectively use the fractional part of the texture coordinates to interpolation adjacent pixels in the four textures. We determine the weight of each pixel based on the distance of the corresponding pixel. That is to say, when the decimal part of the U coordinate of the texture increases, the weight of the adjacent pixels on the left is reduced, and the reduced weight increases the adjacent pixels on the right. The V coordinates in the vertical direction are also similar.

In practical applications, it is obviously slow to directly use this formula. You can use the fixed-point integer and Table query methods to cancel the mixed operation of floating point and integer types and remove multiplication. (Tip: Create a result table for the combination of A and B colors x * A + () * B)

MIP-Mapping

For the first time, I saw that the MIP-mapping technology was in the quake game, which is already everywhere. This technology was invented by Williams in 1983. The name "MIP" originated from "multum in Parvo", which is probably a lot of things in a small area.

Specifically, the idea of MIP-mapping is to build a set of textures, which requires about 1.3 times of memory. Each sub-texture is obtained by filtering the parent texture. Its length and width are 1/2 of its parent texture, and its area is 1/4 of its parent texture. Next, you can select the most appropriate ing based on distance during the application. Practice has proved that this technology is simple, but it is indeed very effective for Improving the Quality of texture ing.

Using MIP-mapping, You can map a smaller area texture to a smaller polygon, which is good for reducing texture disturbance. For example, you have a x texture. when it begins to move in the direction away from the observer, you will see that it starts to flash and vibrate. This phenomenon occurs because we map a large texture to a small area. During the last frame, you may draw the pixels at (50, 20) in the texture, to the next frame, but the pixels at (60, 30) in the texture. If the two pixels are very different, you will observe the phenomenon mentioned above. In general, this drastic change in texture coordinates will damage the image quality and affect the cache efficiency. MIP-mapping is undoubtedly a good solution to this problem.

Tri-linear interpolation

After introducing bilinear interpolation and MIP-mapping, we should talk about tri-linear interpolation. In fact, tri-linear interpolation is also very simple, it is the combination of the first two technologies. It performs bilinear interpolation on each texture of MIP-mapping, and interpolation on the two adjacent textures of MIP-mapping by distance. Calculate the bilinear interpolation pixel value of a point on a larger texture and the bilinear interpolation pixel value of a point on a smaller texture, perform a similar interpolation based on the distance between the two textures of the target.

The use of Tri-linear interpolation can eliminate texture switching in MIP-mapping (the previous frame uses a texture of a certain size, while the next frame changes) and can provide smooth high-quality image output.

Compared with the first two technologies, tri-linear interpolation requires a large amount of computing. Currently, it can only be implemented by hardware.

Implementation of bilinear interpolation texture ing

Next, we will use a descriptive code to look at how bilinear interpolation texture ing is implemented.

The initialization code is omitted here, and the most important part is: U and V are fixed-point integers in the 16.16 format D.
U and DV are floating point du = (U & 0 xFFFF)/65536.0
dv = (V & 0xFFFF) / 65536.0
invdu = 1.0 - du
invdv = 1.0 - dv
// Calculate the respective weights based on the distance to the adjacent four pixels. weight1 = invdu * invdv
Weight2 = invdu*dv
Weight3 = du*invdv
Weight4 = du*dv
// Obtain the RGB color component of each pixel. r00 = texture [V> 16] [U> 16]. Red
g00 = Texture[V >> 16][U >> 16].Green
b00 = Texture[V >> 16][U >> 16].Blue
r01 = Texture[(V >> 16) + 1][U >> 16].Red
g01 = Texture[(V >> 16) + 1][U >> 16].Green
b01 = Texture[(V >> 16) + 1][U >> 16].Blue
r10 = Texture[V >> 16][(U >> 16) + 1].Red
g10 = Texture[V >> 16][(U >> 16) + 1].Green
b10 = Texture[V >> 16][(U >> 16) + 1].Blue
r11 = Texture[(V >> 16) + 1][(U >> 16) + 1].Red
g11 = Texture[(V >> 16) + 1][(U >> 16) + 1].Green
b11 = Texture[(V >> 16) + 1][(U >> 16) + 1].Blue
// Weight-Based RGB color component Red = weight1 * r00 + weight2 * R01 + weight3 * R10 + weight4 * R11
Green = Weight1*g00 + Weight2*g01 + Weight3*g10 + Weight4*g11
Blue = Weight1*b00 + Weight2*b01 + Weight3*b10 + Weight4*b11
// Putpixel (X, Y, pack (red, green, blue ))
 

This code is obviously not optimized (at least don't use that putpixel). If your program is not competent enough, you may not be able to achieve the ideal optimization goal, in this case, you can directly use the hardware (new 3D hardware supports these functions ). However, I believe that after you understand the idea of bilinear interpolation filtering, you will surely be able to draw a line between them and use it to make your game images more attractive.

 

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.