DirectX SDK

Specular Reflection Model

[C++]

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. This halfway vector exists midway between the vector to the light source and the vector to the eye. Direct3D provides applications with two ways to compute the halfway vector, controlled by the D3DRENDERSTATE_LOCALVIEWER render state. If D3DRENDERSTATE_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:

In the preceding formula, norm() is an operator that normalizes an input vector, VC is the vector that exists from the position of the vertex to the position of the viewpoint (or eye), and Ld is the light's direction vector.

Determining the halfway vector in this manner is somewhat computationally intensive. An alternative would be to set D3DRENDERSTATE_LOCALVIEWER to FALSE, which instructs the system to act as though the viewpoint is infinitely distant on the z-axis. This setting is less computationally expensive, but much less accurate, so it is best used by applications that use orthogonal projection. When D3DRENDERSTATE_LOCALVIEWER is set to FALSE, Direct3D determines the halfway vector by the following formula:

This formula is similar to the first formula, but substitutes the vector I (0, 0, -1)—which points at a viewpoint infinitely distant on the z-axis—instead of computing the vector VC.

After determining the halfway vector, H, the system uses the following formula to compute specular reflection:

In the preceding formula, Rs is the specular reflectance, N in the vertex normal, H is the halfway vector, and p is the specular reflection power of the current material (as specified by the dvPower member of the material's D3DMATERIAL7 structure). Vector H is normalized, and vector N is normalized only if the D3DRENDERSTATE_NORMALIZENORMALS render state is enabled.

Like the diffuse reflectance formula, this formula produces values that range from -1.0 to 1.0, which are clamped to the range of 0.0 to 1.0 and used to scale the light reflecting from the vertex. Also similar to the diffuse reflection model, the remaining light is applied to a formula that derives the specular component at that vertex:

In the preceding formula, Sv is the specular color being computed, A is the light from a single light source that has been attenuated for distance and spotlight effects (see Light Attenuation Over Distance and Spotlight Falloff Model). The Rs variable is the previously calculated specular reflectance, Vs is the selected specular component for the vertex, and Ls is the specular light color output by the light.

If the D3DRENDERSTATE_COLORVERTEX render state is enabled, the system selects the color source for V based on the value of the D3DRENDERSTATE_SPECULARMATERIALSOURCE render state. This render state can be set to a member of the D3DMATERIALCOLORSOURCE enumerated type to cause the system to use the current material, or one of the color components for the vertex, as the color source.

For more information, see Diffuse Reflection Model.

[Visual Basic]

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. This halfway vector exists midway between the vector to the light source and the vector to the eye. Direct3D provides applications with two ways to compute the halfway vector, controlled by the D3DRENDERSTATE_LOCALVIEWER render state. If D3DRENDERSTATE_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:

In the preceding formula, norm() is an operator that normalizes an input vector, VC is the vector that exists from the position of the vertex to the position of the viewpoint (or eye), and Ld is the light's direction vector.

Determining the halfway vector in this manner is somewhat computationally intensive. An alternative would be to set D3DRENDERSTATE_LOCALVIEWER to False, which instructs the system to act as though the viewpoint is infinitely distant on the z-axis. This setting is less computationally expensive, but much less accurate, so it is best used by applications that use orthogonal projection. When D3DRENDERSTATE_LOCALVIEWER is set to False, Direct3D determines the halfway vector by the following formula:

This formula is similar to the first formula, but substitutes the vector I (0, 0, -1)—which points at a viewpoint infinitely distant on the z-axis—instead of computing the vector VC.

After determining the halfway vector, H, the system uses the following formula to compute specular reflection:

In the preceding formula, Rs is the specular reflectance, N in the vertex normal, H is the halfway vector, and p is the specular reflection power of the current material (as specified by the power member of the material's D3DMATERIAL7 type). Vector H is normalized, and vector N is normalized only if the D3DRENDERSTATE_NORMALIZENORMALS render state is enabled.

Like the diffuse reflectance formula, this formula produces values that range from -1.0 to 1.0, which are clamped to the range of 0.0 to 1.0 and used to scale the light reflecting from the vertex. Also similar to the diffuse reflection model, the remaining light is applied to a formula that derives the specular component at that vertex:

In the preceding formula, Sv is the specular color being computed, A is the light from a single light source that has been attenuated for distance and spotlight effects (see Light Attenuation Over Distance and Spotlight Falloff Model). The Rs variable is the previously calculated specular reflectance, Vs is the selected specular component for the vertex, and Ls is the specular light color output by the light.

If the D3DRENDERSTATE_COLORVERTEX render state is enabled, the system selects the color source for V based on the value of the D3DRENDERSTATE_SPECULARMATERIALSOURCE render state. This render state can be set to a member of the CONST_D3DMATERIALCOLORSOURCE enumerated type to cause the system to use the current material, or one of the color components for the vertex, as the color source.

For more information, see Diffuse Reflection Model.