//--------------------------------------------------------------------------
//
// WHALE ENGINE 1.0
// LGM GAMES
// DIRECT X9c
// SHADER v2.0
//          
//--------------------------------------------------------------------------
 
#define SHADOW_EPSILON 0.00005f

//--------------------------------------------------------------------------
bool bUseShadows;
int ShadowMapSize = 2048;
           
texture TextureDifuse;                      
texture TextureNormalMap;
texture TextureLightMap;   
texture TextureCubeMap;
                 
//texture TextureCubeMapEnvLight;  
//texture TextureCubeMapEnvReflect;   
                
texture TextureShadowMap;
texture TextureEffect;

// Effect texture stuff
int iEffectMode = 0;
float fEffectPower = 1.0f; // 0-1
float fEffectTime = 0.0f;
bool AlphaDraw;
/**/
float4x4 g_mWorld;                // World matrix for object
float4x4 g_mWorldViewProjection;  // World * View * Projection matrix
float4x4 WorldView;   
float4x4 ViewProjectionLight;
float4x4 WorldViewProjection;     // used for make shadow, not standard parameter
float4x4 g_mShadowProjection;

float    g_fTime;                    
vector   CameraPosition;
float4	 UV = { 0.0f, 1.0f, 0.0f, 1.0f }; 
vector   ExtraVector = { 0.5f, 0.5f, 0.5f, 0.5f };
vector   ColorVector = { 1.0f, 1.0f, 1.0f, 1.0f };
                                          
// Material stuff
float SpecularLevel = 32;
float SpecularPower = 0.4f;
float SpecularMapFac = 4.0f;
//vector LightColor = {0.70f, 0.70f, 0.70f, 1.0f};//{1.0f, 1.0f, 1.0f, 1.0f};
vector LightDir = {0.5f, -0.55f, 0.5f, 1.0f}; // light Direction
//vector Ambient =  {0.1f, 0.1f, 0.1f, 1.0f}; 
//float LightPower = 1.0f;

float4x4	 EnvMapFactors;// = { 4.0f, 0.25f, 0.3f, 1.0f }; 
/*float EnvPower = EnvMapFactors.x;
float EnvAdd = EnvMapFactors.y;
float EnvTexOffset = EnvMapFactors.z;
float EnvLightPower = EnvMapFactors.w;
float EnvPower = 4;
float EnvAdd = 0.25f;
float EnvTexOffset = 0.3;
float EnvLightPower = 1.0f;
*/

//float4	RefMapFactors = { 2.0f, 12.0f, 0.5f, 1.0f };
/*float RefPower = 2;
float RefFactor = 12;
float RefTexOffset = 0.5f;*/
	
float DetailMapFactor = 12;
float TileTexture = 1;

// Fog stuff
vector FogColor = {0.0f, 0.0f, 0.0f, 1.0f};
float FogStart = 1000.0;
float FogEnd = 1050.0;

float FogColFac = 1.0f;

//--------------------------------------------------------------------------
/*struct VS_INPUT
{
 float4 vPos : POSITION; //Pos
 float3 vNormal : NORMAL; //Normal
 float3 tangent	: TANGENT;
 float3 vTexCoord0 : TEXCOORD0;  //Tex0
};*/
struct VS_INPUT
{
 float4 vPos : POSITION; //Pos
 float3 vNormal : NORMAL; //Normal
 float4 Diffuse    : COLOR0;     // vertex diffuse color
 float2 vTexCoord0 : TEXCOORD0;  //Tex0
};

//--------------------------------------------------------------------------
struct VS_OUTPUT
{
 float4  Pos             : POSITION;
 float   Depth			 : TEXCOORD0;
 float4  PosInCameraSpace : TEXCOORD1;
 float2  Tex0            : TEXCOORD2;
// float3  tLight          : COLOR;
// float3  tH              : TEXCOORD0;
// float2  Tex0            : TEXCOORD1;
// float3  NormalWorld     : TEXCOORD2;
// float3  tCamera         : TEXCOORD3;
// float4  CubeReflect     : TEXCOORD4;
// float4  PosInLightSpace : TEXCOORD5;
};

//--------------------------------------------------------------------------
float3 CalcReflectionVector(float3 ViewToPos, float3 Normal)
{
 return reflect(ViewToPos, Normal);
}
//--------------------------------------------------------------------------
VS_OUTPUT VShade(VS_INPUT i)
{
 VS_OUTPUT   o;
 float4 oPos = mul(i.vPos, g_mWorldViewProjection); 
 o.Pos = oPos;
 o.Depth = oPos.z/oPos.w;
 o.PosInCameraSpace = oPos;//mul(i.vPos, g_mWorldViewProjection); 
 o.Tex0 = i.vTexCoord0;
 
 return o;
}    

//--------------------------------------------------------------------------
uniform sampler2D sampler0 = 
sampler_state 
{
 texture = <TextureDifuse>;
 MinFilter = Linear;
 MagFilter = Linear;
 MipFilter = Linear;

 AddressU = Wrap; 
 AddressV = Wrap; 
 AddressW = Wrap; 
};

