home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 April / Gamestar_72_2005-04_dvd.iso / Dema / Liquidator / Liquidator.tma / data / renderer / pixellights.fx < prev    next >
Text File  |  2004-09-27  |  12KB  |  302 lines

  1. //--------------------------------------------------------------------------
  2. //  TMShaders
  3. //  Copyright (c) 2002 by Tools-Media Corp., All Right Reserved.
  4. //  Programmer: Tima Tcvetkov
  5. //
  6. //  pixellights.fx
  7. //  Per-pixeligt shaders
  8. //--------------------------------------------------------------------------
  9. #ifndef __PIXELLIGHTS_FX__
  10. #define __PIXELLIGHTS_FX__
  11.  
  12. void CalculateVertexLightDistanceIF( ){
  13.   float3 vecLight = (float3)vecPointLightPosition[0].w * (vecPointLightPosition[0].xyz - g_fWorldViewVertexPosition.xyz);
  14.   g_fTexCoords[0].x  = clamp( 1.0f - dot(vecLight,vecLight),0.0f,1.0f);
  15. }
  16.  
  17. void CalculateLightDistanceIF( ){
  18.   g_fTexCoords[0].xyz  = vecPointLightPosition[0].w * (vecPointLightPosition[0].xyz - g_fWorldViewVertexPosition.xyz);
  19. }
  20.  
  21. /********************************************************************************************
  22.     Function computes tangent light vector and volume-map distance attenuation
  23.     for either point or spot lights.
  24. ********************************************************************************************/
  25. void CalculatePPLightWithTextureAttenuation( uniform int NumLights, uniform bool bSpecular, uniform bool bSpotLight ){
  26.  
  27.      
  28.   
  29.   if( bSpotLight ){
  30.     
  31.     for( int i =0; i<NumLights; i++ ){
  32.  
  33.       float3 fSpotLightDirection = -vecSpotLightDirection[i];
  34.       g_fTexCoords[i+i+1].xyz = mul(g_fVertexPosition,matTextureProjection[i]);
  35.             
  36.       if( bSpecular ){
  37.        
  38.        float3 fVecVertex2Eye = normalize( -(float3)g_fWorldViewVertexPosition );
  39.        float1 fDot = dot(g_fWorldViewNormal,fSpotLightDirection);  
  40.        fDot = fDot * 6.5f + 0.5f;
  41.        fDot = clamp(fDot,0.0,1.0);
  42.        fSpotLightDirection = fVecVertex2Eye + fSpotLightDirection;
  43.  
  44.        // back face culling value
  45.        if( i== 0 ){
  46.          g_fColor0.xyz = float3(fDot,fDot,fDot);
  47.        }else if( i == 1 ){
  48.          g_fColor0.w = fDot;
  49.        }else if( i == 2 ){
  50.          g_fColor1.xyz = float3(fDot,fDot,fDot);
  51.        }
  52.        else if( i == 3 ){
  53.          g_fColor1.w = fDot;
  54.  
  55.        } // if i
  56.          
  57.      } // if specular
  58.       /*    
  59.       g_fTexCoords[i+i+2].x = dot(g_fWorldViewS,  fSpotLightDirection);
  60.       g_fTexCoords[i+i+2].y = dot(g_fWorldViewT,  fSpotLightDirection);
  61.       g_fTexCoords[i+i+2].z = dot(g_fWorldViewSxT,fSpotLightDirection);
  62.       */
  63.       g_fTexCoords[i+i+2].x = dot(g_fWorldViewS,  fSpotLightDirection);
  64.       g_fTexCoords[i+i+2].y = dot(g_fWorldViewT,  fSpotLightDirection);
  65.       g_fTexCoords[i+i+2].z = dot(g_fWorldViewSxT,fSpotLightDirection);
  66.  
  67.     }  // for
  68.  
  69.   } // if spot light
  70.  
  71.   else{
  72.   
  73.    for( int i =0; i < NumLights; i++){
  74.      float3 rV2LV = (float3)vecPointLightPosition[i] - (float3)g_fWorldViewVertexPosition;
  75.      float3 rVertex2LightVector = normalize(rV2LV);    
  76.     
  77.      if( bSpecular ){
  78.        
  79.        float3 fVecVertex2Eye = normalize( -(float3)g_fWorldViewVertexPosition );
  80.        float1 fDot = dot(g_fWorldViewNormal,rVertex2LightVector);  
  81.        fDot = fDot * 6.5f + 0.5f;
  82.        fDot = clamp(fDot,0.0,1.0);
  83.        rVertex2LightVector = fVecVertex2Eye + rVertex2LightVector;
  84.  
  85.        // back face culling value
  86.        if( i== 0 ){
  87.          g_fColor0.xyz = float3(fDot,fDot,fDot);
  88.        }else if( i == 1 ){
  89.          g_fColor0.w = fDot;
  90.        }else if( i == 2 ){
  91.          g_fColor1.xyz = float3(fDot,fDot,fDot);
  92.        }
  93.        else if( i == 3 ){
  94.          g_fColor1.w = fDot;
  95.        }// if i
  96.      } // if specular
  97.     g_fTexCoords[i+i+1].xyz = rV2LV * vecPointLightPosition[i].w + 0.5; 
  98.     g_fTexCoords[i+i+2].x = dot(g_fWorldViewS,rVertex2LightVector);
  99.     g_fTexCoords[i+i+2].y = dot(g_fWorldViewT,rVertex2LightVector);
  100.     g_fTexCoords[i+i+2].z = dot(g_fWorldViewSxT,rVertex2LightVector);
  101.    
  102.    }// for
  103.  
  104.  }// else
  105.  
  106. }
  107. /*******************************************************************************
  108.   Simplified function for diffuse per-pixel lighting.
  109.   Distance attenuation passed thru diffuse and specular intrapolators.
  110.   Tangent light vector passed thru texture coordinates.
  111. ********************************************************************************/
  112. void CalculateSimplePointLightDiffuseTangentLightVector20( uniform int NumLights ){
  113.   
  114.   for( int i =0; i<NumLights; i++ ){
  115.    /*
  116.    float4 Vertex2PointLight;
  117.    Vertex2PointLight.xyz = vecPointLightPosition[i] - (float3)g_fWorldViewVertexPosition;
  118.    float DotProduct = dot(Vertex2PointLight.xyz,Vertex2PointLight.xyz);    
  119.    Vertex2PointLight.w = rsqrt(DotProduct);
  120.    Vertex2PointLight.xyz = Vertex2PointLight.xyz * Vertex2PointLight.w;
  121.    
  122.    float fAttenuationDot = 1.0f - (Vertex2PointLight.w * DotProduct * vecPointLightPosition[i].w);
  123.    
  124.    float DotProduct = clamp(dot(g_fWorldViewNormal,(float3)Vertex2PointLight),0.0f,1.0f);
  125.    */
  126.    float3 Vertex2PointLight;
  127.    Vertex2PointLight = vecPointLightPosition[i].xyz - (float3)g_fWorldViewVertexPosition;
  128.    float3 AttenuatedLightVec = Vertex2PointLight * (float3)vecPointLightPosition[i].w;
  129.    
  130.    float fAttenuationDot = clamp(1.0f - dot(AttenuatedLightVec,AttenuatedLightVec),0.0f,1.0f);
  131.    Vertex2PointLight = normalize(Vertex2PointLight);
  132.    //float DotProduct = clamp(dot(g_fWorldViewNormal,Vertex2PointLight),0.0f,1.0f);
  133.    
  134.    g_fTexCoords[i+1].w = /*DotProduct **/ fAttenuationDot;
  135.    g_fTexCoords[i+1].x = dot(g_fWorldViewS,Vertex2PointLight);
  136.    g_fTexCoords[i+1].y = dot(g_fWorldViewT,Vertex2PointLight);
  137.    g_fTexCoords[i+1].z = dot(g_fWorldViewSxT,Vertex2PointLight);
  138.  }
  139.   
  140. }
  141.  
  142. void CalculateSimplePointLightDiffuseTangentLightVector( uniform int NumLights ){
  143.   for( int i =0; i<NumLights; i++ ){
  144.    /*
  145.    float4 Vertex2PointLight;
  146.    Vertex2PointLight.xyz = vecPointLightPosition[i] - (float3)g_fWorldViewVertexPosition;
  147.    float DotProduct = dot(Vertex2PointLight.xyz,Vertex2PointLight.xyz);    
  148.    Vertex2PointLight.w = rsqrt(DotProduct);
  149.    Vertex2PointLight.xyz = Vertex2PointLight.xyz * Vertex2PointLight.w;
  150.    
  151.    float1 fAttenuationDot = 1.0f - (Vertex2PointLight.w * DotProduct * vecPointLightPosition[i].w);
  152.    */
  153.    float3 Vertex2PointLight;
  154.    Vertex2PointLight = vecPointLightPosition[i].xyz - (float3)g_fWorldViewVertexPosition;
  155.    float3 AttenuatedLightVec = Vertex2PointLight * (float3)vecPointLightPosition[i].w;
  156.    
  157.    float fAttenuationDot = clamp(1.0f - dot(AttenuatedLightVec,AttenuatedLightVec),0.0f,1.0f);
  158.    Vertex2PointLight = normalize(Vertex2PointLight);
  159.    //float DotProduct = clamp(dot(g_fWorldViewNormal,(float3)Vertex2PointLight),0.0f,1.0f);
  160.    g_fColor0.w = /*DotProduct **/ fAttenuationDot;
  161.    
  162.    g_fColor0.x = dot(g_fWorldViewS,Vertex2PointLight);
  163.    g_fColor0.y = dot(g_fWorldViewT,Vertex2PointLight);
  164.    g_fColor0.z = dot(g_fWorldViewSxT,Vertex2PointLight);
  165.    g_fColor0.xyz = g_fColor0.xyz * 0.5f + 0.5f;
  166.   
  167.  }
  168.   
  169. }
  170. /*******************************************************************************
  171.   Simplified function for specular per-pixel lighting.
  172.   Distance attenuation passed thru diffuse and specular intrapolators.
  173.   Tangent light vector passed thru texture coordinates.
  174. ********************************************************************************/
  175. void CalculateVertexSpecularHalfVector20( uniform int NumLights ){
  176.  
  177.  float3 VecVertex2Eye = normalize( -(float3)g_fWorldViewVertexPosition );
  178.  
  179.  for( int i =0; i<NumLights; i++){
  180.  /*
  181.   float4 Vertex2PointLight;
  182.   Vertex2PointLight.xyz = vecPointLightPosition[i] - (float3)g_fWorldViewVertexPosition;
  183.   float DotProduct = dot(Vertex2PointLight.xyz,Vertex2PointLight.xyz);    
  184.   Vertex2PointLight.w = rsqrt(DotProduct);
  185.   
  186.   Vertex2PointLight.xyz = Vertex2PointLight.xyz * Vertex2PointLight.w;
  187.     
  188.   float1 fAttenuationDot = 1.0f - (Vertex2PointLight.w * DotProduct * vecPointLightPosition[i].w);
  189.   */
  190.   float3 Vertex2PointLight;
  191.   Vertex2PointLight = vecPointLightPosition[i].xyz - (float3)g_fWorldViewVertexPosition;
  192.   float3 AttenuatedLightVec = Vertex2PointLight * (float3)vecPointLightPosition[i].w;
  193.   float fAttenuationDot = clamp(1.0f - dot(AttenuatedLightVec,AttenuatedLightVec),0.0f,1.0f);
  194.   Vertex2PointLight = normalize(Vertex2PointLight);
  195.    
  196.   //float DotProduct = clamp( dot(g_fWorldViewNormal,(float3)Vertex2PointLight),0.0f,1.0f);
  197.   g_fTexCoords[i+1].w = /*DotProduct **/ fAttenuationDot;
  198.   
  199.   float3 VecHalf = normalize( Vertex2PointLight + VecVertex2Eye );
  200.   
  201.   g_fTexCoords[i+1].x = dot(g_fWorldViewS,VecHalf);
  202.   g_fTexCoords[i+1].y = dot(g_fWorldViewT,VecHalf);
  203.   g_fTexCoords[i+1].z = dot(g_fWorldViewSxT,VecHalf);
  204.  
  205.  }// for
  206.  
  207. }
  208.  
  209. void CalculateVertexSpecularHalfVector( uniform int NumLights ){
  210.  
  211.  float3 VecVertex2Eye = normalize( -(float3)g_fWorldViewVertexPosition );
  212.  
  213.  for( int i =0; i<NumLights; i++){
  214.   /*
  215.   float4 Vertex2PointLight;
  216.   Vertex2PointLight.xyz = vecPointLightPosition[i] - (float3)g_fWorldViewVertexPosition;
  217.   float DotProduct = dot(Vertex2PointLight.xyz,Vertex2PointLight.xyz);    
  218.   Vertex2PointLight.w = rsqrt(DotProduct);
  219.   
  220.   Vertex2PointLight.xyz = Vertex2PointLight.xyz * Vertex2PointLight.w;
  221.     
  222.   float1 fAttenuationDot = 1.0f - (Vertex2PointLight.w * DotProduct * vecPointLightPosition[i].w);
  223.   
  224.   DotProduct = clamp( dot(g_fWorldViewNormal,(float3)Vertex2PointLight),0.0f,1.0f);
  225.   */
  226.    float3 Vertex2PointLight;
  227.   Vertex2PointLight = vecPointLightPosition[i].xyz - (float3)g_fWorldViewVertexPosition;
  228.   float3 AttenuatedLightVec = Vertex2PointLight * (float3)vecPointLightPosition[i].w;
  229.   float fAttenuationDot = clamp(1.0f - dot(AttenuatedLightVec,AttenuatedLightVec),0.0f,1.0f);
  230.   Vertex2PointLight = normalize(Vertex2PointLight);
  231.    
  232.   //float DotProduct = clamp( dot(g_fWorldViewNormal,(float3)Vertex2PointLight),0.0f,1.0f);
  233.   
  234.   g_fColor0.w = /*DotProduct **/ fAttenuationDot;
  235.   
  236.   float3 VecHalf = normalize( Vertex2PointLight + VecVertex2Eye );
  237.   
  238.   g_fColor0.x = dot(g_fWorldViewS,VecHalf);
  239.   g_fColor0.y = dot(g_fWorldViewT,VecHalf);
  240.   g_fColor0.z = dot(g_fWorldViewSxT,VecHalf);
  241.   
  242.   g_fColor0.xyz = g_fColor0.xyz * 0.5f + 0.5f;
  243.  
  244.  }// for
  245.  
  246. }
  247. /*******************************************************************
  248.  
  249.    Simple 3D texture projection for point and spot lights
  250.  
  251. ********************************************************************/
  252. void CalculateSimple3DBallLight( uniform int NumLights, uniform bool bSpot ){
  253.  
  254.  
  255.   for( int i =0; i<NumLights; i++ ){
  256.    if( bSpot ){
  257.     g_fTexCoords[i].xyz = mul(g_fVertexPosition,matTextureProjection[i]);
  258.    } // if bSpot
  259.    else{
  260.  
  261.      float3 Vertex2PointLight;
  262.      Vertex2PointLight.xyz = vecPointLightPosition[i] - (float3)g_fWorldViewVertexPosition;   
  263.      g_fTexCoords[i].xyz = Vertex2PointLight.xyz * vecPointLightPosition[i].w + 0.5;
  264.     }//else
  265.    
  266.   } // for
  267.  
  268. }
  269.  
  270. void CalculateBlinnBumpmapMatrix(){
  271.  
  272.  float3 Eye = (float3)vecEyePosition - (float3)g_fVertexPosition;
  273.  g_fTexCoords[1].x = dot( g_fS,  (float3)vecCubeMapMatrixRow[0]);
  274.  g_fTexCoords[1].y = dot( g_fT,  (float3)vecCubeMapMatrixRow[0]);
  275.  g_fTexCoords[1].z = dot( g_fSxT,(float3)vecCubeMapMatrixRow[0]);
  276.  g_fTexCoords[1].w = Eye[0];
  277.  g_fTexCoords[2].x = dot( g_fS,  (float3)vecCubeMapMatrixRow[1]);
  278.  g_fTexCoords[2].y = dot( g_fT,  (float3)vecCubeMapMatrixRow[1]);
  279.  g_fTexCoords[2].z = dot( g_fSxT,(float3)vecCubeMapMatrixRow[1]);
  280.  g_fTexCoords[2].w = Eye[1];
  281.  g_fTexCoords[3].x = dot( g_fS,  (float3)vecCubeMapMatrixRow[2]);
  282.  g_fTexCoords[3].y = dot( g_fT,  (float3)vecCubeMapMatrixRow[2]);
  283.  g_fTexCoords[3].z = dot( g_fSxT,(float3)vecCubeMapMatrixRow[2]);
  284.  g_fTexCoords[3].w = Eye[2];
  285.  
  286. }
  287. void ProjectTexture( uniform int NumLights ){
  288.  
  289.    for( int i =0; i<NumLights; i++ ){
  290.     g_fTexCoords[i].xyz = mul(g_fWorldViewVertexPosition,matTextureProjection[i]);
  291.    }  
  292.  
  293. }
  294. void TransformPosition4x4( uniform int NumLights ){
  295.  
  296.   for( int i =0; i<NumLights; i++ ){
  297.     g_fTexCoords[i] = mul(g_fVertexPosition,matTextureProjection[i]);
  298.    }   
  299.  
  300. }
  301. #endif //__PIXELLIGHTS_FX__
  302.