Basic Lighting Formulas (Windows CE 5.0)

Send Feedback

The output of the lighting stage is the diffuse and possibly the specular colors of the vertex in ARGB format. The D3DMRS_LIGHTING render state (see D3DMRENDERSTATETYPE) controls whether the lighting computations are performed.

If the lighting computations are not enabled, the vertices color values are computed as follows:

  • D3DMRS_COLORVERTEX is ignored.
  • If the diffuse vertex color is present then it is passed on through the rendering pipeline, otherwise the diffuse color is the default diffuse color value 0xFFFFFFFF (white) is used.
  • If the specular vertex color is already present then it is passed on through the rendering pipeline, otherwise the specular color is the default specular color 0 (black) is used.

If the lighting computations are enabled, then the vertex colors are computed and then clamped (0.0 to 1.0) and scaled (0 to 255) before being passed to the rasterizer.

If vertex normals are not specified, all of the dot products involving the vertex normal are set to 0 and the lighting computation is still performed.

Ambient Lighting

The ambient lighting for a scene is described by the following equation.

Ambient Lighting = Ca * ( Ga + Atteni * sum[ Lai ])

Where:

Parameter Default value Type Description
Ca
(0,0,0,0) D3DMCOLORVALUE Material ambient color
Ga
(0,0,0,0) D3DMCOLORVALUE Global ambient color
Atteni
(0,0,0,0) D3DMCOLORVALUE Light attenuation of the ith light. See the Attenuation section below.
sum
N/A N/A Sum of the ambient light
Lai
(0,0,0,0) D3DMVECTOR Light ambient color of the ith light

The value for Ca is either:

  • vertex color1, if D3DMRS_AMBIENTMATERIALSOURCE = D3DMMCS_COLOR1, and the first vertex color is supplied in the vertex declaration (see D3DMRENDERSTATETYPE and D3DMMATERIALCOLORSOURCE).
  • vertex color2, if D3DMRS_AMBIENTMATERIALSOURCE = D3DMMCS_COLOR2, and the second vertex color is supplied in vertex declaration.
  • material ambient color.

Note   If either D3DMRS_AMBIENTMATERIALSOURCE option is used, and the vertex color is not provided, then the material ambient color is used.

To use the material ambient color, use IDirect3DMobileDevice::SetMaterial as shown in the example code below.

Ga is the global ambient color, which is set by the D3DMRS_AMBIENT render state. There is one global ambient color in a Direct3D Mobile scene. This parameter is not associated with a Direct3D Mobile light object.

Lai is the ambient color of the ith light in the scene. Each Direct3D Mobile light has a set of properties, one of which is the ambient color. The term, sum[ Lai ] is a sum of all the ambient colors in the scene.

Diffuse Lighting

Diffuse lighting is described by the following equation.

Diffuse Lighting = sum[ Cd * Ld * (N.Ldir) * Atten]
Parameter Default value Type Description
sum
N/A N/A Summation of each light's diffuse component.
Cd
(0,0,0,0) D3DMCOLORVALUE Diffuse color.
Ld
(0,0,0,0) D3DMCOLORVALUE Light diffuse color.
N
N/A D3DMVECTOR Vertex normal
Ldir
N/A D3DMVECTOR Direction vector from object vertex to the light.
Atten
N/A float Light attenuation. See the Attenuation section below.

The value for Cd is either:

  • vertex color1, if D3DMRS_DIFFUSEMATERIALSOURCE = D3DMCS_COLOR1, and the first vertex color is supplied in the vertex declaration.
  • vertex color2, if D3DMRS_DIFFUSEMATERIALSOURCE = D3DMCS_COLOR2, and the second vertex color is supplied in the vertex declaration.
  • material diffuse color

Note   If either DIFFUSEMATERIALSOURCE option is used, and the vertex color is not provided, the material diffuse color is used.

Diffuse components are limited to be from 0 to 255, after all lights are processed and interpolated separately. The resulting diffuse lighting value is a combination of the ambient and diffuse light values.

Specular Lighting

Specular Lighting is described by the following equation.

