Platform SDK: DirectX

Setting Material Properties

Direct3D rendering devices can render with one set of material properties at a time.

[C++]

In a C++ application, you set the material properties that you want the system to use by preparing a D3DMATERIAL7 structure, then calling the IDirect3DDevice7::SetMaterial method.

To prepare the D3DMATERIAL7 structure for use, set the property information in the structure to create the desired effect during rendering. The following code fragment sets up the D3DMATERIAL7 structure for a purple material with sharp white specular highlights.

    D3DMATERIAL7 mat; 
 
    // 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; 

After preparing the D3DMATERIAL7 structure, you apply them by calling the IDirect3DDevice7::SetMaterial method of the rendering device. This method accepts the address of a prepared D3DMATERIAL7 structure as its only parameter. You can call SetMaterial with new information as needed to update the material properties for the device. The following example shows how this might look in code.

    // This code fragment uses the material properties defined for 
    // the mat variable earlier in this topic. The lpd3dDev is assumed
    // to be a valid pointer to an IDirect3DDevice7 interface.
    HRESULT hr;
    hr = lpd3dDev->SetMaterial(&mat);
    if(FAILED(hr))
    {
// Code to handle the error goes here.
    }
[Visual Basic]

From Visual Basic, set the material properties that you want the system to use by preparing a D3DMATERIAL7 type, then calling the Direct3DDevice7.SetMaterial method.

To prepare the D3DMATERIAL7 type for use, set the property information in the structure to create the desired effect during rendering. The following code fragment sets up the D3DMATERIAL7 type for a purple material with sharp white specular highlights.

    Dim mat As D3DMATERIAL7
 
    ' Set the RGBA for diffuse reflection.
    mat.diffuse.r = 0.5
    mat.diffuse.g = 0#
    mat.diffuse.b = 0.5
    mat.diffuse.a = 1#
 
    ' Set the RGBA for ambient reflection.
    mat.ambient.r = 0.5
    mat.ambient.g = 0#
    mat.ambient.b = 0.5
    mat.ambient.a = 1#
 
    ' Set the color and sharpness of specular highlights.
    mat.specular.r = 1#
    mat.specular.g = 1#
    mat.specular.b = 1#
    mat.specular.a = 1#
    mat.power = 50#

After preparing the D3DMATERIAL7 type, you apply the properties by calling the Direct3DDevice7.SetMaterial method of the rendering device. This method accepts the address of a prepared D3DMATERIAL7 type as its only parameter. You can call SetMaterial with new information as needed to update the material properties for the device. The following example shows how this might look in code.

    ' This code fragment uses the material properties defined for
    ' the mat variable earlier in this topic. The d3dDev is assumed
    ' to contain a valid reference to a Direct3DDevice7 object.
    On Local Error Resume Next
    
    Call d3dDev.SetMaterial(mat)
    If Err.Number <> DD_OK Then
' Code to handle the error goes here.
    End If