Exercise 11: Area Lighting

DirectX8

Microsoft DirectX 8.1 (C++)

Exercise 11: Area Lighting

//
// Effect File Workshop Exercise 11
// Copyright (c) 2000 Microsoft Corporation. All rights reserved.
//

vector lhtR;    // Light Direction
vector matD;    // Material Diffuse

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

texture tDif;   // Diffuse texture of object


// Background color
DWORD  BCLR = 0xFF000000;

pixelshader pNIL;

string XFile = "skullhiv.x";

// Technique names for display in viewer window
string tec0 = "Exercise 11a: Area Lighting";
string tec1 = "Exercise 11b: Area and Diffuse Lighting";

technique tec0
{ 
    pass p0
    {
        // Load matrices
        VertexShaderConstant[0] = <mWld>;                   // World Matrix
        VertexShaderConstant[4] = <mTot>;                   // World*View*Proj Matrix

        // Material properties of object
        VertexShaderConstant[9]  = <matD>;                  // Diffuse
        VertexShaderConstant[10] = (0.0f,0.0f,0.0f,0.0f);   // Specular
        VertexShaderConstant[11] = (0.0f,0.0f,0.0f,0.0f);   // Ambient

        // Properties of light
        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] = <lhtR>;                  // Light direction

        VertexShaderConstant[20] = (.5f,.5f,.5f,.5f); 
        VertexShaderConstant[40] = (1.0f,1.0f,1.0f,1.0f);
        VertexShaderConstant[41] = (1.00f,0.86f,0.75f,1.0f); // sky color
        VertexShaderConstant[42] = (0.25f,.25f,0.15f,1.0f);  // ground color
        VertexShaderConstant[43] = <matD>;                   // object color
        VerteXShaderConstant[44] = (0.0f,-1.0f,0.0f,1.0f);   // sky direction

           Texture[0]   = <tDif>;

        wrap0        = U | V;
        wrap1        = 0;
        wrap2        = 0;

        AddressU[0] = wrap;
        AddressV[0] = wrap;

        MinFilter[0] = Linear;
        MagFilter[0] = Linear;
        MinFilter[1] = Linear;
        MagFilter[1] = Linear;

        ColorOp[0]   = Modulate;
        ColorArg1[0] = Diffuse;
        ColorArg2[0] = Texture;
        AlphaOp[0]   = SelectArg1;
        AlphaArg1[0] = Diffuse;

        VertexShader = 
        decl
        {
            stream 0;
            float v0[3];         // Position
            float v3[3];         // Normal
            float v7[3];         // Texture Coord1
            float v8[3];         // Tangent
        }   
        asm 
        {
            vs.1.1
            m4x4 oPos,v0,c4      // Transform position 
            m3x3   r0,v3,c0      // Transform normal to world space

            dp3 r0,r0,-c44       // Dot product against sky vector

            // The dot product was between -1 to 1. We want
            //   to re-range this to 0 to 1
            mad r0,r0,c20,c20

            // Now lerp between the two sky colors.
            mov r1,c42
            sub r1,c41,r1        // To save a clock, this delta should be
                                 // saved in a register
            mad r0,r1,r0,c42     // Now lerp

            sub r1,c40,v7.zzz    // This modulates against a darkening term
                                 //   or occlusion term, embedded in the z tex
                                 //   coordinate, we want 1-z though
            mul r0,r0,r1         // Darken sky term
            mul oD0,r0,c9        // Now modulate against object's color

            mov oT0.xy,v7.xy
        };
    }
}


technique tec1
{ 
    pass p0
    {
        // Load matrices
        VertexShaderConstant[0] = <mWld>;                    // World Matrix
        VertexShaderConstant[4] = <mTot>;                    // World*View*Proj Matrix

        //Material properties of object
        VertexShaderConstant[9]  = <matD>;                   // Diffuse
        VertexShaderConstant[10] = (0.0f,0.0f,0.0f,0.0f);    // Specular
        VertexShaderConstant[11] = (0.0f,0.0f,0.0f,0.0f);    // Ambient

        //properties of light    
        VertexShaderConstant[13] = (.6f,.6f,.6f,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] = <lhtR>;                   // Light direction

        VertexShaderConstant[20] = (.5f,.5f,.5f,.5f); 
        VertexShaderConstant[40] = (1.0f,1.0f,1.0f,1.0f);
        VertexShaderConstant[41] = (.80f,0.76f,0.65f,1.0f);  // Sky color
        VertexShaderConstant[42] = (0.25f,.25f,0.15f,1.0f);  // Ground color
        VertexShaderConstant[43] = <matD>;                   // Object color
        VerteXShaderConstant[44] = (0.0f,-1.0f,0.0f,1.0f);   // Sky direction

        FillMode = Wireframe;

        Texture[0]   = <tDif>;

        wrap0        = U | V;
        wrap1        = 0;
        wrap2        = 0;

        AddressU[0] = wrap;
        AddressV[0] = wrap;

        MinFilter[0] = Linear;
        MagFilter[0] = Linear;
        MinFilter[1] = Linear;
        MagFilter[1] = Linear;

        ColorOp[0]   = Modulate; 
        ColorArg1[0] = Diffuse;
        ColorArg2[0] = Texture;
        AlphaOp[0]   = SelectArg1; 
        AlphaArg1[0] = Diffuse;

        VertexShader = 
        decl
        {
            stream 0;
            float v0[3];        // Position
            float v3[3];        // Normal
            float v7[3];        // Texture Coord1
            float v8[3];        // Tangent
        }   
        asm 
        {
            vs.1.1
            m4x4 oPos,v0,c4     // Transform position
            m3x3   r9,v3,c0     // Transform normal to world space
        
            dp3 r0,r9,-c44      // Dot product against sky vector
          
            // The dot product was between -1 to 1. We want
            //   to re-range this to 0 to 1
            mad r0,r0,c20,c20

            //now lerp between the two sky colors
            mov r1,c42
             sub r1,c41,r1      // To save a clock, this delta should
                                //   be saved in a register
            mad r0,r1,r0,c42    // Now lerp

            sub r1,c40,v7.zzz   // This modulates against a darkening term
                                //   or occlusion term, embedded in the z tex
                                //   coordinate, we want 1-z though
            mul r2,r0,r1        // Darken sky term
            dp3 r1,r9,-c16      // Dot against light vector
            max r1, c15, r1

            // Complement, square, complement
            sub r1, c40, r1
            mul r1, r1, r1
            mul r1, r1, r1
            sub r1, c40, r1

            mad r0,r1,c13,r2    // Combine with light diffuse 
            mul oD0,r0,c9       // Now modulate against object's color

            mov oT0.xy,v7.xy
        };
    }
}