Basic Lighting Formula (Windows Embedded CE 6.0)
1/6/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.
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 |
---|---|---|---|
|
(0,0,0,0) |
Material ambient color |
|
|
(0,0,0,0) |
D3DMCOLORVALUE |
Global ambient color |
|
(0,0,0,0) |
D3DMCOLORVALUE |
Light attenuation of the ith light. See the Attenuation section below. |
|
N/A |
N/A |
Sum of the ambient light |
|
(0,0,0,0) |
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[ C<SUB>d</SUB> * L<SUB>d</SUB> * (N<SUP>.</SUP>L<SUB>dir</SUB>) * Atten]
Parameter | Default value | Type | Description |
---|---|---|---|
|
N/A |
N/A |
Summation of each light's diffuse component. |
|
(0,0,0,0) |
Diffuse color. |
|
|
(0,0,0,0) |
D3DMCOLORVALUE |
Light diffuse color. |
|
N/A |
Vertex normal. |
|
|
N/A |
D3DMVECTOR |
Direction vector from object vertex to the light. |
|
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 = 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 |
---|---|---|---|
|
(0,0,0,0) |
Specular color. |
|
|
N/A |
N/A |
Summation of each light's specular component. |
|
N/A |
Vertex normal. |
|
|
N/A |
D3DMVECTOR |
Half way vector. See below. |
|
0.0 |
float |
Specular reflection power. Range is 0 to +infinity |
|
(0,0,0,0) |
D3DMCOLORVALUE |
Light specular color. |
|
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(C<SUB>p</SUB> - V<SUB>p</SUB>) + L<SUB>dir</SUB>)
Where:
Parameter | Default value | Type | Description |
---|---|---|---|
|
N/A |
D3DMVECTOR |
Camera position. |
|
N/A |
D3DMVECTOR |
Vertex position. |
|
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] + 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 |
---|---|
|
The distance between the light and the vertex exceeds the light's range. |
|
The light is directional. |
|
All other cases. |
Where:
Parameter | Default Value | Type | Description | Range |
---|---|---|---|---|
|
0.0 |
FLOAT |
Constant attenuation factor |
0 to +infinity |
|
0.0 |
FLOAT |
Linear attenuation factor |
0 to +infinity |
|
0.0 |
FLOAT |
Quadratic attenuation factor |
0 to +infinity |
|
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 = | L<SUB>dir</SUB> |
Where:
Parameter | Default Value | Type | Description |
---|---|---|---|
|
N/A |
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.