Microsoft DirectX 9.0

Specular Lighting

Modeling specular reflection requires that the system not only know the direction that light is traveling, but also the direction to the viewer's eye. The system uses a simplified version of the Phong specular-reflection model, which employs a halfway vector to approximate the intensity of specular reflection.

The default lighting state does not calculate specular highlights. To enable specular lighting, be sure to set D3DRS_SPECULARENABLE to TRUE.

Specular Lighting Equation

Specular Lighting is described by the following equation.

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

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

ParameterDefault valueTypeDescription
Cs(0,0,0,0)D3DCOLORVALUESpecular color.
sumN/AN/ASummation of each light's specular component.
NN/AD3DVECTORVertex normal.
HN/AD3DVECTORHalf way vector. See the section on the halfway vector.
P0.0FLOATSpecular reflection power. Range is 0 to +infinity
Ls(0,0,0,0)D3DCOLORVALUELight specular color.
AttenN/AFLOATLight attenuation value. See Attenuation and Spotlight Factor.
SpotN/AFLOATSpotlight factor. See Attenuation and Spotlight Factor.

The value for Cs is either:

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

Specular components are clamped 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 D3DRS_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)

ParameterDefault valueTypeDescription
CpN/AD3DVECTORCamera position.
VpN/AD3DVECTORVertex position.
LdirN/AD3DVECTORDirection vector from vertex position to the light position.

Determining the halfway vector in this manner can be computationally intensive. As an alternative, setting D3DRS_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.


In this example, the object is colored using the scene specular light color and a material specular color. The code is shown below.

ZeroMemory( &mtrl, sizeof(mtrl) );

D3DLIGHT9 light;
ZeroMemory( &light, sizeof(light) );

vecDir = D3DXVECTOR3(0.5f, 0.0f, -0.5f);
D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir );

light.Specular.r = 1.0f;
light.Specular.g = 1.0f;
light.Specular.b = 1.0f;
light.Specular.a = 1.0f;

light.Range = 1000;
light.Falloff = 0;
light.Attenuation0 = 1;
light.Attenuation1 = 0;
light.Attenuation2 = 0;
m_pd3dDevice->SetLight( 0, &light );
m_pd3dDevice->LightEnable( 0, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE );

mtrl.Specular.r = 1.0f;
mtrl.Specular.g = 1.0f;
mtrl.Specular.b = 1.0f;
mtrl.Specular.a = 1.0f;
mtrl.Power = 20;
m_pd3dDevice->SetMaterial( &mtrl );

According to the equation, the resulting color for the object vertices is a combination of the material color and the light color.

These two images show the material color, which is gray, and the light color, which is white.

Gray sphere?img alt="White sphere" src="../../../../../Art/LightWhite.jpg">

The resulting specular highlight is shown below.

Specular highlight only

Combining the specular highlight with the ambient and diffuse lighting produces the following image. With all three types of lighting applied, this more clearly resembles a realistic object.

Specular highligh applied

Specular lighting is more intensive to calculate than diffuse lighting. It is typically used to provide visual clues about the surface material. The specular highlight varies in size and color with the material of the surface.

© 2002 Microsoft Corporation. All rights reserved.