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


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Parts of the Process
Specifying Normal Vectors
Specifying Normal Vectors (cont.)

An Example

directx9examples/lighting/cube/cube.cpp (Fragment: vertices)
        // Specify the struct for a vertex
struct CUSTOMVERTEX
{
    float x, y, z;
        float nx, ny, nz;
};

// Specify the flexible vertex format (FVF) for the struct above
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL)


// Color Definitions
DWORD colorBlue   = 0xFF0000FF;
DWORD colorGold   = 0xFFC2A14D; // JMU Gold
DWORD colorGray   = 0xFFAAAAAA;
DWORD colorGreen  = 0xFF00FF00;
DWORD colorPurple = 0xFF450084; // JMU Purple
DWORD colorRed    = 0xFFFF0000;
DWORD colorYellow = 0xFFFFFF00;

// Create the vertex elements
CUSTOMVERTEX vertices[] =
{
        {-1.0f, 1.0f,-1.0f,  0.0f,  0.0f, -1.0f}, // Front
        { 1.0f, 1.0f,-1.0f,  0.0f,  0.0f, -1.0f},
        {-1.0f,-1.0f,-1.0f,  0.0f,  0.0f, -1.0f},
        { 1.0f,-1.0f,-1.0f,  0.0f,  0.0f, -1.0f},
        
        {-1.0f, 1.0f, 1.0f,  0.0f,  0.0f,  1.0f}, // Back
        {-1.0f,-1.0f, 1.0f,  0.0f,  0.0f,  1.0f},
        { 1.0f, 1.0f, 1.0f,  0.0f,  0.0f,  1.0f},
        { 1.0f,-1.0f, 1.0f,  0.0f,  0.0f,  1.0f},
        
        {-1.0f, 1.0f, 1.0f,  0.0f,  1.0f,  0.0f}, // Top
        { 1.0f, 1.0f, 1.0f,  0.0f,  1.0f,  0.0f},
        {-1.0f, 1.0f,-1.0f,  0.0f,  1.0f,  0.0f},
        { 1.0f, 1.0f,-1.0f,  0.0f,  1.0f,  0.0f},
        
        {-1.0f,-1.0f, 1.0f,  0.0f, -1.0f,  0.0f}, // Bottom
        {-1.0f,-1.0f,-1.0f,  0.0f, -1.0f,  0.0f},
        { 1.0f,-1.0f, 1.0f,  0.0f, -1.0f,  0.0f},
        { 1.0f,-1.0f,-1.0f,  0.0f, -1.0f,  0.0f},

        { 1.0f, 1.0f,-1.0f,  1.0f,  0.0f,  0.0f}, // Right
        { 1.0f, 1.0f, 1.0f,  1.0f,  0.0f,  0.0f},
        { 1.0f,-1.0f,-1.0f,  1.0f,  0.0f,  0.0f},
        { 1.0f,-1.0f, 1.0f,  1.0f,  0.0f,  0.0f},
        
        {-1.0f, 1.0f,-1.0f, -1.0f,  0.0f,  0.0f}, // Left
        {-1.0f,-1.0f,-1.0f, -1.0f,  0.0f,  0.0f},
        {-1.0f, 1.0f, 1.0f, -1.0f,  0.0f,  0.0f},
        {-1.0f,-1.0f, 1.0f, -1.0f,  0.0f,  0.0f}
};

        
Specifying Material Properties
Specifying Material Properties (cont.)

An Example

directx9examples/lighting/cube/cube.cpp (Fragment: material)
                // Specify the properties for brass
    D3DMATERIAL9 brass = {
                      {0.78f, 0.57f, 0.11f, 1.00f}, // Diffuse RGBA
                      {0.33f, 0.22f, 0.03f, 1.00f}, // Ambient RGBA
                      {0.99f, 0.94f, 0.81f, 1.00f}, // Specular RGBA
                      {0.00f, 0.00f, 0.00f, 1.00f}, // Emissive RGBA
                      27.89f
                     };
                                         
        // Tell the device to make brasas the current material
        d3dDevice->SetMaterial(&brass);
        
Creating and Positioning Light Sources
Light Sources (cont.)

An Example

directx9examples/lighting/cube/cube.cpp (Fragment: lightsource)
                // Declare a light source
        D3DLIGHT9   light;

        // Clear the memory since we aren't setting all components
        ZeroMemory( &light, sizeof(light) );

        // Initialize the light source
        light.Type      = D3DLIGHT_DIRECTIONAL;
        light.Diffuse.r = 1.0f;
        light.Diffuse.g = 1.0f;
        light.Diffuse.b = 1.0f;
        light.Range     = 1.0f;

        // Declare a direction vector
        D3DXVECTOR3 direction;

        // Change the direction over time
        direction = D3DXVECTOR3(cosf(timeGetTime()/360.0f),
                                    0.0f,
                                        sinf(timeGetTime()/360.0f));

        // Normalize the direction vector
        D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &direction);

        // Tell the device to use the light source
        d3dDevice->SetLight(0, &light);
        
Specifying the Lighting Model
Enabling Lighting
Light Sources (cont.)

An Example of Setting-Up Lighting

directx9examples/lighting/cube/cube.cpp (Fragment: setupLighting)
                // Set the lighting properties
        d3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
        d3dDevice->SetRenderState(D3DRS_AMBIENT, 0x00AAAAAA);
        
Enabling Lights
Light Sources (cont.)

An Example of Enabling a Light

directx9examples/lighting/cube/cube.cpp (Fragment: enable)
                // Enable lighhting
        d3dDevice->LightEnable(0, TRUE);