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