Microsoft DirectX 8.1 (C++)

Using Lights

Setting and Retrieving Light Properties

You set lighting properties in a C++ application by preparing a D3DLIGHT8 structure and then calling the IDirect3DDevice8::SetLight method. The SetLight method accepts the index at which the device should place the set of light properties to its internal list of light properties, and the address of a prepared D3DLIGHT8 structure that defines those properties. You can call SetLight with new information as needed to update the light's illumination properties.

The system allocates memory to accommodate a set of lighting properties each time you call the SetLight method with an index that has never been assigned properties. Applications can set a number of lights, with only a subset of the assigned lights enabled at a time. Check the MaxActiveLights member of the D3DCAPS8 structure when you retrieve device capabilities to determine the maximum number of active lights supported by that device. If you no longer need a light, you can disable it or overwrite it with a new set of light properties.

The following C++ code example prepares and sets properties for a white point-light whose emitted light will not attenuate over distance.

/*
 * For the purposes of this example, the d3dDevice variable
 * is a valid pointer to an IDirect3DDevice8 interface.
 */
D3DLIGHT8 d3dLight;
HRESULT   hr;

// Initialize the structure.
ZeroMemory(&d3dLight, sizeof(D3DLIGHT8));

// Set up a white point light.
d3dLight.Type = D3DLIGHT_POINT;
d3dLight.Diffuse.r  = 1.0f;
d3dLight.Diffuse.g  = 1.0f;
d3dLight.Diffuse.b  = 1.0f;
d3dLight.Ambient.r  = 1.0f;
d3dLight.Ambient.g  = 1.0f;
d3dLight.Ambient.b  = 1.0f;
d3dLight.Specular.r = 1.0f;
d3dLight.Specular.g = 1.0f;
d3dLight.Specular.b = 1.0f;

// Position it high in the scene and behind the user.
// Remember, these coordinates are in world space, so
// the user could be anywhere in world space, too. 
// For the purposes of this example, assume the user
// is at the origin of world space.
d3dLight.Position.x = 0.0f;
d3dLight.Position.y = 1000.0f;
d3dLight.Position.z = -100.0f;

// Don't attenuate.
d3dLight.Attenuation0 = 1.0f; 
d3dLight.Range        = 1000.0f;

// Set the property information for the first light.
hr = d3dDevice->SetLight(0, &d3dLight);
if (FAILED(hr))
{
    // Code to handle the error goes here.
}

You can update a set of light properties with another call to SetLight at any time. Just specify the index of the set of light properties to update and the address of the D3DLIGHT8 structure that contains the new properties.

Note  Assigning a set of light properties to the device does not enable the light source whose properties are being added. Enable a light source by calling the IDirect3DDevice8::LightEnable method for the device.

You can retrieve all the properties for an existing light source from C++ by calling the IDirect3DDevice8::GetLight method for the device. When calling the GetLight method, pass in the first parameter the zero-based index of the light source for which the properties will be retrieved, and supply the address of a D3DLIGHT8 structure in the second parameter. The device fills the D3DLIGHT8 structure to describe the lighting properties it uses for the light source at that index.

The following code example illustrates this process.

/*
 * For the purposes of this example, the pd3dDevice variable
 * is a valid pointer to an IDirect3DDevice8 interface.
 */
HRESULT hr;
D3DLIGHT8 light;

// Get the property information for the first light.
hr = pd3dDevice->GetLight(0, &light);
if (FAILED(hr))
{
    // Code to handle the error goes here.
}

If you supply an index outside the range of the light sources assigned in the device, the GetLight method fails, returning D3DERR_INVALIDCALL.

Enabling and Disabling Lights

Once you assign a set of light properties for a light source in a scene, the light source can be activated by calling the IDirect3DDevice8::LightEnable method for the device. New light sources are disabled by default. The LightEnable method accepts two parameters. Set the first parameter to the zero-based index of the light source to be affected by the method, and set the second parameter to TRUE to enable the light or FALSE to disable it.

The following code example illustrates the use of this method by enabling the first light source in the device's list of light source properties.

/*
 * For the purposes of this example, the d3dDevice variable
 * is a valid pointer to an IDirect3DDevice8 interface.
 */
HRESULT hr;

hr = pd3dDevice->LightEnable(0, TRUE);
if (FAILED(hr))
{
    // Code to handle the error goes here.
}

Check the MaxActiveLights member of the D3DCAPS8 structure when you retrieve device capabilities to determine the maximum number of active lights supported by that device.

If you enable or disable a light that has no properties that are set with IDirect3DDevice8::SetLight, the LightEnable method creates a light source with the properties listed in following table and enables or disables it.

Member Default
Type D3DLIGHT_DIRECTIONAL
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)
Position (0, 0, 0)
Direction (0, 0, 1)
Range 0
Falloff 0
Attenuation0 0
Attenuation1 0
Attenuation2 0
Theta 0
Phi 0