uniform float4x4	matWorldViewProj;
uniform float4x4	matWorld;

extern uniform float worldSize;

static const int  	NUM_LIGHTS = 2;

struct VS_INPUT 
{
	float3 position						: POSITION;
	float2 uv0							: TEXCOORD0;
	float3 normal						: NORMAL;
	float3 tangent						: TANGENT0;
};

struct VS_OUTPUT 
{
	float4 position						: POSITION;
	float2 uv0 							: TEXCOORD0;
	float2 terrainUV					: TEXCOORD1;
	float3 tsLightDir					: TEXCOORD2;	//tangent space light direction
};

uniform float3 wsLightDir[NUM_LIGHTS];

VS_OUTPUT main_vs( VS_INPUT input )
{
	VS_OUTPUT output;

	float4 inPosHomogeneous = float4( input.position, 1.0f );
	float4 wsPos = mul( matWorld, inPosHomogeneous );
	
	output.position =  mul(matWorldViewProj, inPosHomogeneous );

	output.uv0 = input.uv0;

	output.terrainUV = float2( wsPos.x/worldSize, wsPos.z/worldSize );

	// for bumpmapping
	// calculate the tangent space basis and transform the light to that coordinate system
	float3 tangent = normalize(input.tangent ); 
	float3 binormal = normalize( cross(tangent.xyz, input.normal) );
	float3x3 rotation = float3x3( tangent.xyz, binormal, input.normal );
	
	output.tsLightDir = normalize( mul(rotation, wsLightDir[0]).xyz );
	
	return output;
}
