//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;
float sintime : SinTime;



/******TWEAKABLES***************************/

float SpecExpon : Power
<
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 128.0;
    float UIStep = 1.0;
    string UIName =  "specular power";
> = 60.000000;


/******VALUES PULLED FROM FPSC - NON TWEAKABLE**********/

float4 AmbiColor : Ambient
<
    string UIName =  "Ambient Light Color";
> = { 0.100000, 0.100000, 0.100000, 1.000000 };

float4 SurfColor : Diffuse
<
    string UIName =  "Surface Color";
    string UIType = "Color";
> = { 1.000000, 1.000000, 1.000000, 1.000000 };

float4 LightSource
<
    string UIType = "Fixed Light Source";
> = { 500000.000000, 50000.000000, -0.000000, 1.000000 };




/****************** TEXTURES AND SAMPLERS*********************/



texture DiffuseMap : DiffuseMap
<
    string Name = "D.tga";
    string type = "2D";
 string ResourceName = "atlas_test_d.png"; >;

//could be anything here - ill,spec,normal,cube
texture EffectMap : DiffuseMap
<
    string Name = "I.tga";
    string type = "2D";
 string ResourceName = ""; >;

texture NormalMap : DiffuseMap
<
    string Name = "N.tga";
    string type = "2D";
 string ResourceName = "atlas_test_ln.png"; >;
texture EnvironmentMap 
< 
    string type = "2D"; 
    string name = "i.tga"; 
 string ResourceName = ""; >;






//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;
    
};



/*data passed to pixel shader*/
struct vertexOutput
{
    float4 Position    : POSITION;
    float2 TexCoord     : TEXCOORD0;
    float3 WorldNormal	: TEXCOORD1;
    float4 WPos : TEXCOORD2;
    float2 atlasUV:TEXCOORD3;
    //float3 LightVec	    : TEXCOORD4;
    
};


/*******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.LightVec = normalize (LightSource - worldSpacePos );
    OUT.Position = mul(IN.Position, WorldViewProj);
    OUT.TexCoord  = IN.UV0; 
    
    OUT.WPos =   worldSpacePos; 
    
     /****Calculate a set of texture coordinates to perform atlas (sprite) walking*********/
	float2 DimensionsXY = float2(6,10); 
	float2 atlasUVtemp = IN.UV0; 
	float framespersec = 25;  //speed of animation in frames per second
	float looptime = 180; //looptime in seconds
			
	float loopcounter  = floor(time/looptime); //increments by one every 50 seconds (or whatever "looptime" is)
	float offset ;  //initialize offset value used below
	
	offset = looptime*loopcounter; //offset time value -increments every looptime
	
		 
	float speed =(time*framespersec) - (offset*framespersec) ;
	
	
	float index = floor( speed);		//floor of speed
	float rowCount = floor( (index / DimensionsXY.y) );		//floor of (speed / Ydimension.g)
	float2 offsetVector = float2(index, rowCount);
	float2 atlas = (1.0 / DimensionsXY) ;
	float2 move = (offsetVector + atlasUVtemp);
	
	OUT.atlasUV = (atlas.xy *move);
	/*******************************************************************************************/                                                                        
                                           

    return OUT;
}


/****************Framgent Shader*****************/

float4 mainPS(vertexOutput IN) : COLOR
{
    
    
    
    
    float4 diffuse = tex2D(DiffuseSampler,IN.atlasUV);    //sample diffuse texture    
    //float4 effectmap = tex2D(EffectSampler,IN.TexCoord.xy);    //sample specular map texture 
   // float3 normalmap = tex2D(NormalSampler,IN.atlasUV.xy) *2-1 ;  //sample and expand normal map

    //float3 Ln = (IN.LightVec);
    float3 Nn = (IN.WorldNormal);
    //float3 Tn = (IN.WorldTangent);
    //float3 Bn = (IN.WorldBinorm);
    //float3 Nb = (normalmap.z * Nn) + (0.4*(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);
    //float3 V = ViewVector;
    
    //reflection mapping for windows
    //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),48);
    
         
    //give a slight boost to AmbiColor and clamp, prevents diffuse lighting from getting to dark on non-lit side
    AmbiColor = AmbiColor+0.13;
    AmbiColor = clamp(AmbiColor,0,1);

    
    
    //diffuse light contribution
    //float4 diffuseContrib = diffuse * 1.0* lighting.y * (SurfColor) +(AmbiColor*diffuse);
    
    //specular light contribution
   // float4 specContrib = lighting.z * effectmap*(SurfColor+0.1) ;
    
    //Cubemap reflection contribution
    //float4 Reflectivity = Env*SurfColor+AmbiColor;
    
        
    
    float4 result =  diffuse;
    //float4 result = normalmap;
    //float4 result = Env;
    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();
    }
}

