Microsoft DirectX 8.1 (Visual Basic)

Using Lights

Setting and Retrieving Light Properties

Your Microsoft Visual Basic® application sets lighting properties by preparing a D3DLIGHT8 type and then calling the Direct3DDevice8.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 type 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 had properties assigned. 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 type 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 code example prepares and sets properties for a white point-light whose emitted light will not attenuate over distance.

'
' For this example, the d3dDevice variable contains a valid reference
' to a Direct3DDevice8 object.
'
Dim LightDesc As D3DLIGHT8
Dim c As D3DCOLORVALUE
Dim vPos As D3DVECTOR

' Use the same color settings for all emitted light color.
With c
    .r = 1#: .g = 1#: .b = 1#
    .a = 1# ' The alpha component isn't used for lights.
End With

With vPos
    .x = 0: .y = 1000: .z = -100
End With

With LightDesc
    .Type = D3DLIGHT_POINT
    .Position = vPos
    .Ambient = c: .diffuse = c: .specular = c
    .Attenuation0 = 1# ' Don't attenuate the light
End With

d3dDevice.SetLight 0, LightDesc

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.

A Microsoft® Visual Basic® application retrieves the properties for an existing light source by calling the Direct3DDevice8.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 a variable of type D3DLIGHT8 as the second parameter. The device fills the D3DLIGHT8 type 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 d3dDevice variable contains
' a valid reference to a Direct3DDevice8 object.
'
Dim lightDesc As D3DLIGHT8

' Get the property information for the first light.
Call d3dDevice.GetLight(0, lightDesc)
If Err.Number <> D3D_OK Then
    ' Code to handle the error goes here.
End If

If you supply an index outside the range of the light sources assigned in the device, the GetLight method fails, and the value of Err.Number is 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 Direct3DDevice8.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 1 to enable the light or 0 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 contains
' a valid reference to Direct3DDevice8 object.
'
On Local Error Resume Next

Call d3dDevice.LightEnable(0, 1)
If Err.Number <> D3D_OK Then
    'Code to handle the error goes here.
End If

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 Direct3DDevice8.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