Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

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.

Copy Code
Ambient Lighting = C<SUB>a</SUB> * (
G<SUB>a</SUB> + Atten<SUB>i</SUB> * sum[
L<SUB>ai</SUB> ])

Where:

Parameter Default value Type Description
Copy Code
C<SUB>a</SUB>

(0,0,0,0)

D3DMCOLORVALUE

Material ambient color

Copy Code
G<SUB>a</SUB>

(0,0,0,0)

D3DMCOLORVALUE

Global ambient color

Copy Code
Atten<SUB>i</SUB>

(0,0,0,0)

D3DMCOLORVALUE

Light attenuation of the ith light. See the Attenuation section below.

Copy Code
sum

N/A

N/A

Sum of the ambient light

Copy Code
L<SUB>ai</SUB>

(0,0,0,0)

D3DMVECTOR

Light ambient color of the ith light

The value for Cais either:

  • vertex color1, if D3DMRS_AMBIENTMATERIALSOURCE = D3DMMCS_COLOR1, and the first vertex color is supplied in the vertex declaration (see D3DMRENDERSTATETYPEand 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::SetMaterialas shown in the example code below.

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

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

Copy Code
Diffuse Lighting = sum[ C<SUB>d</SUB> *
L<SUB>d</SUB> *
(N<SUP>.</SUP>L<SUB>dir</SUB>) * Atten]

Parameter Default value Type Description
Copy Code
sum

N/A

N/A

Summation of each light's diffuse component.

Copy Code
C<SUB>d</SUB>

(0,0,0,0)

D3DMCOLORVALUE

Diffuse color.

Copy Code
L<SUB>d</SUB>

(0,0,0,0)

D3DMCOLORVALUE

Light diffuse color.

Copy Code
N

N/A

D3DMVECTOR

Vertex normal.

Copy Code
L<SUB>dir</SUB>

N/A

D3DMVECTOR

Direction vector from object vertex to the light.

Copy Code
Atten

N/A

float

Light attenuation. See the Attenuation section below.

The value for Cdis 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.

Copy Code
Specular Lighting = C<SUB>s</SUB> * sum[
L<SUB>s</SUB> *
(N<SUP>.</SUP>H)<SUP>P</SUP> * Atten ]

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

Parameter Default value Type Description
Copy Code
C<SUB>s</SUB>

(0,0,0,0)

D3DMCOLORVALUE

Specular color.

Copy Code
sum

N/A

N/A

Summation of each light's specular component.

Copy Code
N

N/A

D3DMVECTOR

Vertex normal.

Copy Code
H

N/A

D3DMVECTOR

Half way vector. See below.

Copy Code
P

0.0

float

Specular reflection power. Range is 0 to +infinity

Copy Code
L<SUB>d</SUB>

(0,0,0,0)

D3DMCOLORVALUE

Light specular color.

Copy Code
Atten

N/A

float

Light attenuation. See the Attenuation section below.

The value for Csis 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.

Copy Code
H = norm(norm(C<SUB>p</SUB> -
V<SUB>p</SUB>) + L<SUB>dir</SUB>)

Where:

Parameter Default value Type Description
Copy Code
C<SUB>p</SUB>

N/A

D3DMVECTOR

Camera position.

Copy Code
V<SUB>p</SUB>

N/A

D3DMVECTOR

Vertex position.

Copy Code
L<SUB>dir</SUB>

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.

Copy Code
H = norm([0,0,1] + L<SUB>dir</SUB>)

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
Copy Code
Atten<SUB>i</SUB> = 0

The distance between the light and the vertex exceeds the light's range.

Copy Code
Atten<SUB>i</SUB> = 1

The light is directional.

Copy Code
Atten<SUB>i</SUB> = 1/(att0<SUB>i </SUB>+
att1<SUB>i</SUB> * d<SUB>i </SUB>+
att2<SUB>i</SUB> *
d<SUB>i</SUB><SUP>2</SUP>)

All other cases.

Where:

Parameter Default Value Type Description Range
Copy Code
att0<SUB>i</SUB>

0.0

FLOAT

Constant attenuation factor

0 to +infinity

Copy Code
att1<SUB>i</SUB>

0.0

FLOAT

Linear attenuation factor

0 to +infinity

Copy Code
att2<SUB>i</SUB>

0.0

FLOAT

Quadratic attenuation factor

0 to +infinity

Copy Code
d<SUB>i</SUB>

N/A

FLOAT

Distance from vertex position to light position

N/A

The value att0, att1, and att2are specified by the Attenuation0, Attenuation1, and Attenuation2members of D3DMLIGHT.

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

Copy Code
d = | L<SUB>dir</SUB> |

Where:

Parameter Default Value Type Description
Copy Code
L<SUB>dir</SUB>

N/A

D3DMVECTOR

Direction vector from the vertex position to the light position.

If dis greater than the light's range, that is, the Rangemember of a D3DMLIGHTstructure, 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 Attenuation1to 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

Concepts

Lighting