//--------------------------------------------------------------------------
uniform sampler2D sampler1 = 
sampler_state 
{
 texture = <TextureNormalMap>;
 MinFilter = Linear;
 MagFilter = Linear;
 MipFilter = Linear;

 AddressU = Wrap; 
 AddressV = Wrap; 
 AddressW = Wrap; 
};
//--------------------------------------------------------------------------
uniform samplerCUBE samplerCubeEnvLight = 
sampler_state 
{
 texture = <TextureCubeMap>;
 MinFilter = Linear;
 MagFilter = Linear;
 MipFilter = Linear;

 AddressU = Wrap; 
 AddressV = Wrap; 
 AddressW = Wrap; 
};/**/
//--------------------------------------------------------------------------
/*uniform samplerCUBE samplerCubeEnvReflect = 
sampler_state 
{
 texture = <TextureCubeMapEnvReflect>;
 MinFilter = Linear;
 MagFilter = Linear;
 MipFilter = Linear;

 AddressU = Wrap; 
 AddressV = Wrap; 
 AddressW = Wrap; 
};*/
//--------------------------------------------------------------------------
uniform sampler2D samplerShadow =
sampler_state
{
 Texture = <TextureShadowMap>;
 MinFilter = Point;
 MagFilter = Point;
 MipFilter = Point;
 AddressU = Clamp;
 AddressV = Clamp;
};
//--------------------------------------------------------------------------
uniform sampler2D samplerEffect = 
sampler_state 
{
 texture = <TextureEffect>;
 MinFilter = Linear;
 MagFilter = Linear;
 MipFilter = Linear;

 AddressU = Wrap; 
 AddressV = Wrap; 
 AddressW = Wrap; 
};
//--------------------------------------------------------------------------
struct PS_INPUT2
{
 float4  Pos             : POSITION;
 float   Depth			 : TEXCOORD0;
 float4  PosInCameraSpace : TEXCOORD1;
 float2  Tex0            : TEXCOORD2;
};

//--------------------------------------------------------------------------
struct PS_OUTPUT
{
 float4  Col        : COLOR;
 float4  ColAO        : COLOR1;
};
//-------------------------------------------------------------------------- 
PS_OUTPUT PShade(PS_INPUT2 i)
{
 PS_OUTPUT o;
 float4    tDiffuse; 
 
	if(AlphaDraw)
		{
			tDiffuse = tex2D(sampler0, i.Tex0); 
 			if(tDiffuse.a <= 0.6f)//0.3f
			{
				clip(-1);
			} 		
		} 
/**/

 o.Col = 0;  
 o.ColAO = 0; 
  
 o.Col.r = i.Depth;
 o.ColAO.r = i.PosInCameraSpace.w / 1200.0f;
// 
 //o.Col = 1;  
// o.ColAO = 1; 
 /*if(tDiffuse.a > 0.3f)
			{
				 o.Col.r = tDiffuse.a;
				  o.ColAO.r = tDiffuse.a;
			} */

 //float depthSample = i.Depth;
 //float depthWater = i.PosInCameraSpace.z/ i.PosInCameraSpace.w;
   
		//Lineariziraj depth
		//i.PosInCameraSpace.z = depthSample;
		//i.PosInCameraSpace.w = 1;
     /*    float4 depthViewSample = mul( i.PosInCameraSpace, g_mShadowProjection );
       i.PosInCameraSpace.z = depthWater;
		float4 depthViewWater = mul( i.PosInCameraSpace, g_mShadowProjection );   
   
		float depthDiff = depthViewSample.z/depthViewSample.w - depthViewWater.z/depthViewWater.w;
		
		float DFog = saturate( abs(depthDiff * 1.0f) );//ExtraVector.x
*/
		//float4 depthLinMB = mul( depthWater, g_mShadowProjection );   
/*		//float4 depthView = mul( i.PosInCameraSpace, g_mShadowProjection ) ;i.PosInCameraSpace.z * 
		if(AlphaDraw)
		{
			tDiffuse = tex2D(sampler0, i.Tex0); 
			if(tDiffuse.a < 1.0f)
			{
				clip(-1);
			
			}
		o.Col.r = depthViewSample.z/depthViewSample.w;//;depthWater;//saturate( )
		}
*/ 	 

/* */	
 


 
 return o;
}

//--------------------------------------------------------------------------
technique RenderScene
{ 
 pass p0 
 {      
  VertexShader = compile vs_2_0 VShade();

  PixelShader = compile ps_2_0 PShade();
  
  FogEnable = false;
  CullMode = NONE;
  //CullMode = CW;//CW;//
  //ColorWriteEnable = red | green | blue;
  ZEnable = true;
  ZWriteEnable = true;
/*  ZFunc = ALWAYS;
  //DepthBias = 0.0000001f;*/
 }

}
//--------------------------------------------------------------------------