// define inputs from application
struct appin
{
  float4 vertexPos     : POSITION;
  float4 normal        : NORMAL;
  float4 diffuseCoords : TEXCOORD0;
  float4 lmCoords      : TEXCOORD1;
};

// define outputs from vertex shader
struct vertout
{
  float4 HPosition         : POSITION;
  float4 diffuseMapCoords  : TEXCOORD0;
  float4 lightMapCoords    : TEXCOORD1;

  float4 reflCoords    : TEXCOORD2;

  float4 vertexColor       : COLOR;
};

vertout main(appin IN,

  uniform float4x4 MVPMatrix,

  uniform float4 lightPosition0,   // object space
  uniform float4 lightDiffuse0,
  uniform float4 lightSpecular0,

  uniform float4 eyePosition,      // object space
  uniform float4 globalAmbient
  )
{
  vertout OUT;

  // transform vertex position into homogenous clip-space
  //OUT.HPosition = mul(MVPMatrix, IN.vertexPos);
  OUT.HPosition = mul(MVPMatrix, IN.vertexPos);
  OUT.diffuseMapCoords = IN.diffuseCoords;
  OUT.lightMapCoords   = IN.lmCoords;

  // transform normal and light dirs into world space
  //IN.normal = normalize(mul(MMatrix,IN.normal));

  // light 0
  // diffuse contribution
  float diffuse = saturate(dot(IN.normal.xyz,lightPosition0.xyz));

  // specular contribution
  float3 V = normalize(eyePosition.xyz - IN.vertexPos.xyz);
  float3 H = normalize(lightPosition0.xyz + V);
  float shininess = 10;
  float specular = pow(max(dot(IN.normal.xyz,H),0),shininess);
  //if(diffuse <= 0) specular = 0;

  OUT.vertexColor.xyz = (lightDiffuse0.xyz * diffuse) + (specular*lightSpecular0) + globalAmbient.xyz;

  // store depth in alpha
  //OUT.vertexColor.a = 1.0;
  OUT.vertexColor.a = OUT.HPosition.z;

  // Projective texture coordinates, adjust for mapping
  float4x4 scalemat = float4x4(0.5,   0,   0, 0.5, 
                               0,-0.5,   0, 0.5,
                               0,   0, 0.5, 0.5,
                               0,   0,   0,   1);
  OUT.reflCoords = mul(scalemat, OUT.HPosition);

  //eturn Vector2(((-hcsPosition.x)+1.0)*0.5,((hcsPosition.y)+1.0)*0.5);
  //OUT.reflCoords.x = OUT.HPosition.x*0.001953125;//IN.diffuseCoords.x;//(-OUT.HPosition.x + 1.0)*0.5;
  //OUT.reflCoords.y = OUT.HPosition.y*0.001953125;
  //  OUT.reflCoords.y = IN.diffuseCoords.y;//(OUT.HPosition.y+1.0)*0.5;

  return OUT;
}
