Directx11 tutorial 37 texture ing (7)

Source: Internet
Author: User

This chapter is a program that combines illumination textures Based on tutorials 35 and 36, that is, adding textures to the Rotated Cube in the scenario.

The vertex structure in lighttex. VS is:

Struct vertexinputtype
{
Float4 position: position;
Float3 normal: normal;
Float2 TEX: texcoord0; // texture coordinate
Float4 KD: diffuse;
Float4 KS: specular;
};

// Texture coordinates
Output. Tex = input. Tex;

Texture coordinates are not changed, but output from vs to PS.

In lighttex. ps, there are now two more variables:

Texture2d shadertexture;
Samplerstate sampletype;

Texture and texture sampling status.

The HLSL function float4 texturecolor = shadertexture can be used for texture color. sample (sampletype, input. tex); finally, we calculate the color of the light and the color of the texture sample by means of pattern modulation (that is, multiplication), and get the final color. Finalcolor = finalcolor * texturecolor;

Now we have made some changes to the cubemodelclass class:

Struct vertextype
{
D3dxvector3 position;
D3dxvector3 normal; // normal
D3dxvector2 texture; // texture coordinate
D3dxvector4 KD; // material diffuse reflection coefficient
D3dxvector4 KS; // The highlight coefficient of the material.
};

Define a private member variable textureclass * m_texture; use it to read the texture and generate the required texture resource view data. In the initialization function, we load the texture data:

Bool cubemodelclass: Initialize (id3d11device * device, char * modelfilename, wchar * texturefilename)
{
Bool result;

// Load model data,
Result = loadmodel (modelfilename );
If (! Result)
{
Return false;
}

// Load the texture data.
Result = loadtexture (device, texturefilename );
If (! Result)
{
Return false;
}

// Initialize the vertex buffer and the vertex index buffer.
Result = initializebuffers (device );
If (! Result)
{
Return false;
}

Return true;
}

Next, we will write a new lighttexshaderclass to handle the shader processing of texture and light mixing. The difference between this class and lightshaderclass is that there is an additional variable in the sampling state.

// Sample status
Id3d11samplerstate * m_samplestate;

After this status is set, it will be passed into PS

// Set the sampling status
Devicecontext-> pssetsamplers (0, 1, & m_samplestate );

After running the program, the interface is as follows:

Complete code can be found:

Project File mytutoriald3d11_30

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.