Directx11 tutorial (35) Texture ing (5)

Source: Internet
Author: User

So far, our textureclass initialization function is very simple. To put it bluntly, it is a line of code:

Result = d3dx11createshaderresourceviewfromfile (device, filename, null, null, & m_texture, null );

This line of code loads a DDS file, and other options are set to null, then the system will use the format of the file itself, such as the tong we load. DDS. We have previously created the mipmaps layer for it and set the surface format to dxt5 (corresponding to the bc3 compression format in d3d11 ). Now we can see what settings are used for the generated 2D texture.

First, add the following code after d3dx11createshaderresourceviewfromfile, so that we can debug and observe the texture format:

Id3d11texture2d * Tex;
M_texture-> getresource (id3d11resource **) & Tex );
D3d11_texture2d_desc texdesc;
Tex-> getdesc (& texdesc );

After the breakpoint is set, we can see the content of texdesc. The value of mipmaps is 10, and the texture format is dxgi_format_bc3_unorm:

Now we have installed a tong.jpg file in the graphicsclass class,

// Initialize the model object.
Result = m_model-> initialize (m_d3d-> getdevice (), L "tong.jpg ");

Observe the texdesc content. We can see that the mipmaps layer is 10, but the texture format is dxgi_format_r8g8b8a8_unorm. By default, the system will automatically create the mipmaps layer, but the compression format is not automatically selected.

What should I do if I want to compress the JPG texture to be loaded? In fact, it is very easy to use the upload image information in d3dx11createshaderresourceviewfromfile. for example, when we install the tong.jpg file, we will use the compression format of dxgi_format_bc3_unorm.

Bool textureclass: Initialize (id3d11device * device, wchar * filename)
{
Hresult result;
D3dx11_image_load_info loadinfo;
Zeromemory (& loadinfo, sizeof (d3dx11_image_load_info ));
Loadinfo. bindflags = d3d11_bind_shader_resource;
Loadinfo. format = dxgi_format_bc3_unorm;
Loadinfo. miplevels = d3dx11_default; // The maximum mipmaps layer is generated.
Loadinfo. mipfilter = d3dx11_filter_linear;

// Create a texture resource view from a file.
Result = d3dx11createshaderresourceviewfromfile (device, filename, & loadinfo, null, & m_texture, null );
If (failed (result ))
{
Return false;
}

// These codes are used to debug texture attributes...
Id3d11texture2d * Tex;
M_texture-> getresource (id3d11resource **) & Tex );
D3d11_texture2d_desc texdesc;
Tex-> getdesc (& texdesc );
Return true;
}

Observe the output information of texdesc to verify that our settings are valid.

If we use loadinfo to load our previous Tong. Dds, what will happen? We set loadinfo. format: dxgi_format_bc2_unorm. The following figure shows that the format is BC2, not Tong. DDS's own bc3 format. It can be seen that the format defined by loadinfo will overwrite the file's own format.

Of course, we can also create texture and texture resources separately. The effect is the same. The code for separate creation is as follows:

Id3d11texture2d * ptexture2d = NULL;
D3d11_shader_resource_view_desc srvdesc;
D3d11_texture2d_desc DESC;

D3dx11createtexturefromfile (device, filename, & loadinfo, null, (id3d11resource **) (& ptexture2d), null );

Ptexture2d-> getdesc (& DESC );

Srvdesc. format = DESC. format;
Srvdesc. viewdimension = d3d11_srv_dimension_texture2d;
Srvdesc. texture2d. mostdetailedmip = 0;
Srvdesc. texture2d. miplevels = DESC. miplevels;

Device-> createshaderresourceview (ptexture2d, & srvdesc, & m_texture );

 

Complete code can be found:

Project File mytutoriald3d11_28

Http://files.cnblogs.com/mikewolf2002/d3d1127-28.zip

Http://files.cnblogs.com/mikewolf2002/pictures.zip

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.