Specular Lighting = Cs * sum[ Ls * (N.H)P * Atten ]

The following table identifies the variables, their types, and their ranges.

Parameter Default value Type Description
Cs
(0,0,0,0) D3DMCOLORVALUE Specular color.
sum
N/A N/A Summation of each light's specular component.
N
N/A D3DMVECTOR Vertex normal.
H
N/A D3DMVECTOR Half way vector. See below.
P
0.0 float Specular reflection power. Range is 0 to +infinity
Ld
(0,0,0,0) D3DMCOLORVALUE Light specular color.
Atten
N/A float Light attenuation. See the Attenuation section below.

The value for Cs is either:

  • vertex color1, if D3DMRS_SPECULARMATERIALSOURCE is D3DMMCS_COLOR1 , and the first vertex color is supplied in the vertex declaration.
  • vertex color2, if D3DMRS_SPECULARMATERIALSOURCE is D3DMMCS_COLOR2, and the second vertex color is supplied in the vertex declaration.
  • material specular color

Note   If either specular material source option is used and the vertex color is not provided, then the material specular color is used.

Specular components are limited to be from 0 to 255, after all lights are processed and interpolated separately.

The Halfway Vector

The halfway vector, H ,exists midway between two vectors: the vector from an object vertex to the light source, and the vector from an object vertex to the camera position. Microsoft Direct3D provides two ways to compute the halfway vector. When D3DMRS_LOCALVIEWER is set to TRUE, the system calculates the halfway vector using the position of the camera and the position of the vertex, along with the light's direction vector. The following formula illustrates this.

H = norm(norm(Cp - Vp) + Ldir)

Where:

Parameter Default value Type Description
Cp
N/A D3DMVECTOR Camera position.
Vp
N/A D3DMVECTOR Vertex position.
Ldir
N/A D3DMVECTOR Direction vector from vertex position to the light position.

Determining the halfway vector in this manner can be computationally intensive. As an alternative, setting D3DMRS_LOCALVIEWER = FALSE instructs the system to act as though the viewpoint is infinitely distant on the z-axis. This is reflected in the following formula.

H = norm([0,0,1] + Ldir)

This setting is less computationally intensive, but much less accurate, so it is best used by applications that use orthogonal projection.

Attenuation

The attenuation of a light depends on the type of light and the distance between the light and the vertex position. To calculate attenuation, use one of the following equations.

Equation Condition
Atteni = 0
The distance between the light and the vertex exceeds the light's range
Atteni = 1
The light is directional.
Atteni = 1/(att0i + att1i * di + att2i * di2)
All other cases.

Where:

Parameter Default Value Type Description Range
att0i
0.0 FLOAT Constant attenuation factor 0 to +infinity
att1i
0.0 FLOAT Linear attenuation factor 0 to +infinity
att2i
0.0 FLOAT Quadratic attenuation factor 0 to +infinity
di
N/A FLOAT Distance from vertex position to light position N/A

The value att0, att1, and att2 are specified by the Attenuation0, Attenuation1, and Attenuation2 members of D3DMLIGHT.

The distance between the light and the vertex position is always positive.

d = | Ldir |

Where:

Parameter Default Value Type Description
Ldir
N/A D3DMVECTOR Direction vector from the vertex position to the light position.

If d is greater than the light's range, that is, the Range member of a D3DMLIGHT structure, Microsoft Direct3D Mobile makes no further attenuation calculations and applies no effects from the light to the vertex.

The attenuation constants act as coefficients in the formula — you can produce a variety of attenuation curves by making simple adjustments to them. You can set Attenuation1 to 1.0 to create a light that doesn't attenuate but is still limited by range, or you can experiment with different values to achieve various attenuation effects.

The attenuation at the maximum range of the light is not 0.0. To prevent lights from suddenly appearing when they are at the light range, an application can increase the light range. Or, the application can set up attenuation constants so that the attenuation factor is close to 0.0 at the light range. The attenuation value is multiplied by the red, green, and blue components of the light's color to scale the light's intensity as a factor of the distance light travels to a vertex.

See Also

Lighting

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.