Exercise 5: Vertex Shader Specular Lighting

DirectX8

Microsoft DirectX 8.1 (C++)

Exercise 5: Vertex Shader Specular Lighting

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

vector lhtR;    // Light direction

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

vector matD;    // Material diffuse
vector matS;    // Material specular

vector vCPS;    // Camera position

// Background color
DWORD  BCLR = 0xFF000000;

pixelshader pNIL;

string XFile = "f40.x";

// Technique names for display in viewer window
string tec0 = "Exercise 5: Vertex Shader Specular Lighting";

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

        // Material properties of object
        VertexShaderConstant[9]  = ;                // Diffuse
        VertexShaderConstant[10] = ;                // Specular
        VertexShaderConstant[11] = (0.0f,0.0f,0.0f,0.0f); // Ambient
	
        // Properties of light
        VertexShaderConstant[13] = (1.0f,0.0f,0.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

        // Blending Constants
        VertexShaderConstant[20] = (0.7f,0.7f,0.7f,0.7f);
        VertexShaderConstant[21] = (0.3f,0.3f,0.3f,0.3f);
	
        // Camera Information.
        VertexShaderConstant[24] = ;	

        ColorOp[0]   = SelectArg1;
        ColorArg1[0] = Diffuse;
        AlphaOp[0]   = SelectArg1;
        AlphaArg1[0] = Diffuse;
        ColorOp[1]   = Disable; 
        AlphaOp[1]   = Disable;

        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,r1,r2

            // 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

	    
            // Compute diffuse term
            dp3 r4,r1,-c16
 	 
            // Blend it in
            mul r3,c20,r3   // Kd
            mul r4,r4,c21   // Ks

            mul r4,r4,c10   // Specular
            mad r4,r3,c9,r4 // Diffuse		
	 
            mov oD0,r4      // Put into Diffuse Color
       };     
    }
}

Exercise 5B

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

vector lhtR;	// Light Direction

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

vector vCPS;    // Camera position

texture tEnv;   // Environment texture
texture tDif;   

vector matD;    // Object Diffuse Material Color
vector matS;    // Object Specular Material Color

// Background color
DWORD  BCLR = 0xFF000000;

pixelshader pNIL;

//string XFile = "f40.x";
string XFile = "viper.x";

string BIMG  = "lobbyzneg.bmp";

// Technique names for display in viewer window
string tec0 = "Exercise 5b: Vertex Shader Specular Envmap";


technique tec0
{ 
    pass p0
    {
        // Load matrices
        VertexShaderConstant[0] = ; 	// World Matrix
        VertexShaderConstant[4] = ;	// World*View*Proj Matrix
        
        // Material properties of object
        VertexShaderConstant[9]  = ;                // 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,0.0f,0.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
                
        // Blending constants
        VertexShaderConstant[20] = (-2.0f,-2.0f,-2.0f,-2.0f);
        VertexShaderConstant[21] = ( 0.25f, 0.25f, 0.25f, 0.05f );
        VertexShaderConstant[22] = ( 0.75f, 0.75f, 0.75f, 0.95f );
        VertexShaderConstant[23] = ( 1.00f, 1.00f, 1.00f, 1.00f );
        
        // Camera information
        VertexShaderConstant[24] = ;	
        
        ColorOp[0]   = Modulate;
        ColorArg2[0] = Diffuse;
        ColorArg1[0] = Texture;
        
        AlphaOp[0]   = SelectArg1;
        AlphaArg1[0] = Diffuse;
        
        ColorOp[1]   = Disable; 
        AlphaOp[1]   = Disable;
        
        Texture[0]   = ;
        PixelShader  = ;	
        
        AlphaBlendEnable = True;
        SrcBlend  = One;
        DestBlend = InvSrcAlpha;
        
        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 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)
            
            m3x3 r1,v3,c0             // Transform normal to world space, put in r1
            
            dp3 r3,r0,r1              // Dot product Cam*Normal
            mul r2,c20,r3
            mad oT0.xyz,r2,r1,r0      // Compute reflection vector
            
            // (1-cos)^4 = approx fresnel
            add r0,c23,r3		      // Complement color
            mul r1,r0,r0              // Square
            mul r0,r1,r1              // 4th
            mul r0,r0,c22
//          mov r1,c9
//          mul r1,r1,c21             // Blend in scaled diffuse material color
            add oD0,r0,r1		      // Put into Diffuse Color
        };     
    }
}


