uniform float4x4	matWorldViewProj;
uniform float4x4	matWorld;

static const float 	TILING_MODIFIER = 40.0f;
uniform float		worldSize;

static const int  NUM_LIGHTS = 2;

// Direction of the texture co-ordinates u axis
static const float3	u = float3(1, 0, 0);

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

struct VS_OUTPUT 
{
	float4 position						: POSITION;

	float2 uv0 							: TEXCOORD0;
	float2 terrainUV					: TEXCOORD2;	
	float3 tsLightDir					: TEXCOORD3;	//tangent space light direction
	float3 wsPos						: TEXCOORD6;
};

uniform float3 wsLightDir[NUM_LIGHTS];

VS_OUTPUT main_vs( VS_INPUT input )
{
	VS_OUTPUT output;

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

	output.uv0 = input.uv0*worldSize/TILING_MODIFIER;

	output.terrainUV = float2( input.position.x/worldSize, input.position.z/worldSize );
	
	// for bumpmapping
	// calculate the tangent space basis and transform the light to that coordinate system
	// we know the texture u direction to be the +ve x-axis, so all we need is its component perpendicular to the normal
	float3 tangent = normalize( u - dot(u, input.normal)*input.normal ); 
	float3 binormal = normalize( cross(tangent.xyz, input.normal) );
	float3x3 rotation = float3x3( tangent.xyz, binormal, input.normal );
	
	output.tsLightDir = normalize( mul( rotation, wsLightDir[0]).xyz );

	output.wsPos = mul( matWorld, inPosHomogeneous ).xyz;
	
	return output;
}
