Directx11 tutorial 39 texture ing (9)

Source: Internet
Author: User

In mytutoriald3d11_32, we add a texture textureclass * m_texture in planemodelclass; to read a grass texture. After the program is executed, the effect is as follows:

Complete code can be found:

Project File mytutoriald3d11_32

Download Code:

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

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

In mytutoriald3d11_33, we removed the texture resource view member variable in the model class, and also removed the textureclass class. In this case, we used a texture management class texmanagerclass to manage all the texture resources, texture management is more convenient. Previously, texture resources are placed in each model class, so that each model can correspond to only one texture, which is inconvenient.

The texture management class is very simple. It uses a vector to store all the texture resource views. STD: vector <id3d11shaderresourceview *> m_texturervs; the resource management class stores the Resource Name and resource itself, the createtex function is used to return the texture resource. If the texture resource does not exist, the texture resource is created.

Id3d11shaderresourceview * texmanagerclass: createtex (id3d11device * device, string filename)
{

// If the texture resource already exists, return; otherwise, create
For (INT I = 0; I <m_texturervs.size (); ++ I)
If (! M_texturenames [I]. Compare (filename ))
Return m_texturervs [I];

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;

Id3d11shaderresourceview * Rv = 0;

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

M_texturenames.push_back (filename );
M_texturervs.push_back (RV );

Return RV;
}

In graphicsclass, we define the member variable texmanagerclass * m_texmanager. In the rendering function, the last parameter uses m_texmanager-> createtex (m_d3d-> getdevice (), string ("grass. (DDS.

// Put plane vertices and index data into the buffer for rendering
M_planemodel-> render (m_d3d-> getdevicecontext ());
// Use light shader texture for rendering

Result = m_lighttexshader-> render (m_d3d-> getdevicecontext (), m_planemodel-> getindexcount (), worldmatrix, viewmatrix, projectionmatrix,
Light, material, camera, m_texmanager-> createtex (m_d3d-> getdevice (), string ("grass. Dds ")));
If (! Result)
{
Return false;
}

// Perform the translation operation to obtain the final model world matrix.
D3dxmatrixrotationx (& worldmatrix1,-1.57); // PAI/2
D3dxmatrixtranslation (& worldmatrix2, 0.0, 0.0, 8.0 );
D3dxmatrixmultiply (& worldmatrix3, & worldmatrix1, & worldmatrix2 );
Result = m_lighttexshader-> render (m_d3d-> getdevicecontext (), m_planemodel-> getindexcount (), worldmatrix3, viewmatrix, projectionmatrix,
Light, material, camera, m_texmanager-> createtex (m_d3d-> getdevice (), string ("stone01.dds ")));
If (! Result)
{
Return false;
}

After the program is executed, the interface is as follows. Because the two planes use the same model and the diffuse reflection coefficient is the same, the front wall is also green.

In mytutoriald3d11_34, we changed the PS code and directly used the texture as the Kd, which is much better.

Complete code can be found:

Project File mytutoriald3d11_33

Project File mytutoriald3d11_34

Download Code:

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.