// force vc7 to group all shader related stuff into the Shader namespace
#ifdef WIN32
namespace Shader {
#endif

// define inputs from application
struct appin
{
  float4 position   : POSITION;
  float3 normal     : NORMAL;
  float2 texCoords0 : TEXCOORD0;
  float3 tangent    : TEXCOORD1;
};

// define outputs from vertex shader
struct vertout
{
  float4 position   : POSITION;
  float2 texCoords0 : TEXCOORD0;
  float2 texCoords1 : TEXCOORD1;
  float3 lightDir   : TEXCOORD2;
  float3 halfAngle  : TEXCOORD3;
};

vertout main(appin v_in,
	     uniform float4x4 MVPMatrix, 

	     uniform float3 lightPosition0,   // object space
	     uniform float3 eyePosition)      // object space
{
  vertout v_out;

  // transform vertex position into homogenous clip-space
  v_out.position   = mul(MVPMatrix, v_in.position);
  v_out.texCoords0 = v_in.texCoords0;
  v_out.texCoords1 = v_in.texCoords0;

  float3 binormal = cross(v_in.tangent, v_in.normal);
  float3x3 rotation = float3x3(v_in.tangent, binormal, v_in.normal);
  v_out.lightDir = normalize(mul(rotation, lightPosition0));

  // Calculate half-angle in tangent space
  float3 eyeDir = normalize(eyePosition - v_in.position.xyz);
  //float3 halfAngle = normalize(eyeDir + v_out.lightDir);
  float3 halfAngle = normalize(eyeDir + lightPosition0);
  v_out.halfAngle = mul(rotation, halfAngle);

  return v_out;
}

#ifdef WIN32
}
#endif
