//
// Projective texturing (car lights)
//

void main(float4 position : POSITION,
          float3 normal   : NORMAL,
		  float2 texCoord : TEXCOORD0,

          out float4 oPosition       : POSITION,
          out float2 oTexCoord       : TEXCOORD0,	// Diffuse
          out float4 oTexCoordProj   : TEXCOORD1,	// Projected
          out float  fallOff         : TEXCOORD2,	// Light fall-off
//               out float4 diffuseLighting : COLOR,

           //uniform float Kd,
           uniform float4x4 modelViewProj,
           //uniform float4x4 modelMatrix,
           uniform float3   lightDirection,
           uniform float4x4 textureMatrix,
           uniform float3   center,           // Center of light (origin)
           uniform float    far               // Light far Z
		   )
{
  // World to clipspace
  oPosition=mul(modelViewProj,position);

  //float3 ray=oPosition-center;
  //p=mul(modelMatrix,position);         // Movable objects
  //float3 ray=position-center;
  float3 ray=position-center;
  fallOff=max(1.0-length(ray)/far,0);
  //fallOff=max(1.0-oPosition.z/far,0);
  
  // Fighting Z fighting
  oPosition.z*=0.99995;
//  oPosition.z-=0.0001;

  // Compute texture coordinates for querying the projective texture
  // Note that 'textureMatrix' is calculated in Racer to be
  // a perspective projection on the geometry.
  oTexCoordProj=mul(textureMatrix,position);

  // Pass diffuse map's texture coordinates
  oTexCoord=texCoord;
}
