MSDN Documentation

Windows DirectX Computational Graphics

Lighting Concepts in DirectX

Last updated: October 26, 2023

Understanding lighting is fundamental to creating realistic and visually appealing 3D graphics in DirectX. Lighting models simulate how light interacts with surfaces in a scene, affecting their perceived color, brightness, and shading. This document explores the core concepts of lighting within the DirectX framework.

Basic Light Properties

Every light source in a 3D scene typically possesses several properties:

Types of Lights

DirectX supports several common types of light sources, each with distinct behaviors:

1. Ambient Light

Ambient light is a constant, uniform illumination that affects all objects in the scene equally, regardless of their position or orientation. It simulates indirect light that has bounced off multiple surfaces, providing a base level of visibility and preventing completely black areas.

AmbientLightColor = R_ambient * C_object

Where R_ambient is the ambient light color and C_object is the object's base color.

2. Diffuse Light

Diffuse light models the scattering of light from a surface, where the light reflects equally in all directions. The intensity of diffuse reflection depends on the angle between the light source's direction and the surface's normal vector. Surfaces facing the light source directly appear brightest, while those at an angle appear dimmer.

DiffuseReflection = LightColor * SurfaceColor * max(0, dot(N, L))

Where N is the surface normal vector and L is the light direction vector.

Diagram illustrating diffuse lighting: Light rays hitting a surface and scattering equally.
Diffuse reflection simulates light scattering off a surface.

3. Specular Light

Specular light simulates the highlights that appear on shiny surfaces. These highlights are brightest when the viewing direction is precisely aligned with the reflection direction of the light source. This effect makes surfaces appear glossy and highlights their curvature.

SpecularReflection = LightColor * SpecularShininess * pow(max(0, dot(R, V)), Shininess)

Where R is the reflection vector, V is the view vector, and Shininess controls the size and intensity of the highlight.

Diagram illustrating specular lighting: A sharp highlight on a curved surface.
Specular reflection creates highlights on shiny surfaces.

Lighting Models

DirectX employs various lighting models to combine these light properties and types. The most common is the Phong Reflection Model, which is a widely used empirical model for approximating lighting. It combines ambient, diffuse, and specular components:

FinalColor = Ambient + Diffuse + Specular

More advanced models like the Blinn-Phong model offer performance improvements and slightly different visual characteristics by using a half-vector instead of calculating the full reflection vector.

Surface Properties

In addition to light properties, the way surfaces interact with light is crucial. These are often defined by material properties:

Implementation in DirectX Shaders

Lighting calculations are typically performed in the vertex and pixel shaders. Vertex shaders can transform vertex positions and compute normals, while pixel shaders perform the final lighting calculations per-pixel to determine the color of each fragment.

Example Pixel Shader Snippet (HLSL)


struct PixelInput {
    float4 Position : SV_POSITION;
    float3 Normal : NORMAL;
    float2 Tex : TEXCOORD;
};

float4 AmbientColor;
float4 DiffuseColor;
float4 LightDirection; // Normalized
float4 LightColor;

float4 main(PixelInput input) : SV_TARGET {
    float3 normal = normalize(input.Normal);
    float NdotL = dot(normal, LightDirection.xyz);
    float3 diffuse = DiffuseColor.rgb * LightColor.rgb * max(0.0f, NdotL);

    float4 finalColor = AmbientColor + float4(diffuse, 1.0f);
    // Specular calculation would follow here...
    return finalColor;
}
            

Advanced Lighting Techniques

Beyond basic models, DirectX supports advanced techniques for more realistic lighting:

Mastering these lighting concepts is essential for creating immersive and believable 3D worlds within DirectX applications.