BannerLeft BannerLeft

 

Direct3D Materials

Materials and lights work together. A material is set before rendering geometry and defines how that geometry reflects light. If there are no lights in your scene the material has no affect.

See the related lighting notes as well.

Material Properties

In order to define a material you fill out a material structure and call the SetMaterial device interface. The material structure has a number of variables that determine how light is reflected. There are four colours diffuse, ambient, specular and emissive.

Diffuse Colour

This colour determines how diffuse light is reflected. (When you create a light you also supply a diffuse colour).

Ambient Colour

This determines how ambient light is reflected.

Specular

Specular colour of the object. This is like that shiny bit on an apple, it is where light is focused to cause a bright patch. In order for your object to actually show specular you must turn on specular lighting - see the DirectX help for more details. There is also a power value that defines the size of the specular highlight.

Emissive

Your object can emit light. This can be quite useful e.g. you might have a crystal object that emits red light. Note however that no light is cast onto any other objects.

Creating a material

To create a material we fill in the structure values. Commonly an object reflects all types of light with the same colour so the example shown below is often used. Note: the r, g, b values this time range from 0 to 1.0f .

D3DMATERIAL9 m_material;

void CreateMaterial(float r,float g,float b)
{

    ZeroMemory( &m_material, sizeof(D3DMATERIAL9) );
    m_material.Diffuse.r = m_material.Ambient.r = r
    m_material.Diffuse.g = m_material.Ambient.g = g;
    m_material.Diffuse.b = m_material.Ambient.b = b;
    m_material.Diffuse.a = m_material.Ambient.a = 1.0f;

}

When we come to render our object we must set the material e.g.

void Render(const D3DXVECTOR3 &pos)
{
     gD3dDevice->SetMaterial( &m_material );

     // Now render as usual, everything rendered after here will use this material
     ...
}



© 2004-2014 Keith Ditchburn