Platform SDK: DirectX

Step 3.1: Set Up Material and Initial Lighting States

[C++]

This section pertains only to application development in Visual Basic. See Direct3D Immediate Mode C/C++ Tutorials.

[Visual Basic]

After you create the basic 3-D rendering objects (a DirectDraw object, a rendering device, and a viewport), you nearly have all that you need to render a simple scene. The next thing to do is to set some initial lighting states and to create and configure a material. If necessary, these settings can all be changed later. For an introduction to these concepts, see Lighting and Materials.

The code applies an initial lighting state to the rendering device, g_d3dDevice, by calling the Direct3DDevice7.SetRenderState method.

    g_d3dDevice.SetRenderState D3DRENDERSTATE_AMBIENT, _
                               g_dx.CreateColorRGBA(1#, 1#, 1#, 1#)

In its first parameter, SetRenderState passes in the D3DRENDERSTATE_AMBIENT flag, associating the call with the ambient light color setting for the scene. The second argument uses the DirectX7.CreateColorRGBA method to set the red, green, and blue color values of the ambient light to 1.0, 1.0, and 1.0, respectively. The resulting light emits a white light. Lights do not use the alpha component, which is also set to 1.0 in this case.

Use a D3DMATERIAL7 type to specify material properties:

    Dim mtrl As D3DMATERIAL7
 
    mtrl.Ambient.r = 1#: mtrl.Ambient.g = 1#: mtrl.Ambient.b = 0#
    
    ' Commit the material to the device.
    g_d3dDevice.SetMaterial mtrl

When created, a new material has no properties and cannot be used in rendering. The preceding code sets material properties in a D3DMATERIAL7 type, describing a material that will reflect the red and green components of ambient light, making the material appear yellow in the scene. (The Triangle 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 therefore set diffuse and specular reflectance properties as well.)

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

Now that the material, and initial lighting parameters are set, the Triangle tutorial code sets up the transformation matrices. This is covered in Step 3.2: Prepare and Set Transformation Matrices.