After you create a light object, you must set the light's properties by preparing a D3DLIGHT2 structure and then calling the IDirect3DLight::SetLight method. The SetLight method accepts the address of a prepared D3DLIGHT2 structure as its only parameter. You can call SetLight with new information as needed to update the light's illumination properties.
The following code sets up properties for a white point light that doesn't attenuate over distance, then calls the SetLight method to put the properties into effect:
/*
* For the purposes of this example, the g_lpD3DLight variable
* is a valid pointer to an IDirect3D3 interface.
*/
D3DLIGHT2 g_light;
HRESULT hr;
// Initialize the structure.
ZeroMemory(&g_light, sizeof(D3DLIGHT2));
g_light.dwSize = sizeof(D3DLIGHT2); // MUST set the size!
// Set up for a white point light.
g_light.dltType = D3DLIGHT_POINT;
g_light.dcvColor.r = 1.0f;
g_light.dcvColor.g = 1.0f;
g_light.dcvColor.b = 1.0f;
// Position it high in the scene, and behind the viewer.
// (Remember, these coordinates are in world space, so
// the "viewer" could be anywhere in world space, too.
// For the purposes of this example, assume the viewer
// is at the origin of world space.)
g_light.dvPosition.x = 0.0f;
g_light.dvPosition.y = 1000.0f;
g_light.dvPosition.z = -100.0f;
// Don't attenuate.
g_light.dvAttenuation0 = 1.0f;
g_light.dvRange = D3DLIGHT_RANGE_MAX;
// Make the light active light.
g_light.dwFlags = D3DLIGHT_ACTIVE;
// Set the property info for this light.
// We have to cast the LPD3DLIGHT2 to be
// an LPD3DLIGHT in order to compile.
//
// (See the following note for details.)
hr = g_lpD3DLight->SetLight((LPD3DLIGHT)&g_light);
if (SUCCEEDED(hr))
{
// Add the light to the viewport.
}
else
return hr;
Note The IDirect3DLight::SetLight method checks the dwSize member of the structure specified in the lpLight parameter to determine whether you are using a D3DLIGHT2 or D3DLIGHT structure. The two structures are interpreted differently. The D3DLIGHT2 structure supersedes the D3DLIGHT structure, using the newer structure will provide the most reliable lighting. For backward compatibility, the SetLight method's parameter list is unchanged. As a result, you must cast the address of the D3DLIGHT2 structure you provide to the LPD3DLIGHT data type to avoid compiler errors.
(You can update a light's properties by a subsequent call to SetLight at any time, you need not add the light to the viewport each time.)
After choosing the light type by setting its properties, you can add the light to a viewport to complete the process of preparing a light for rendering. For more information, see Adding a Light to a Viewport.