Platform SDK: DirectX

Step 3.2: Set Up Material and Initial Lighting States

[Visual Basic]

The information in this section pertains only to applications written in C and C++. See Direct3D Immediate Mode Visual Basic Tutorials.

[C++]

After you create the basic 3-D rendering objects (a DirectDraw object, a rendering device, and a viewport), you've got almost all you need to render a simple scene. The next thing to do is to create and configure a material and set some initial lighting states. These can all be changed later if needed. For an introduction to these concepts, see Lighting and Materials.

The Triangle sample sets material parameters, as shown here:

    D3DMATERIAL7 mtrl;
    ZeroMemory( &mtrl, sizeof(mtrl) );
    mtrl.ambient.r = 1.0f;
    mtrl.ambient.g = 1.0f;
    mtrl.ambient.b = 0.0f;
    pd3dDevice->SetMaterial( &mtrl );

The preceding code prepares material properties, represented by a D3DMATERIAL7 structure. The code prepares the D3DMATERIAL7 structure to describe a material that will reflect the red and green components of ambient light, making it appear yellow in the scene. (This tutorial only uses ambient light, so it only sets an ambient reflectance property. A real-world application would use direct light as well as ambient light, and should set therefore set diffuse and specular reflectance properties as well.) After preparing the material properties, the code applies them in the device by calling the IDirect3DDevice7::SetMaterial method.

Note  When using textures, the object material is usually omitted or colored white.

After the current material is selected, all polygons will be rendered using this material. However, before anything in the scene will be visible you need to provide some light. The code in the Triangle application sets an ambient light level by calling the IDirect3DDevice7::SetRenderState method with the D3DRENDERSTATE_AMBIENT render state for white ambient light:

    // The ambient lighting value is another state to set. Here, we are turning
    // ambient lighting on to full white.
    pd3dDevice->SetRenderState( D3DRENDERSTATE_AMBIENT, 0xffffffff );

Now that the geometry, material, and initial lighting parameters are set, the sample moves on to setting up the transformation matrices. This is covered in Step 3.3: Prepare and Set Transformation Matrices.