Material properties detail a material's diffuse reflection, ambient reflection, light emission, and specular highlighting characteristics. Direct3D uses the D3DMATERIAL structure to carry all material property information, as well as information used only for ramp emulation (an associated texture handle and the ramp palette size). Material properties affect the colors Direct3D uses to rasterize polygons that use the material. With the exception of the specular property, each of the properties is described as an RGBA color that represents how much of the red, green, and blue parts of a given type of light it reflects, and an alpha blending factor (the alpha component of the RGBA color). The material's specular property is described in two parts: color and power. For more information, see Color Values for Lights and Materials.
Diffuse and Ambient Reflection
The dcvDiffuse and dcvAmbient members of the D3DMATERIAL structure describe how a material reflects the ambient and diffuse light in a scene. Because most scenes contain much more diffuse light than ambient light, diffuse reflection plays the largest part in determining color. Additionally, because diffuse light is directional, the angle of incidence for diffuse light affects the overall intensity of the reflection. Diffuse reflection is greatest when the light strikes a vertex parallel to the vertex normal. As the angle increases, the effect of diffuse reflection diminishes. The amount of light reflected is the cosine of the angle between the incoming light and the vertex normal, as shown here.
Ambient reflection, like ambient light, is nondirectional. Ambient reflection has a lesser impact on the apparent color of a rendered object, but it does affect the overall color, and is most noticeable when little or no diffuse light reflects off the material. A material's ambient reflection is affected by the ambient light set for a scene by calling the IDirect3DDevice3::SetLightState method with the D3DLIGHTSTATE_AMBIENT flag.
Diffuse and ambient reflection work together to determine the perceived color of an object, and are usually identical values. For example, to render a blue crystalline object, you would create a material that reflected only the blue component of diffuse and ambient light. When placed in a room with a white light, the crystal appears to be blue. However, in a room that has only red light, the same crystal would appear to be black, because its material doesn't reflect red light.
Emission
Materials can be used to make a rendered object appear to be self-luminous. The dcvEmissive member of the D3DMATERIAL structure is used to describe the color and transparency of the emitted light. Emission affects an object's color and can, for example, make a dark material brighter and take on part of the emitted color.
You can use a material's emissive property to add the illusion that an object is emitting light, without incurring the computational overhead of adding a light to the scene. In the case of the blue crystal, the emissive property could be handy if you wanted to make the crystal appear to light up, but not actually cast light on other objects in the scene. Remember, materials with emissive properties don't actually emit light that can be reflected by other objects in a scene. To achieve this effect, you would need to place an additional light within the scene.
Specular Reflection
Specular reflection creates highlights on objects, making them appear shiny. The D3DMATERIAL structure contains two members that describe the specular highlight color as well as the material's overall shininess. You establish the color of the specular highlights by setting the dcvSpecular member to the desired RGBA color — the most common colors are white or light gray. The values you set in the dvPower member control how sharp the specular effects are.
Specular highlights can create dramatic effects. Drawing again on the blue crystal analogy: a larger dvPower value will create sharper specular highlights, making the crystal appear to be quite shiny. Smaller values increase the area of the effect, creating a dull reflection that might make the crystal look frosty. To make an object truly matte, set the dvPower member to zero, and the color in dcvSpecular to black. Experiment with different levels of reflection to produce a realistic appearance for your needs. The following illustration shows two identical models, the one on the left uses a specular reflection power of 10; the model on the right has no specular reflection:
Associated Textures
When using ramp emulation, textures are assigned to materials by setting the texture's handle to the hTexture member in the D3DMATERIAL structure. You can set this member to NULL if you won't be using ramp emulation, or if no texture is associated with the material.
You can retrieve the texture handle by calling the IDirect3DTexture2::GetHandle method. For more information, see Textures.
Ramp Emulation Properties
The dwRampSize member provides information about how the material should be drawn when Direct3D uses the ramp emulation shade model for shading (as opposed to RGB emulation, or hardware acceleration). Specifically, this member is an integer value that tells Direct3D how many palette entries it should create when it renders shaded polygons. For most materials, you should set this value to a reasonable number of shades; 16 is a common value. Direct3D automatically determines the shades it uses based on the material color and number of shades you specify.
It is recommended that you use the same ramp size for all of your materials. If Direct3D runs out of palette entries when adding a new material it tries to pick the closest material already in the palette. In order to get a match, the materials must have the same ramp size. By using the same size for all materials you have a better chance of getting a good color match, providing more attractive results.
Note A material that you will use solely as a background in a viewport should have a dwRampSize value of 1. This is because backgrounds are never shaded, regardless of the shade mode you set. Providing additional ramp shades for a background material doesn't provide any advantages, and effectively wastes memory.