//Shader code by Mark Blosser
//WEBSITE:  www.hyrumark.com
//EMAIL:  mark@hyrumark.com

//Description:  Shader for dynamic entities, normal mapping and specular.  "LightSource" variable is pulled from FPSC for accurate lighting.
//Textures: 
//"texture_D.dds"      diffuse texture
//"texture_I.dd"       specular texture
//"texture_N.dds"       normal map texture
/************* UNTWEAKABLES **************/

float4x4 World      : WORLD;

float4x4 WorldViewProj : WorldViewProjection;
float4x4 WorldIT : WorldInverseTranspose;
float4x4 ViewInv : ViewInverse;
float4 eyePos : CameraPosition;



/******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";
> = {1.0f, 1.0f, 1.0f, 1.0f};

float4 LightSource
<
    string UIType = "Fixed Light Source";
> = {500000.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";
>;






//Diffuse Texture _D
sampler2D DiffuseSampler = sampler_state
{
    Texture   = <DiffuseMap>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

//Effect Texture _I (could be anything here - ill,spec,normal,cube)
sampler2D EffectSampler = sampler_state
{
    Texture   = <EffectMap>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

//Effect Texture _N (could be anything here - ill,spec,normal,cube)
sampler2D NormalSampler = sampler_state
{
    Texture   = <NormalMap>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

;





/************* 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
{
    
    float4 diffuse = tex2D(DiffuseSampler,IN.TexCoord.xy);    //sample diffuse texture    
    float4 effectmap = tex2D(EffectSampler,IN.TexCoord.xy);    //sample specular map texture 
    float4 normalmap = tex2D(NormalSampler,IN.TexCoord.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) + (1.0*(normalmap.x * Tn + normalmap.y * Bn));
    Nb = normalize(Nb);

   
     
    
    //normalized view vector for accurate specular highlights from LightSource
    float3 V  = normalize(eyePos - IN.WPos);
	
        
    //half vector
    float3 Hn = normalize(V+Ln);
    
    
   //calculate lighting Half Lamert scale and bias 
    float4 lighting = lit(pow(0.5*(dot(Ln,Nb))+0.5,2),dot(Hn,Nb),32);
    //float4 lighting = lit(((dot(Ln,Nb))),dot(Hn,Nb),24);
//SurfColor = SurfColor + 0.3;
SurfColor = clamp(SurfColor,0.2,0.55);
    
    
    //diffuse light contribution
    float4 diffuseContrib = diffuse *1.0* lighting.y * (SurfColor) +(AmbiColor*diffuse);
    
    //specular light contribution
    float4 specContrib = lighting.z * effectmap*(SurfColor) ;
    
        
    
    float4 result =  diffuseContrib + (2*specContrib);
    
    
        
    
    
    
    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();
    }
}