technique tec1
{ 
    pass p0
    {        
        // Load matrices
        VertexShaderConstant[0] = ; 	// World Matrix
        VertexShaderConstant[4] = ;	// World*View*Proj Matrix
        
        // Material properties of object
        VertexShaderConstant[9]  = ;                // 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,0.0f,0.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
        
        // Blending Constants
        VertexShaderConstant[20] = (-2.0f,-2.0f,-2.0f,-2.0f);
        VertexShaderConstant[21] = ( 0.25f, 0.25f, 0.25f, 0.05f );
        VertexShaderConstant[22] = ( 0.75f, 0.75f, 0.75f, 0.95f );
        VertexShaderConstant[23] = ( 1.00f, 1.00f, 1.00f, 1.00f );
        VertexShaderConstant[24] = ( 1.0f, 1.0f, 1.0f, 1.0f );
        
        // Camera Information
        VertexShaderConstant[25] = ;	
        
        ColorOp[0]   = Modulate;
        ColorArg1[0] = Texture;
        ColorArg2[0] = Diffuse;
        
        AlphaOp[0]   = SelectArg1;
        AlphaArg1[0] = Diffuse;
        
        ColorOp[1]   = Add; 
        ColorArg1[1] = Current;
        ColorArg2[1] = Specular;

        ColorOp[2]   = Disable;
        AlphaOp[2]   = Disable;
        
        Texture[0]   = ;
        PixelShader  = ;	
        
//      AlphaBlendEnable = True;
//      SrcBlend  = One; //SrcAlpha;
//      DestBlend = InvSrcAlpha;
        
//      CullMode = None;
 
    	SpecularEnable = True;       
        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,-c25     // Get a vector toward the camera position,
                               //   this is 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)
            
            m3x3 r1,v3,c0             // Transform normal to world space, put in r1
            
            dp3 r3,r0,r1              // Dot product Cam*Normal
            mul r2,c20,r3
            mad oT0.xyz,r2,r1,r0      // Compute reflection vector

            // (1-cos)^4 = approx fresnel
            add r0,c23,r3             // Complement color
            mul r1,r0,r0              // Square

            add oD0,r1,c21			
            mov r1,c9
            mul oD1,r1,c21            // Blend in scaled diffuse material color
        };
    }
}

technique tec2
{ 
    pass p0
    {        
        // Load matrices
        VertexShaderConstant[0] = ; 	// World Matrix
        VertexShaderConstant[4] = ;	// World*View*Proj Matrix
        
        // Material properties of object
        VertexShaderConstant[9]  = ;                // Diffuse
        VertexShaderConstant[10] = ;                // Specular
        VertexShaderConstant[11] = (0.0f,0.0f,0.0f,0.0f); // Ambient
        
        // Properties of light	
        VertexShaderConstant[13] = (1.0f,0.0f,0.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
        
        // Blending constants
        VertexShaderConstant[20] = (-2.0f,-2.0f,-2.0f,-2.0f);
        VertexShaderConstant[21] = ( 0.25f, 0.25f, 0.25f, 0.05f );
        VertexShaderConstant[22] = ( 0.75f, 0.75f, 0.75f, 0.95f );
        VertexShaderConstant[23] = ( 1.00f, 1.00f, 1.00f, 1.00f );
        VertexShaderConstant[24] = ( 1.0f, 1.0f, 1.0f, 1.0f );
        
        // Camera information
        VertexShaderConstant[25] = ;	
        
        ColorOp[0]   = Modulate;
        ColorArg1[0] = Texture;
        ColorArg2[0] = Diffuse;
        
        AlphaOp[0]   = SelectArg1;
        AlphaArg1[0] = Diffuse;
        
        ColorOp[1]   = Add; 
        ColorArg1[1] = Current;
        ColorArg2[1] = Specular;

        ColorOp[2]   = Disable;
        AlphaOp[2]   = Disable;
        
        Texture[0]   = ;
        PixelShader  = ;	

//      FillMode = Wireframe;

	    SpecularEnable = True;       
        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,-c25     // Get a vector toward the camera position,
                               //   this is 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)
            
            m3x3 r1,v3,c0             // Transform normal to world space, put in r1
            
            dp3 r3,r0,r1              // Dot product Cam*Normal
            mul r2,c20,r3
            mad oT0.xyz,r2,r1,r0      // Compute reflection vector

            //(1-cos)^4 = approx fresnel
            add r0,c23,r3			  // Complement color
            mul r1,r0,r0			  // Square

//          add r1, r1, c21
//          mul oD0, r1, c10

            mad oD0, r1, c10, c10

            mov r1,c9
            mul oD1,r1,c21            // blend in scaled diffuse mat color
        };     
    }
}


