# Exercise 1

Fixed Function Diffuse Lighting and Vertex Shader Diffuse Lighting.

## Example

```float3 lightDirection = {0.5f, 0.5f, 0.0f };
float4 diffuseMaterialColor = {1.0f, 0.9f, 0.9f, 1.0f };

float4x4 worldMatrix;
float4x4 worldProjViewMatrix;

// Given: The application has set the matrices before calling this technique.

technique tec0 < string purpose = "Fixed Function Diffuse Lighting" ; >
{
pass P0
{
MaterialDiffuse  = {diffuseMaterialColor};
MaterialSpecular = {0.0f,0.0f,0.0f,0.0f};
MaterialAmbient  = {0.0f,0.0f,0.0f,0.0f};

LightType[0]      = DIRECTIONAL;
LightDiffuse[0]   = {1.0f,1.0f,1.0f,1.0f};
LightSpecular[0]  = {0.0f,0.0f,0.0f,0.0f};
LightAmbient[0]   = {0.0f,0.0f,0.0f,0.0f};
LightDirection[0] = {lightDirection};
LightRange[0]     = 100000.0f;

// Turn lighting on and use light zero
LightEnable[0]    = TRUE;
Lighting          = TRUE;

// Assign diffuse color to be used
ColorOp[0]   = SelectArg1;
ColorArg1[0] = Diffuse;
AlphaOp[0]   = SelectArg1;
AlphaArg1[0] = Diffuse;

// Only one color being used
ColorOp[1]   = Disable;
AlphaOp[1]   = Disable;

// Z-buffering to be used
ZEnable      = true;
ZWriteEnable = true;
}
}

technique tec1 < string purpose = "Vertex Shader Diffuse Lighting" ; >
{
pass p0
{
// Load matrices
VertexShaderConstant[0]  = (worldMatrix);
VertexShaderConstant[4]  = (worldProjViewMatrix);

// Material properties of object
VertexShaderConstant[9]  = (1.0f,1.0f,1.0f,1.0f);
VertexShaderConstant[10] = (0.0f,0.0f,0.0f,0.0f);
VertexShaderConstant[11] = (0.0f,0.0f,0.0f,0.0f);

// Light Properties are input from the shader application
// Diffuse
VertexShaderConstant[13] = (1.0f,1.0f,1.0f,1.0f);
// Specular
VertexShaderConstant[14] = (0.0f,0.0f,0.0f,0.0f);
// Ambient
VertexShaderConstant[15] = (0.0f,0.0f,0.0f,0.0f);
VertexShaderConstant[16] = {lightDirection};

// Assign diffuse color to be used
ColorOp[0]   = SelectArg1;
ColorArg1[0] = Diffuse;
AlphaOp[0]   = SelectArg1;
AlphaArg1[0] = Diffuse;

// Only one color being used
ColorOp[1]   = Disable;
AlphaOp[1]   = Disable;

// Definition of the vertex shader, declarations then assembly
VertexShader =
asm
{
vs_1_1

dcl_position v0
dcl_normal   v3
dcl_texcoord v7
dcl_texcoord v8

m4x4 oPos, v0, c4 // Transform point to projection space.
m3x3 r0,v3,c0     // Transform normal to world space,
//   put result into r0.
dp3  r0,r0,-c16   // Dot product against light, r0 now has lighting
//   constant in x, y, and z components (r,g,b).
mul  r0,r0,c13    // Modulate against diffuse light color.
mov oD0,r0        // Put into diffuse color output.
};
}
}
```

© 2002 Microsoft Corporation. All rights reserved.