· Appendix A 4. Coloring of Direct3D
(Cai Junsheng copyright)
Now let's introduce 3D polygon coloration in Direct3D. What kind of coloring mode is there in Direct3D? How to set up coloring mode? There are two coloring patterns in Direct 3D, namely flat coloration and gouraud coloring. Two coloring distinction is the difference between each coloring in the polygon. The flat coloring is the color of each point in the entire polygon with the color of the first vertex; and Gouraud coloring is based on the color, normal and light of each vertex, which is calculated from the color of each point in the polygon. Interior is interpolated. For example, the red component of the vertex 1 is 0.8, the red ingredient of the vertex 2 is 0.4, and the red ingredients in the middle of the vertex 1 and the vertex 2 are calculated using Gouraud. The advantage of flat coloration is that coloring speed is fast. The advantage of gouraud coloring is that the plane is smooth and it looks more real.
The following figure compares two different colored teapots:
There are still many different points, and the flat coloring mode makes two flat-connected rides look more tips, and Gouraud coloring looks a smooth. But in the light coloration, the flat colors is not good, because the entire plane is uniform, it looks not true, but Gouraud is just the opposite, it looks more real, more close to the realistic light. Object. However, Gouraud coloring will also throw away some of the details because of the interpolation operation. Like gouraud coloring like the picture below, you will lose the middle spotlight.
The default coloring mode in Direct3D is gouraud coloring, which can call the function idirect3ddevice9 :: setRenderState to set the parameter D3DRS_SHADEMODE to select different colorime. How is the following code set up a coloring mode.
// Set to a flat coloring mode. // PDEV is a pointer to iDirect3DDevice9. HR = PDEV-> SetRenderstate (D3DRS_SHADEMODE, D3DSHADE_FLAT); if (Failed (HR)) {// Processes the error condition here. } // Set gouraud coloring mode. The default is this mode. HR = PDEV-> Setrenderstate (D3DRS_SHADEMODE, D3DSHADE_GOURAUD); if (Failed (HR)) {// Processing an error here. }