technique tec4
{ 
    pass p0
    {        
        // Load matrices
        VertexShaderConstant[0] = ; 	// World Matrix
        VertexShaderConstant[4] = ;	// World*View*Proj Matrix
        
        // Material properties of object
        VertexShaderConstant[9]  = ;                // Diffuse
        VertexShaderConstant[10] = ;                // Specular
        VertexShaderConstant[11] = (0.0f,0.0f,0.0f,0.0f); // Ambient
        
        // Properties of light	
        VertexShaderConstant[13] = (1.0f,0.0f,0.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
        
        // Blending Constants
        VertexShaderConstant[20] = (-2.0f,-2.0f,-2.0f,-2.0f);
        
        VertexShaderConstant[21] = ( 1.0f, 1.0f, 1.0f, 1.0f );
        VertexShaderConstant[22] = ( 0.75f, 0.75f, 0.75f, 0.95f );
        VertexShaderConstant[23] = ( 1.00f, 1.00f, 1.00f, 1.00f );
        
        // Camera Information
        VertexShaderConstant[24] = ;	
        
//      FillMode = Wireframe;

        ColorOp[0]   = Modulate;
        ColorArg2[0] = Diffuse;
        ColorArg1[0] = Texture;
        
        AlphaOp[0]   = SelectArg1;
        AlphaArg1[0] = Diffuse;
        
        ColorOp[1]   = Disable; 
        AlphaOp[1]   = Disable;
        
        Texture[0]   = ;
        PixelShader  = ;	
        
        AlphaBlendEnable = True;
        SrcBlend  = One;
        DestBlend = InvSrcAlpha;
        
//      CullMode = None;
        
        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 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)
            
            m3x3 r1,v3,c0             // Transform normal to world space, put in r1
            
            dp3 r3,r0,r1              // Dot product Cam*Normal
            mul r2,c20,r3
            mad oT0.xyz,r2,r1,r0      // Compute reflection vector
            
            // (1-cos)^4 = approx fresnel
            add r0,c23,r3				// Complement color
            mul r1,r0,r0				// Square
            mul r0,r1,r1				// 4th

            mul r0,r0,c22
            mov r1,c9					
//          add r0, r0, c10             // Add in specular
            add oD0,r0,r1               // Put into Diffuse Color
        };     
    }
}

Exercise 5C


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

vector lhtR;	// Light direction

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

vector vCPS;	// Camera position

texture tEnv;   // Environment texture
texture tDif;   

vector matD; 	// Object diffuse material color

// Background color
DWORD  BCLR = 0xFF000000;

pixelshader pNIL;

// string XFile = "sphere.x";
// string XFile = "f40.x";
string XFile = "viper.x";

string BIMG  = "lobbyzneg.bmp";

// Technique names for display in viewer window
string tec0 = "Exercise 5b: Vertex Shader Specular Envmap";


technique tec0
{ 
    pass p0
    {        
        // Load matrices
        VertexShaderConstant[0] = ; 	// World Matrix
        VertexShaderConstant[4] = ;	// World*View*Proj Matrix
        
        // Material properties of object
        VertexShaderConstant[9]  = ;                // 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,0.0f,0.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
        
        // Blending Constants
        VertexShaderConstant[20] = (-2.0f,-2.0f,-2.0f,-2.0f);
        
        VertexShaderConstant[25] = ( 1.0f, 1.0f, 1.0f, 1.0f );
        VertexShaderConstant[21] = ( 0.25f, 0.25f, 0.25f, 0.05f );
        VertexShaderConstant[22] = ( 0.75f, 0.75f, 0.75f, 0.95f );
        VertexShaderConstant[23] = ( 1.00f, 1.00f, 1.00f, 1.00f );
        
        // Camera information
        VertexShaderConstant[24] = ;	
        
        ColorOp[0]   = Modulate;
        ColorArg1[0] = Texture;
        ColorArg2[0] = Diffuse;
        
        AlphaOp[0]   = SelectArg1;
        AlphaArg1[0] = Diffuse;
        
        ColorOp[1]   = Add; 
        ColorArg1[1] = Current;
        ColorArg2[1] = Specular;

        AlphaOp[1]   = Disable;
        
        Texture[0]   = ;
        PixelShader  = ;	
        
//      AlphaBlendEnable = True;
//      SrcBlend  = One;//SrcAlpha;
//      DestBlend = InvSrcAlpha;
        
//      CullMode = None;
 
    	SpecularEnable = True;       
        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 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)
            
            m3x3 r1,v3,c0             // Transform normal to world space, put in r1
            
            dp3 r3,r0,r1              // Dot product Cam*Normal
            mul r2,c20,r3
            mad oT0.xyz,r2,r1,r0      // Compute reflection vector

            // (1-cos)^4 = approx fresnel
            add r0,c23,r3			  // Complement color
            mul r1,r0,r0			  // Square
//          mul r0,r1,r1			  // 4th
//          mul r0,r1,r1
	        add oD0,r1,c21			
            mul r0,r0,c22
            mov r1,c9					
            mul oD1,r1,c25            // Blend in scaled diffuse mat color

//          add oD0,r0,r1             // Put into Diffuse Color
//          add oD0,r0,r1             // Put into Diffuse Color
        };     
    }
}