How to specify the value of a material
The material on the surface may change; That is to say, different points on the surface may have different material values. For example, the body, windows, lights, and tires of a car model have different abilities to reflect and absorb light, so the material values on the surface of the car should also be different
(Divide the car mesh into 5 material attribute groups).
One way to simulate different material values is to define material values at the vertex level. These material values will be linearly interpolated on the surface of the triangle, so that each surface point of the triangle mesh has a material value. However, as can be seen from the "Mountain and Valley Demonstration Program" in Chapter 6, the effect simulated by defining material colors at the vertex level is still too rough. Moreover, vertex colors will add additional data to the vertex structure, and we also need tools to color each vertex. A more common method is to use texture mapping, which will be introduced in the next chapter. Also, during frequent drawing calls, we also need to modify the material. Therefore, we set the material value as a member of the constant buffer, unless this material value is changed between two draws, all geometry drawn after the setting will use this material. The following pseudocode shows how to draw a sedan:
set the main car light material to constant cache
draw the geometry of the main car lights
set auxiliary car light material to constant cache
draw the geometry of the auxiliary light
set tire material to constant cache
draw tire geometry
set window material to constant cache
draw window geometry
set body material to constant cache
draw body geometry
Our material structure is defined as follows, located in LightHelper. h:
XMFLOAT4 Diffuse ;
XMFLOAT4 Specular; // w the component is the highlight intensity
We will not discuss the Reflect member variable here, as it will be used in simulating mirrors in the future. We place the specular highlight index p in the fourth component of the material's highlight color. This is because lighting does not require an alpha component, so the empty space can store some useful things. The alpha component of the diffuse material can be used for alpha blending in subsequent chapters.
Finally, we remind readers that each point on the surface of a triangular mesh requires a normal vector so that each point on the mesh surface can calculate the angle of incident light (according to Lambert's cosine theorem). To estimate the normal vector of each point on the surface of a triangular mesh, we specify the normal at the vertex level. During the rasterization phase, these normals are linearly interpolated on the surface of the triangle.
So far, we have discussed the principles of light composition, but we have not yet discussed specific types of light sources. In the following three sections, we will explain the implementation methods of parallel light, point light, and spotlight.