Exercise 8: Texturing with Lights

DirectX8

Microsoft DirectX 8.1 (C++)

Exercise 8: Texturing with Lights

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

vector lhtR;    // Light direction

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

texture tDif;   // Diffuse texture of object
texture tNSE;   // Noise Texture

vector vCPS;    // Camera position

// Background color
DWORD  BCLR = 0xFF0000FF;

pixelshader pNIL;

string XFile = "sphere.x";

// Technique names for display in viewer window
string tec0 = "Exercise 8: Texturing with lights";

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

        // Material properties of object
        VertexShaderConstant[9]  = (1.0f,1.0f,1.0f,1.0f); // 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

        // Useful constants
        VertexShaderConstant[20] = (1.0f,1.0f,1.0f,1.0f);
        VertexShaderConstant[21] = (1.0f,1.0f,1.0f,0.0f);

        // Camera Information
        VertexShaderConstant[24] = <vCPS>;    

        Texture[0]   = <tDif>;
        Texture[1]   = <tNSE>;
        wrap0        = U | V;
        wrap1        = U | V;
    
        AddressU[0] = wrap;
        AddressV[0] = wrap;
        AddressU[1] = wrap;
        AddressV[1] = wrap;

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

        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
            m4x4 r0,v0,c0             // Transform point to world space
        
            add r0,-r0,c24            // Get a vector toward the camera position,
                                      //   this is the negative of the camera direction 
            // Normalize
            dp3 r11.x,r0.xyz,r0.xyz   // Load the square into r1
            rsq r11.xyz,r11.x         // Get the inverse of the square
            mul r0.xyz,r0.xyz,r11.xyz // Multiply, r0 = -(camera vector)

            add r2.xyz,r0.xyz,-c16    // Get half angle

            // Normalize
            dp3 r11.x,r2.xyz,r2.xyz   // Load the square into r1
            rsq r11.xyz,r11.x         // Get the inverse of the square
            mul r2.xyz,r2.xyz,r11.xyz // Multiply, r2 = HalfAngle

            m3x3 r1,v3,c0             // Transform normal to world space, put in r1

            // r2 = half angle, r1 = normal, r3 (output) = intensity
            dp3  r3.xyzw,r2,r1

            // Now raise it several times
            mul r3,r3,r3              // 2nd
            mul r3,r3,r3              // 4th
            mul r3,r3,r3              // 8th
            mul r3,r3,r3              // 16th

            mul r3,r3,c20

            // Compute diffuse term
            dp3 r4,r1,-c16
            mul r4,r4,c21

            mov oD0,r4
            mov oD1,r3

            mov oT0.xy,v7.xy          // Copy texture coordinates to oT0
            mov oT1.xy,v7.xy          // Copy texture coordinates to oT1
        };

        PixelShader = 
        asm
        {
           ps.1.1
           tex t0                     // Sample texture 0
           tex t1                     // Sample texture 1

           mul_x2 r1,t1,t0;           // Blend them together 
           mov    r0,r1

           mul    r0,r1,v0;           // Modulate diffuse 
           mul    r1,r1,v1;           // Modulate specular
           add    r0,r0,v1;           // Blend them together
        };
    }
}