Setting Light Properties
A version of this page is also available for
4/8/2010
You set lighting properties in a C++ application by preparing a D3DMLIGHT structure and then calling the IDirect3DMobileDevice::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 D3DMLIGHT 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 D3DMCAPS 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 d3dmDevice variable
* is a valid pointer to an IDirect3DMobileDevice interface.
*/
D3DMLIGHT d3dmLight;
HRESULT hr;
// Initialize the structure.
ZeroMemory(&d3dmLight, sizeof(D3DMLIGHT));
// Set up a white point light.
d3dmLight.Type = D3DLIGHT_POINT;
d3dmLight.Diffuse.r = 1.0f;
d3dmLight.Diffuse.g = 1.0f;
d3dmLight.Diffuse.b = 1.0f;
d3dmLight.Ambient.r = 1.0f;
d3dmLight.Ambient.g = 1.0f;
d3dmLight.Ambient.b = 1.0f;
d3dmLight.Specular.r = 1.0f;
d3dmLight.Specular.g = 1.0f;
d3dmLight.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.
d3dmLight.Position.x = 0.0f;
d3dmLight.Position.y = 1000.0f;
d3dmLight.Position.z = -100.0f;
// Do not attenuate.
d3dmLight.Attenuation0 = 1.0f;
d3dmLight.Range = 1000.0f;
// Set the property information for the first light.
hr = d3dmDevice->SetLight(0, &d3dmLight);
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 D3DMLIGHT 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 IDirect3DMobileDevice::LightEnable method for the device.