Game Development Reference
In-Depth Information
Like initializing a D3DMATERIAL9 structure, initializing a D3DLIGHT9
structure can also be tedious when you only want a simple light. We
add the following functions to the d3dUtility.h/cpp files to initialize sim-
ple lights:
namespace d3d
{
.
.
.
D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction,
D3DXCOLOR* color);
D3DLIGHT9 InitPointLight(D3DXVECTOR3* position,
D3DXCOLOR* color);
D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position,
D3DXVECTOR3* direction,
D3DXCOLOR* color);
}
The implementation of these functions is straightforward. We will only
show the implementation of InitDirectionalLight . The others
are similar:
D3DLIGHT9 d3d::InitDirectionalLight(D3DXVECTOR3* direction,
D3DXCOLOR* color)
{
D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Ambient = *color * 0.4f;
light.Diffuse = *color;
light.Specular = *color * 0.6f;
light.Direction = *direction;
return light;
}
Then to create a directional light that runs parallel with the x-axis in
the positive direction and emits white light, we would write:
D3DXVECTOR3 dir(1.0f, 0.0f, 0.0f);
D3DXCOLOR c = d3d::WHITE;
D3DLIGHT9 dirLight = d3d::InitDirectionalLight(&dir, &c);
After we have initialized a D3DLIGHT9 instance, we need to register
with an internal list of lights that Direct3D maintains. We do this like
so:
Device->SetLight(
0, // element in the light list to set, range is 0-maxlights
&light);// address of the D3DLIGHT9 structure to set
Search WWH ::




Custom Search