Setting Material Properties

After creating a material object, you must set its properties before you can use it during rendering. Setting the material's properties involves preparing a D3DMATERIAL structure and then calling the IDirect3DMaterial3::SetMaterial method.

To prepare the D3DMATERIAL structure for use, set the property information in the structure to create the desired effect during rendering. The following code fragment sets up the D3DMATERIAL structure for a purple material, with sharp white specular highlights, and a 16 color ramp (which Direct3D only uses for ramp emulation):

    D3DMATERIAL mat; 
 
    // Initialize the structure for use. 
    ZeroMemory(&mat, sizeof(D3DMATERIAL)); 
    mat.dwSize   = sizeof(D3DMATERIAL);  // This is REQUIRED.
 
    // Set the RGBA for diffuse reflection. 
    mat.dcvDiffuse.r = (D3DVALUE)0.5; 
    mat.dcvDiffuse.g = (D3DVALUE)0.0; 
    mat.dcvDiffuse.b = (D3DVALUE)0.5; 
    mat.dcvDiffuse.a = (D3DVALUE)1.0; 
 
    // Set the RGBA for ambient reflection. 
    mat.dcvAmbient.r = (D3DVALUE)0.5; 
    mat.dcvAmbient.g = (D3DVALUE)0.0; 
    mat.dcvAmbient.b = (D3DVALUE)0.5; 
    mat.dcvAmbient.a = (D3DVALUE)1.0; 
 
    // Set the color and sharpness of specular highlights. 
    mat.dcvSpecular.r = (D3DVALUE)1.0; 
    mat.dcvSpecular.g = (D3DVALUE)1.0; 
    mat.dcvSpecular.b = (D3DVALUE)1.0; 
    mat.dcvSpecular.a = (D3DVALUE)1.0; 
    mat.dvPower = (float)50.0; 
 
    // Use a 16 entry color ramp, in case 
    // we're using ramp emulation. 
    mat.dwRampSize = 16; 
 

After preparing the D3DMATERIAL structure, complete setting material properties by calling the IDirect3DMaterial3::SetMaterial method of the desired material object. This method accepts the address of a prepared D3DMATERIAL structure as its only parameter. You can call SetMaterial with new information as needed to update the material's illumination properties.

Once you successfully set a material's properties, you can retrieve its material handle, which you need to select the material during rendering. For more information, see Retrieving Material Handles.