Microsoft DirectX 8.0 (C++) |
Microsoft® Direct3D® rendering devices can render with one set of material properties at a time.
In a C++ application, you set the material properties that the system uses by preparing a D3DMATERIAL8 structure, and then calling the IDirect3DDevice8::SetMaterial method.
To prepare the D3DMATERIAL8 structure for use, set the property information in the structure to create the desired effect during rendering. The following code example sets up the D3DMATERIAL8 structure for a purple material with sharp white specular highlights.
D3DMATERIAL8 mat; // Set the RGBA for diffuse reflection. mat.Diffuse.r = 0.5f; mat.Diffuse.g = 0.0f; mat.Diffuse.b = 0.5f; mat.Diffuse.a = 1.0f; // Set the RGBA for ambient reflection. mat.Ambient.r = 0.5f; mat.Ambient.g = 0.0f; mat.Ambient.b = 0.5f; mat.Ambient.a = 1.0f; // Set the color and sharpness of specular highlights. mat.Specular.r = 1.0f; mat.Specular.g = 1.0f; mat.Specular.b = 1.0f; mat.Specular.a = 1.0f; mat.Power = 50.0f;
After preparing the D3DMATERIAL8 structure, you apply the properties by calling the IDirect3DDevice8::SetMaterial method of the rendering device. This method accepts the address of a prepared D3DMATERIAL8 structure as its only parameter. You can call SetMaterial with new information as needed to update the material properties for the device. The following code example shows how this might look in code.
// This code example uses the material properties defined for // the mat variable earlier in this topic. The pd3dDev is assumed // to be a valid pointer to an IDirect3DDevice8 interface. HRESULT hr; hr = pd3dDev->SetMaterial(&mat); if(FAILED(hr)) { // Code to handle the error goes here. }
When you create a Direct3D device, the current material is automatically set to the default shown in the following table.
Member | Value |
---|---|
Diffuse | (R:1, G:1, B:1, A:0) |
Specular | (R:0, G:0, B:0, A:0) |
Ambient | (R:0, G:0, B:0, A:0) |
Emissive | (R:0, G:0, B:0, A:0) |
Power | (0.0) |