Now we create a project named mytutoriald3d_23. In this project, we will sort out the code in the previous chapter:
1. we add the diffuse reflection coefficient and highlight coefficient of the material to the vertex attributes. We put them in a light material structure before, so that we can flexibly define the material attributes of different vertices, of course, this also increases the size of the vertex buffer.
Struct vertextype
{
D3dxvector3 position;
D3dxvector3 normal; // normal
D3dxvector4 KD; // material diffuse reflection coefficient
D3dxvector4 KS; // The highlight coefficient of the material.
};
2. Define three structures in common. h to indicate the illumination, material, and camera respectively. They correspond to three constant buffers in PS.
Common. h
Struct lightbuffertype
{
D3dxvector4 lightposition [num_lights]; // The Position of the light source.
D3dxvector4 lightcolor [num_lights]; // light source color
D3dxvector4 globalambient [num_lights]; // environmental light reflection coefficient of the light source
D3dxvector4 attenuation [num_lights]; // attenuation coefficient, which corresponds to constants X, Y, and Z, linear and quadratic Coefficients
D3dxvector4 spotlight [num_lights]; // The Inside and Outside cos values of spotlight, X, and Y are stored respectively.
D3dxvector3 lightdirection [num_lights]; // Parallel Direction
Float shininess [num_lights]; // highlight Index
};
Struct materialbuffertype
{
D3dxvector4 ke; // The self-illumination of the material.
D3dxvector4 Ka; // environmental light coefficient of the material
};
Struct camerabuffertype
{
D3dxvector4 cameraposition; // camera position
};
The corresponding light. ps and various model classes also need to change.
After the program is executed, the final effect is as follows:
Complete code can be found:
Project File mytutoriald3d11_23
Download Code:
Http://files.cnblogs.com/mikewolf2002/myTutorialD3D11_2345.zip
The following mytutoriald3d11_24 and mytutoriald3d11_25 are the light versions of simple terrain and simple water surface. The Code is a combination of mytutoriald3d11_23, mytutoriald3d11_13, and mytutoriald3d11_14.
In these two projects, I added DT to the render function of graphicsclass to dynamically change the position of the light source.
Mytutoriald3d11_24 program execution results are as follows: because there are two light sources and both have spot effects, the position of a light source will continue to rotate, so we can see the variation of light and shade:
The project mytutoriald3d11_25 has the following effects:
Complete code can be found:
Project files mytutoriald3d11_24 and mytutoriald3d11_25
Download Code:
Http://files.cnblogs.com/mikewolf2002/myTutorialD3D11_2345.zip