Directx11 tutorial (31) Texture ing (1)

Source: Internet
Author: User

In the previous example, we either directly assign a color value to the vertex, or set the diffuse and specular coefficients in the vertex attribute to calculate the surface color of the Object Based on illumination parameters, however, the color will be much less realistic. If we map an image directly to the triangle surface to obtain the surface color value of the object, the effect will be much better, such as the two images below, the picture on the right maps an image to two triangles.

 

Even we can directly use the color value of the image as the diffuse value of the vertex (or pixel) and combine the illumination calculation formula to obtain the final surface color value, which will have a better effect.

Generally, the technology that maps an image to a triangle is called texture ing ). During texture ing, we use texture coordinates (or U or V coordinates) to divide the texture. For example, if the image is 256*256 pixels, the texture coordinates in the upper left corner are (), the upper right corner is (), the lower left is (), and the lower right is ).

The reason for using this normalized UV coordinate is to make the UV coordinate value irrelevant to the texture image size, such as for the following figure, UV coordinates (0.5, 0.5 ), in fact, it is the pixel mapped to the texture (128,128). If it is changed to a larger texture image (1024*1024), the (0.5, 0.5) will be mapped to (512,512 ).

For a triangle, we need to specify UV coordinates for each vertex. For example, for the two triangles, We need to specify UV coordinates according to the texture coordinates on the graph, the two triangles on the left are fully mapped to the right texture.

The following describes several concepts related to texture Sampling:

Magnification and minification:

If the triangle after raster is exactly 256*256 pixels, which is exactly the same as the texture size, the ing relationship is a unit (Texel) corresponding to a pixel's texture ), however, the actual situation may be more complicated.

For example, the texture size is 256*256, but the quad composed of our two triangles is 512*512. Then, a texture unit corresponds to four pixels on Quad, A Texture unit corresponds to multiple rasterized pixels,

In the case of magnification, how does one map textures? We usually use bilinear interpolation. For example, if there is no corresponding texture Texel in the red pixel on the left of the figure below, the color value is obtained by the pixel color on the top, bottom, and right, of course, we can also choose the closest Texture unit color as its color, depending on the mag value set in the texture filter.

In contrast to magnification, minification indicates that the triangle surface is small, while the texture is large, which is the problem of how a pixel chooses color among multiple texture units. In this case, you can select the closest Texture unit, or perform bilinear interpolation on the upper and lower left texture units and map them with pixel. For example, the quad surface of 128*128 and the texture ing of 256*256 are as follows:

Mipmap layer:

Generally, when creating a texture, you can choose to create the mipmp layer. Then, the system automatically creates a series of downsample images for us, each image is 1/4 of the previous image (as shown in). We can select the number of layers of the mipmap on our own. If you use the DDS file, it already contains the mipmap layer. Therefore, you 'd better convert the texture You need into the DDS format and obtain the number of mipmap layers for preprocessing.

 

How do I perform texture ing when the mipmap layer is used?

For example, if the original texture size is 256*256 and the triangle size is 178*178, a three-linear interpolation will be performed. Quad first and 128*128 textures execute the magnificaiton interpolation to get a result, perform minification interpolation with the 256*256 texture to get a result, and then perform linear interpolation on the two results. The obtained color is the final pixel color.

Addressing Mode of texture:

Generally, the texture coordinates of our vertices are within the range of [0, 1]. In this way, the pixel after raster can always find the corresponding texture units from the texture.

We can also extend texture ing by setting the texture addressing mode. For texture coordinates not between [], we can also find the corresponding Texture unit.

Common texture addressing modes include:

Wrap method:

For texture coordinates not between [], the texture units are wound. As shown in the following figure, this method is especially suitable for pasting a large plane with a small texture, for example, Tile laying on the ground and grass laying on the ground. This also requires us to seamlessly connect the left and right sides, up and down edges when designing a texture image.

Border mode:

Set the texture coordinates that are not in the range of [] to some specified colors. For example, specify the texture coordinates as red.

Clamp mode:

Specify the texture coordinate that is not in the range of [] as the color of the closest Texture unit.

Mirror method:

Specify the texture units by mirroring the texture coordinates that are not in the range of [0, 1], as shown in:

Now we know some texture sampling settings. before using the texture in d3d11, we must set the sampling status through the following functions:

// Create a texture sampling state descriptor.
Samplerdesc. Filter = d3d11_filter_min_mag_mip_linear;
Samplerdesc. addressu = d3d11_texture_address_wrap;
Samplerdesc. addressv = d3d11_texture_address_wrap;
Samplerdesc. addressw = d3d11_texture_address_wrap;
Samplerdesc. miplodbias = 0.0f;
Samplerdesc. maxanisotropy = 1;
Samplerdesc. comparisonfunc = d3d11_comparison_always;
Samplerdesc. bordercolor [0] = 0;
Samplerdesc. bordercolor [1] = 0;
Samplerdesc. bordercolor [2] = 0;
Samplerdesc. bordercolor [3] = 0;
Samplerdesc. minlevels = 0;
Samplerdesc. maxlevels = d3d11_float32_max;

// Create the texture status.
Result = device-> createsamplerstate (& samplerdesc, & m_samplestate );
If (failed (result ))
{
Return false;
}

In this tutorial, we understand some basic concepts in the use of d3d11 textures. The next log is to start writingCode...

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.