uniform float4x4 	matWorld;
uniform float4x4 	matWorldViewProj;
uniform float4x4	matInvTransposeWorld;
uniform float4x4	matInvWorld;
uniform float3		msCamPos;

static const int  NUM_LIGHTS = 2;
static float fDistMax = 1000;

struct VS_INPUT 
{
	float4 position		: POSITION0;
	float2 uv			: TEXCOORD0;
	float3 normal 		: NORMAL0;
	float3 tangent		: TANGENT0;
};

struct VS_OUTPUT 
{
	float4 position 					: POSITION0;
	float2 uv		 					: TEXCOORD0;
	float3 tsViewDirection 				: TEXCOORD1;
	float3 tsLightDirection[NUM_LIGHTS]	: TEXCOORD2;
	float  fDistance					: TEXCOORD5;
};

uniform float3 wsLightDir[NUM_LIGHTS];

VS_OUTPUT main_vs( VS_INPUT input )
{
	VS_OUTPUT output;

	output.position = mul( matWorldViewProj, input.position );
	output.uv = input.uv;

	// build a tangent space basis
	float3 msTangent = normalize( input.tangent.xyz );
	float3 msNormal = normalize( input.normal );
	float3 msBinormal = normalize( cross(msTangent, msNormal) );
	float3x3 rotation = float3x3( msTangent, msBinormal, msNormal);

	float3 tmp =  normalize(input.position - float4(msCamPos,0)).xyz;
	output.tsViewDirection =  normalize( mul(rotation, tmp) ).xyz;
	
	for (int i = 0; i < NUM_LIGHTS; i++)
	{
		float3 msLightDir = mul( matInvWorld, wsLightDir[i] ).xyz;
		output.tsLightDirection[i] = normalize( mul(rotation, msLightDir) ).xyz;	
	}

	float fDist = distance( msCamPos.xyz, input.position.xyz );
	output.fDistance = fDist / fDistMax; 

	return output;
}
