//!HLSL

#include "fogHelper.hlsl"

// figure out how many textures we are blending
#if defined(FOUR_TEXTURES)
#define NUM_TEXTURES 4
#elif defined(THREE_TEXTURES)
#define NUM_TEXTURES 3
#elif defined(TWO_TEXTURES)
#define NUM_TEXTURES 2
#else
#define NUM_TEXTURES 1
#endif

// -----------------------------------------------------------------------------
// Vertex Shader
// -----------------------------------------------------------------------------
struct VSOutput
{
	float4 _position	: POSITION;
	float4 _fogValue    : COLOR0;

#ifdef VERTEX_COLOR
	float4 _color       : COLOR1;
#endif

	float2 _uv0			: TEXCOORD1;

#if NUM_TEXTURES > 1
	float2 _uv1			: TEXCOORD2;
#if NUM_TEXTURES > 2
	float2 _uv2			: TEXCOORD3;
#if NUM_TEXTURES > 3
	float2 _uv3         : TEXCOORD4;
#endif
#endif
#endif

};

VSOutput VSMain(float4 vPosition : POSITION,
	            float2 vUV0 : TEXCOORD0,
#if NUM_TEXTURES > 1
	            float2 vUV1 : TEXCOORD1,
#if NUM_TEXTURES > 2
	            float2 vUV2 : TEXCOORD2,
#if NUM_TEXTURES > 3
		        float2 vUV3 : TEXCOORD3,
#endif
#endif
#endif

#ifdef VERTEX_COLOR
			    float4 vColor : COLOR,
#endif

				uniform float4x4 modelViewProjection,
				uniform float4 fogColor,
				uniform float2 fogParams
 #ifdef OCEANDEPTH
 				,
				uniform float4x4 modelWorld,
 				uniform float3 oceanDepthRange,
 				uniform float4 oceanDepthColor
 #endif
 
#ifdef TEXTURE_TRANSFORM0
				,
				uniform float4x4 textureMatrix0
#endif
 
#ifdef TEXTURE_TRANSFORM1
				,
				uniform float4x4 textureMatrix1
#endif
 

 )  
{
	VSOutput ret;

	// position
	//
	ret._position = mul(modelViewProjection, vPosition);

	// uvs
	//
#ifdef TEXTURE_TRANSFORM0
	float4 tmpUV;
	tmpUV.xy = vUV0;
	tmpUV.zw = 0;
	tmpUV.w = 1;
	ret._uv0 = mul( textureMatrix0, tmpUV ).xy;
#else
	ret._uv0 = vUV0;
#endif

#if NUM_TEXTURES > 1
	#ifdef TEXTURE_TRANSFORM1
		tmpUV.xy = vUV1;
		ret._uv1 = mul( textureMatrix1, tmpUV ).xy;
	#else
		ret._uv1 = vUV1;
	#endif

#if NUM_TEXTURES > 2
	ret._uv2 = vUV2;
#if NUM_TEXTURES > 3
	ret._uv3 = vUV3;
#endif
#endif
#endif

#ifdef VERTEX_COLOR
	ret._color = vColor;
#endif

	// fog
	//
	
#ifdef OCEANDEPTH
 	ret._fogValue = getFogValueWorldSpace(fogColor, fogParams,
 								ret._position, mul( modelWorld, vPosition ).xyz, 
 								oceanDepthRange, oceanDepthColor);
#else
 	ret._fogValue = getFogValue(fogColor, fogParams,ret._position.xyz);
#endif 
	
	return ret;
}

// -----------------------------------------------------------------------------
// Pixel Shader
// -----------------------------------------------------------------------------

float4 PSMain(VSOutput vertex,
			  uniform sampler2D samplerDiffuse0
#if NUM_TEXTURES > 1
			  , uniform sampler2D samplerDiffuse1
#if NUM_TEXTURES > 2
			  , uniform sampler2D samplerDiffuse2
#if NUM_TEXTURES > 3
			  , uniform sampler2D samplerDiffuse3
#endif
#endif
#endif
			  ) : COLOR0
{
	// modulate the layered textures
	//
#if LOWEND	
	float4 vFinalColor = tex2D(samplerDiffuse0, vertex._uv0);
#else
	float4 vFinalColor = tex2Dbias(samplerDiffuse0, float4(vertex._uv0.x, vertex._uv0.y, 0, -1) );
#endif	
#if NUM_TEXTURES > 1
	vFinalColor *= tex2D(samplerDiffuse1, vertex._uv1);
#if NUM_TEXTURES > 2
	vFinalColor *= tex2D(samplerDiffuse2, vertex._uv2);
#if NUM_TEXTURES > 3
	vFinalColor *= tex2D(samplerDiffuse3, vertex._uv3);
#endif
#endif
#endif

#ifdef VERTEX_COLOR
	vFinalColor *= vertex._color;
#endif

	// fog it
	//
	
	
	vFinalColor = getFogColor(vFinalColor, vertex._fogValue);
	
	return vFinalColor;
}
