Microsoft DirectX 8.1 (C++) |
Materials describe how polygons reflect light or appear to emit light in a 3-D scene. Essentially, a material is a set of properties that tell Microsoft® Direct3D® the following things about the polygons it is rendering.
Direct3D applications written in C++ use the D3DMATERIAL8 structure to describe material properties. For more information, see Material Properties.
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; // Set the RGBA for emissive color. mat.Emissive.r = 0.0f; mat.Emissive.g = 0.0f; mat.Emissive.b = 0.0f; mat.Emissive.a = 0.0f;
After preparing the D3DMATERIAL8 structure, you apply the properties by calling the 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) |
You retrieve the material properties that the rendering device is currently using by calling the IDirect3DDevice8::GetMaterial method for the device. Unlike the IDirect3DDevice8::SetMaterial method, GetMaterial doesn't require preparation. The GetMaterial method accepts the address of a D3DMATERIAL8 structure, and fills the provided structure with information describing the current material properties before returning.
// For this example, the pd3dDev variable is assumed to // be a valid pointer to an IDirect3DDevice8 interface. HRESULT hr; D3DMATERIAL8 mat; hr = pd3dDev->GetMaterial(&mat); if(FAILED(hr)) { // Code to handle the error goes here. }
For more information about material properties see:
Default Material Properties Note If your application does not specify material properties for rendering, the system uses a default material. The default material reflects all diffuse light—white, for example—with no ambient or specular reflection, and no emissive color.