//--------------------------------------------------------------------------
//
// 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

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;

//--------------------------------------------------------------------------
struct VS_INPUT
{
 float4 vPos : POSITION; //Pos
 float3 vNormal : NORMAL; //Normal
 float3 tangent	: TANGENT;
 float3 vTexCoord0 : TEXCOORD0;  //Tex0
};
//--------------------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Position   : POSITION;
    float tLight    : TEXCOORD0;
    float2  Tex0            : TEXCOORD1;
};



//--------------------------------------------------------------------------
struct PS_OUTPUT
{
 float4  Col        : COLOR;
};

//--------------------------------------------------------------------------
VS_OUTPUT VertShadow(VS_INPUT i)
{
    VS_OUTPUT Output;

    // Transform the position from object space to homogeneous projection space
    Output.Position = mul(i.vPos, g_mWorldViewProjection);
    Output.tLight    = Output.Position.z/Output.Position.w;////

	Output.Tex0 = i.vTexCoord0.xy;
    
    return Output;  
}

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

 AddressU = Wrap; 
 AddressV = Wrap; 
 AddressW = Wrap; 
};
//--------------------------------------------------------------------------
PS_OUTPUT PixShadow(VS_OUTPUT In)
{
 PS_OUTPUT o;
 float4    tDiffuse; 
  
 	if(AlphaDraw)
		{
			tDiffuse = tex2D(sampler0, In.Tex0); 
 			if(tDiffuse.a <= 0.6f)//0.3f
			{
				clip(-1);
			} 		
		} 
		
 o.Col = 0;
 o.Col.r = In.tLight;

 
 return o;
}
//--------------------------------------------------------------------------
technique RenderScene
{ 
 pass p0 // Make shadow
 {      
	VertexShader = compile vs_2_0 VertShadow();
	PixelShader = compile ps_2_0 PixShadow();
 }
}
//--------------------------------------------------------------------------