- Forward


An Introduction to Lighting in Direct3D
with Examples in C++


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Parts of the Process
Back SMYC Forward
  • Specify normal vectors
  • Specify material properties
  • Create and position light sources
  • Specify properties of the lighting model
  • Enable lighting and light sources
Specifying Normal Vectors
Back SMYC Forward
  • The Custom Vertex struct:
    • Can use a D3DXVECTOR3
    • struct CUSTOMVERTEX
      {
          D3DXVECTOR3 position;
          D3DXVECTOR3 normal;
      };
             	
  • Set the Flexible Vertex Format (FVF):
    • Use the bitflag D3DFVF_NORMAL
Specifying Normal Vectors (cont.)
Back SMYC Forward

An Example

directx9examples/lighting/cube/cube.cpp (Fragment: vertices)
 
Specifying Material Properties
Back SMYC Forward
  • D3DMATERIAL9:
    • typedef struct D3DMATERIAL9 
      {
        D3DCOLORVALUE diffuse; 
        D3DCOLORVALUE ambient; 
        D3DCOLORVALUE specular; 
        D3DCOLORVALUE emissive; 
        float         exponent;
      }
      
  • D3DCOLORVALUE:
    • typedef struct D3DCOLORVALUE 
      {
        float r; 
        float g; 
        float b; 
        float alpha;
      } 
      
Specifying Material Properties (cont.)
Back SMYC Forward

An Example

directx9examples/lighting/cube/cube.cpp (Fragment: material)
 
Creating and Positioning Light Sources
Back SMYC Forward
  • D3DLIGHT9:
    • typedef struct D3DLIGHT9 
      {
        D3DLIGHTTYPE  type;
        D3DCOLORVALUE diffuse;
        D3DCOLORVALUE specular;
        D3DCOLORVALUE ambient;
        D3DVECTOR     position;
        D3DVECTOR     direction;
        float         range;
        float         falloff;
        float         attenuation0;
        float         attenuation1;
        float         attenuation2;
        float         theta;
        float         phi;
      }
      
  • Type of Light:
    • Point
    • Directional
    • Spotlight
Light Sources (cont.)
Back SMYC Forward

An Example

directx9examples/lighting/cube/cube.cpp (Fragment: lightsource)
 
Specifying the Lighting Model
Back SMYC Forward
  • The Function:
    • HRESULT IDirect3DDevice9::SetRenderState(D3DRENDERSTATETYPE name, DWORD value);
  • An Important Name-Value Pair:
    • Name: D3DRS_AMBIENT
    • Value: A color
Enabling Lighting
Back SMYC Forward
  • The Method:
    • HRESULT IDirect3DDevice9::SetRenderState(D3DRENDERSTATETYPE type, DWORD value)
  • The Name-Value Pair:
    • type is D3DRS_LIGHTING and the value is TRUE
Light Sources (cont.)
Back SMYC Forward

An Example of Setting-Up Lighting

directx9examples/lighting/cube/cube.cpp (Fragment: setupLighting)
 
Enabling Lights
Back SMYC Forward
  • The Method:
    • HRESULT IDirect3DDevice9::LightEnable(DWORD index, BOOL enable)
  • The Parameters:
    • index is the ID of the light
    • enable is TRUE or FALSE
Light Sources (cont.)
Back SMYC Forward

An Example of Enabling a Light

directx9examples/lighting/cube/cube.cpp (Fragment: enable)
 
There's Always More to Learn
Back -