/*********************************************************************NVMH3****

Terrain rendering.
This is a port of the old DX8 ASM shaders --
but with a normal map per layer, and support for 3 directional lights and 3 points.

******************************************************************************/
#include "..\mrt.fx"

float4x4 worldViewProj : WorldViewProjection;
float4x4 viewProj : ViewProjection;
float4x4 world   : World;

float3 campos : CAMERAPOS;

struct DirectionalLight
{
    float3 Direction    : DIRECTION;
    float3 Diffuse      : DIFFUSE;
    float3 Specular     : SPECULAR;
};


struct PointLight
{
    float4 Position_Att0     : POSITION_ATT0;
    float4 Diffuse_Att1      : DIFFUSE_ATT1;
    float4 Specular_Att2     : SPECULAR_ATT2;
};

struct Material
{
    float4 Diffuse_Opacity  : DIFFUSE_OPACITY;
    float4 Specular_Power : SPECULAR_POWER;
    float3 Ambient  : AMBIENT;
    float3 Emissive : EMISSIVE;
};

//------------------------------------
struct vertexInput {
    float4 position				: POSITION;
    float4 blendgoo				: COLOR0;
    float3 normal               : NORMAL;
    float4 tangentVec			: TANGENT;
    float2 noisefactor			: COLOR1;
};    

struct vertexOutput
{
	float4 hPosition : POSITION;
    float4 posWorld         : TEXCOORD0;
    float2 texCoordDiffuse0	: TEXCOORD1;
    float4 posView          : TEXCOORD2;
};

struct vertexOutputExtents
{
	float4 hPosition : POSITION;
    float4 posWorld         : TEXCOORD0;
    float2 texCoordDiffuse0	: TEXCOORD1;
    float4 posView          : TEXCOORD2;   
	float4 posShadowMap		: TEXCOORD4;
};


float WorkingSaturate(float x)
{
    if (x<0)
        x = 0;
    else if (x>1)
        x = 1;
    return x;
}

float4x4 LightViewProj0 : LightViewProj0;

texture ShadowMap0 : ShadowMap0;
sampler ShadowMapTextureSampler0 = sampler_state 
{
    texture = <ShadowMap0>;
    MinFilter = Point;
    MagFilter = Point;
    MipFilter = Point;
    AddressU = Border;
    AddressV = Border;
    //BorderColor = 0xFFFFFFFF;
    BorderColor = 0;
};



//------------------------------------
vertexOutput VS_TransformAndTexture(vertexInput IN) 
{
    vertexOutput OUT;
    
    OUT.hPosition = mul( IN.position, worldViewProj);
    
    float4 posWorld = mul(IN.position, world);   
    OUT.posWorld = posWorld;
    
    float4 baseUVs = float4(posWorld.x,0,-posWorld.z,1); 	
      
	OUT.texCoordDiffuse0.x = baseUVs.x;
    OUT.texCoordDiffuse0.y = -baseUVs.z;
    
    OUT.posView = mul( posWorld , viewProj);
    return OUT;
}


//-----------------------------------
PS_OUTPUT PS_Textured( vertexOutput IN)
{ 		 
	PS_OUTPUT Out;	   
 	float depth = IN.posView.z / IN.posView.w;
    float4 outColor = float4(depth, 0, 0, 1);

    SetPSOutput( Out, outColor, float4(0, 0, 0, 1) );
    return Out;
}



//------------------------------------
vertexOutputExtents VS_TransformExtents(vertexInput IN) 
{
    vertexOutputExtents OUT;
    
    OUT.hPosition = mul( IN.position, worldViewProj);
    
    float4 posWorld = mul(IN.position, world);   
    OUT.posWorld = posWorld;
    
    float4 baseUVs = float4(posWorld.x,0,-posWorld.z,1); 	
      
	OUT.texCoordDiffuse0.x = baseUVs.x;
    OUT.texCoordDiffuse0.y = -baseUVs.z;
    
    OUT.posView = mul( posWorld , viewProj);
    
    OUT.posShadowMap = mul( posWorld, LightViewProj0 );
   
    return OUT;
}


//-----------------------------------
PS_OUTPUT PS_Extents( vertexOutputExtents IN)
{ 		 
	PS_OUTPUT Out;	   
 	
 	 float4 shadowdepth = tex2Dproj(ShadowMapTextureSampler0, IN.posShadowMap);   
    float4 posShadowMap = IN.posShadowMap;
    float fDepth = (posShadowMap.z / posShadowMap.w) - 0.000000;
    float islit = (shadowdepth.r >= fDepth ); 


    float4 outColor = float4( 1.0f, islit, islit, 1);

    SetPSOutput( Out, outColor, float4( campos - IN.posWorld.xyz, 1) );
    return Out;
}


//-----------------------------------
technique depthRender
{
    pass p0 
    {		      
		VertexShader = compile vs_3_0 VS_TransformAndTexture();
		PixelShader  = compile ps_3_0 PS_Textured();
    }
}


technique displayExtents
{
    pass p0 
    {		      
		VertexShader = compile vs_3_0 VS_TransformExtents();
		PixelShader  = compile ps_3_0 PS_Extents();
    }
}
