//Description:  Shader for dynamic entities, normal mapping,specular, reflectivity,
// and "hero" spec that always faces player.  "LightSource" variable is pulled from FPSC for accurate lighting.
//
//Textures: 
//"texture_D.dds"      diffuse texture
//"texture_I.dd"       specular texture, alpha channel is reflectivity mask
//"texture_N.dds"       normal map texture
//"texture_S.dds"       reflectivity texture
/************* UNTWEAKABLES **************/

float4x4 World      : WORLD;

float4x4 WorldViewProj : WorldViewProjection;
float4x4 WorldIT : WorldInverseTranspose;
float4x4 ViewInv : ViewInverse;
float4 eyePos : CameraPosition;
float time: Time;




/******TWEAKABLES***************************/

float SpecExpon : Power
<
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 128.0;
    float UIStep = 1.0;
    string UIName =  "specular power";
> = 64.0;


/******VALUES PULLED FROM FPSC - NON TWEAKABLE**********/

float4 AmbiColor : Ambient
<
    string UIName =  "Ambient Light Color";
> = {0.1f, 0.1f, 0.1f, 1.0f};

float4 SurfColor : Diffuse
<
    string UIName =  "Surface Color";
    string UIType = "Color";
> = {0.50f, 0.50f, 0.50f, 1.0f};

float4 LightSource
<
    string UIType = "Fixed Light Source";
> = {50000.0f,50000.0f, -0.0f, 1.0f};




/****************** TEXTURES AND SAMPLERS*********************/



texture DiffuseMap : DiffuseMap
<
    string Name = "D.tga";
    string type = "2D";
>;

//could be anything here - ill,spec,normal,cube
texture EffectMap : DiffuseMap
<
    string Name = "I.tga";
    string type = "2D";
>;

texture NormalMap : DiffuseMap
<
    string Name = "N.tga";
    string type = "2D";
>;
texture EnvironmentMap 
< 
    string type = "2D"; 
    string name = "i.tga"; 
>;






//Diffuse Texture _D
sampler2D DiffuseSampler = sampler_state
{
    Texture   = <DiffuseMap>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
    AddressU = WRAP;
	AddressV = WRAP;

};

//Effect Texture _I (could be anything here - ill,spec,normal,cube)
sampler2D EffectSampler = sampler_state
{
    Texture   = <EffectMap>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
    AddressU = WRAP;
	AddressV = WRAP;

};

//Effect Texture _N (could be anything here - ill,spec,normal,cube)
sampler2D NormalSampler = sampler_state
{
    Texture   = <NormalMap>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
    AddressU = WRAP;
	AddressV = WRAP;

};

//Effect Texture _S   fake cubemap texture (blurred 2d image)
sampler EnvironmentSampler = sampler_state
{ 
    Texture = (EnvironmentMap);
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
    AddressU = WRAP;
	AddressV = WRAP;

};

;





/************* DATA STRUCTS **************/

struct appdata {
    float4 Position	: POSITION;
    float4 UV0		: TEXCOORD0;
    float4 Normal	: NORMAL;
    float4 Tangent	: TANGENT0;
    float4 Binormal	: BINORMAL0;
};



/*data passed to pixel shader*/
struct vertexOutput
{
    float4 Position    : POSITION;
    float2 TexCoord     : TEXCOORD0;
    float3 LightVec	    : TEXCOORD2;
    float3 WorldNormal	: TEXCOORD3;
    float3 WorldTangent	: TEXCOORD4;
    float3 WorldBinorm	: TEXCOORD5;
    float4 WPos : TEXCOORD6;
    
    
};


/*******Vertex Shader***************************/

vertexOutput mainVS(appdata IN)   
{
    
	vertexOutput OUT;
    
    //float4 tempPos = float4(IN.Position, 1);
    float4 worldSpacePos = mul(IN.Position, World);
    
    OUT.WorldNormal = normalize(mul(IN.Normal, WorldIT).xyz);
    OUT.WorldTangent = mul(IN.Tangent, WorldIT).xyz;
    OUT.WorldBinorm = mul(IN.Binormal, WorldIT).xyz;


    OUT.LightVec = normalize (LightSource - worldSpacePos );
    OUT.Position = mul(IN.Position, WorldViewProj);
    OUT.TexCoord  = IN.UV0; 
    
    OUT.WPos =   worldSpacePos; 
    
    
	/*******************************************************************************************/                                                                        
                                           

    return OUT;
}


/****************Framgent Shader*****************/

float4 mainPS(vertexOutput IN) : COLOR
{
    
    //Code chunk to keep time value from getting too high/////////////////////
    float looptime = 190; //looptime in seconds
	float loopcounter  = floor(time/looptime); //increments by one every 190 seconds (or whatever "looptime" is)
	float offset ;  //initialize offset value used below
	offset = looptime*loopcounter; //offset time value -increments every looptime
	float speed =(time) - (offset) ;
    ///////////////////////////////////////////////////////////////////////////
    
    //scrolling the uv coordinates
    float2 scroll = IN.TexCoord + float2(-0.0,-0.1)*speed;
   
    float4 diffuse = tex2D(DiffuseSampler,scroll.xy);    //sample diffuse texture    
    float4 effectmap = tex2D(EffectSampler,IN.TexCoord.xy);    //sample specular map texture 
    float3 normalmap = tex2D(NormalSampler,scroll.xy) *0-1 ;  //sample and expand normal map

    float3 Ln = (IN.LightVec);
    float3 Nn = normalize(IN.WorldNormal);
    float3 Tn = normalize(IN.WorldTangent);
    float3 Bn = normalize(IN.WorldBinorm);
    float3 Nb = (normalmap.z * Nn) + (1.0*(normalmap.x * Tn + normalmap.y * Bn));
    Nb = normalize(Nb);
    
    
    
    //normalized view vector for accurate specular highlights from LightSource
    float3 ViewVector  = (eyePos - IN.WPos);
    float3 Vn = normalize(ViewVector);
    
    
    //reflection mapping for windows and flat surfaces
    float3 G = normalize(2 * dot(Nb, Vn) * Nb - Vn);                // glance vector (view space)
    float3 Reflection = float3(-G.x, G.y, -G.z);
    float4 Env = texCUBE(EnvironmentSampler,Reflection);
	
        
    //half vectors
    float3 Hn = normalize(Vn+Ln);
    
        
   //calculate lighting Half Lamert scale and bias 
    //float4 lighting = lit(pow(0.5*(dot(Ln,Nb))+0.5,1.0),dot(Hn,Nb),24);
    float4 spec = pow(max(dot(Nb,Hn),0),24);

    
     
    
        
    //specular light contribution
    float4 specContrib = spec * effectmap ;
    
    //Cubemap reflection contribution
    float4 Reflectivity = Env;
    
        
    float4 result =  (0.6*diffuse*SurfColor)+(0.4*Reflectivity)+1.5*specContrib;
    
    result.w = 0.5 * diffuse.w;
   
    
        
    
    
    
    return result;
}


/****** technique ********************************/

technique dx9textured
{
    pass P0
    {
        // lighting
        Lighting       = FALSE;
        FogEnable      = FALSE;
         

        // samplers
        //Sampler[0] = (LightmapSampler);
        //Sampler[1] = (DiffuseSampler);
        //Sampler[2] = (IllSpecSampler);
		//Sampler[3] = (NormalSampler);

        // shaders
        VertexShader = compile vs_2_0 mainVS();
        PixelShader  = compile ps_2_0 mainPS();
    }
}
