An Introduction to Lighting in Direct3D
with Examples in C++ |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
struct
:
D3DXVECTOR3
struct CUSTOMVERTEX
{
D3DXVECTOR3 position;
D3DXVECTOR3 normal;
};
D3DFVF_NORMAL
// 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} };
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;
}
// 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);
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;
}
// 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);
HRESULT IDirect3DDevice9::SetRenderState(D3DRENDERSTATETYPE name, DWORD value);
D3DRS_AMBIENT
HRESULT IDirect3DDevice9::SetRenderState(D3DRENDERSTATETYPE type, DWORD value)
type
is D3DRS_LIGHTING
and the value
is TRUE
HRESULT IDirect3DDevice9::LightEnable(DWORD index, BOOL enable)
index
is the ID of the lightenable
is TRUE
or FALSE