Exercise 1: Diffuse Lighting

DirectX8

Microsoft DirectX 8.1 (C++)

Exercise 1: Diffuse Lighting

//
// Effect File Workshop Solution for Exercise 1
// Copyright (c) 2001 Microsoft Corporation. All rights reserved.
//

vector lhtR;	// Direction of light
vector matD; 	// Object diffuse material color

matrix mWld;	// World
matrix mTot;	// Total

// Load model
string XFile = "sphere.x";

// Background color
DWORD  BCLR = 0xff333333;

// No pixel shader
pixelshader pNIL; 

// Technique names for display in viewer window
string tec0 = "Exercise 1a: Fixed Function Diffuse Lighting";
string tec1 = "Exercise 1b: Vertex Shader Diffuse Lighting";


/////////////////////////////////////////////////////////////////////////////
///////        Exercise 1a: Fixed Function Diffuse Lighting        //////////
///////     Change diffuse material color to color from model,     //////////
///////     rather than the current white material constant.       //////////
/////////////////////////////////////////////////////////////////////////////
// Given:  The app has already set the matrices before calling this technique.

technique tec0
{
    pass P0
    {
        // Diffuse, specular, and ambient material colors of object
        MaterialDiffuse  = ;                   // Diffuse from object material diffuse
        MaterialDiffuse  = (0.0f,0.0f,1.0f,1.0f);    // Diffuse from constant color

        MaterialSpecular = (0.0f,0.0f,0.0f,0.0f);
        MaterialAmbient  = (0.0f,0.0f,0.0f,0.0f);
        
        // Light Properties. lhtR, the light direction, is input from the shader app
        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] = ;
        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;
    }
}


/////////////////////////////////////////////////////////////////////////////
///////        Exercise 1b: Vertex Shader Diffuse Lighting         //////////
///////     Change diffuse material color to color from model,     //////////
///////     rather than the current white material constant.       //////////
/////////////////////////////////////////////////////////////////////////////
technique tec1
{ 
    pass p0
    {
        // Load matrices
        VertexShaderConstant[0] = ;                   // World Matrix
        VertexShaderConstant[4] = ;                   // World*View*Proj Matrix
        
        // Material properties of object
        VertexShaderConstant[9]  = (1.0f,1.0f,1.0f,1.0f);   // Diffuse from constant color
        VertexShaderConstant[9]  = ;                  // Diffuse from object material diffuse

        VertexShaderConstant[10] = (0.0f,0.0f,0.0f,0.0f);   // Specular from constant color
        VertexShaderConstant[11] = (0.0f,0.0f,0.0f,0.0f);   // Ambient from constant color
        
        // Light Properties. lhtR, the light direction, is input from the shader app
        VertexShaderConstant[13] = (1.0f,1.0f,1.0f,1.0f);   // Diffuse
        VertexShaderConstant[14] = (0.0f,0.0f,0.0f,0.0f);   // Specular
        VertexShaderConstant[15] = (0.0f,0.0f,0.0f,0.0f);   // Ambient
        VertexShaderConstant[16] = ;                  // Light direction
        
        // 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 =
        decl
        {
            stream 0;
            float v0[3];       // Position
            float v3[3];       // Normal
            float v7[3];       // Texture Coord1
            float v8[3];       // Texture coord2
        }   
        asm
        {
            vs.1.1             // Version number

            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.
        };     
    }
}