Microsoft DirectX 8.0 (C++)

Mathematics of Direct3D Lighting

This section describes the mechanics and implementation details behind the Microsoft® Direct3D® lighting engine. Direct3D models illumination by estimating how light behaves in nature. The Direct3D light model keeps track of light color, the direction and distance that light travels, the position of the viewer, and the characteristics of the current material to compute two color components for each vertex in a face. Direct3D uses these color components to compute the color it draws while rasterizing the pixels of a face.

Note  All computations are made in model space by transforming the light source's position and direction, along with the camera position to model space using the inverse of the world matrix, then they are back transformed. As a result, if the world or view matrices introduce nonuniform scaling, the resultant lighting might be inaccurate.

This section presents a technical look at the formulas that Direct3D uses to come up with diffuse and specular components. By understanding the approach of Direct3D, you will be better equipped to decide if the Direct3D light model suits your needs. The Direct3D light model was designed to be accurate, efficient, and easy to use. However, if the formulas used by Direct3D don't suit your needs, you can implement your own light model, bypassing the Direct3D lighting module altogether. The following topics are discussed.

The parameters used for these formulas are listed in the tables below. Each table has its corresponding default value, type, and a range of accepted values.

The following table lists the position parameters.

Parameter Default value Type Description
Pe (0,0,0) D3DVECTOR Camera position in camera space.
Po N/A D3DVECTOR Position of current model origin (0,0,0,1) in the camera space. This is the fourth row of the Mw*Mv matrix.
Mw N/A D3DMATRIX World matrix, set by D3DTRANSFORMSTATE_VIEW.
Mv N/A D3DMATRIX View matrix, set by D3DTRANSFORMSTATE_VIEW.
Mwv N/A D3DMATRIX Mw*Mv.
Halfway N/A D3DVECTOR Normalized vector, used to compute specular reflection.
La (0.0, 0.0, 0.0, 0.0) D3DCOLORVALUE Ambient color in the light state. Set by D3DRENDERSTATE_AMBIENT.
V N/A D3DVECTOR Vertex position in camera space.
N N/A D3DVECTOR Normalized vertex normal in camera space.
Vcd N/A D3DCOLORVALUE Vertex diffuse color.
Vcs N/A D3DCOLORVALUE Vertex specular color.

The following table lists the material parameters.

Parameter Default value Type Description
Ma (0.0, 0.0, 0.0, 0.0) D3DCOLORVALUE Ambient color.
Md (255, 255, 255, 255) D3DCOLORVALUE Diffuse color.
Ms (0.0, 0.0, 0.0, 0.0) D3DCOLORVALUE Specular color.
Me (0.0, 0.0, 0.0, 0.0) D3DCOLORVALUE Emissive color.
Mp 0.0 D3DVALUE Specular exponent. Range: (-∞,+∞)

The following table lists the light source i parameters.

Parameter Default value Type Description
LpI (0.0, 0.0, 0.0) D3DVECTOR Position in camera space.
LdI (0.0, 0.0, 1.0) D3DVECTOR Direction to the light in the camera space.
LrI 0.0 D3DVALUE Distance range. Range: [0.0, D3DLIGHT_RANGE_MAX]
LcaI (0.0, 0.0, 0.0, 0.0) D3DCOLORVALUE Ambient color.
LcsI (0.0, 0.0, 0.0, 0.0) D3DCOLORVALUE Specular color.
LcI (1.0, 1.0, 1.0, 0.0) D3DCOLORVALUE Diffuse color.
att0I 0.0 D3DVALUE Constant attenuation factor. Range: (0, +∞)
att1I 0.0 D3DVALUE Linear attenuation factor. Range: (0, +∞)
att2I 0.0 D3DVALUE Quadratic attenuation factor. Range: (0, +∞)
falloffI 0.0 D3DVALUE Falloff factor. Range: (-∞, +∞)
thetaI 0.0 D3DVALUE Umbra angle of spotlight in radians. Range: [0, π)
phiI 0.0 D3DVALUE Penumbra angle of spotlight in radians. Range: [thetaI, π)