//
// You are free to use this code in part or whole in any way you seem fit!
// If you find this code useful we would like to know how,
// you can find us at www.spellofplay.com feel free to drop by!
//

float4 g_color;
float3 g_eyePos;
float4x4 g_worldMat : World;   // World
float4x4 g_viewMat;
float4x4 g_projMat;


texture g_texture0 : texture0;
texture g_texture1 : texture1;

float3 g_lightDir = (0.0, 0.0, 1.0);

sampler g_diffuseMap = sampler_state {
   Texture = (g_texture0);
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
   AddressU = CLAMP;
   AddressV = CLAMP;
};
sampler g_specMap = sampler_state {
   Texture = (g_texture1);
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
   AddressU = CLAMP;
   AddressV = CLAMP;
};

struct PERPIXEL_VS_OUTPUT { 
	float4 m_pos    : POSITION;
	float2 m_uv		: TEXCOORD0;
	float3 m_normal : TEXCOORD1;
	float3 m_view   : TEXCOORD2;
};

struct STANDARD_VS_OUTPUT { 
	float4 m_pos    : POSITION;
	float4 m_color : COLOR0;
	float2 m_uv		: TEXCOORD0;
};

// Material properties
float4 g_ambient = ( 0.93, 0.94, 0.94, 1.00 );
float g_ka = 0.4;

float4 g_diffuse = ( 1.0, 1.0, 1.0, 1.00 );
float g_kd = 0.8;

float4 g_specular = ( 1.00, 1.0, 1.0, 1.00 );
float g_specularN = 100.00;
float g_ks = 1.00;

PERPIXEL_VS_OUTPUT PerPixel_VS_Main(float4 a_pos  : POSITION, float3 a_normal : NORMAL, float2 a_uv  : TEXCOORD0) {
   PERPIXEL_VS_OUTPUT Out = (PERPIXEL_VS_OUTPUT) 0; 
   
   //CalcMatrices();
   
   	float4x4 wvMatrix = mul(g_worldMat, g_viewMat);
    float4x4 wvpMatrix = mul(wvMatrix, g_projMat);

   // Output transformed position:
   Out.m_pos = mul(a_pos, wvpMatrix);
   Out.m_uv = a_uv;
   
   // Transform the input normal to world space:
   Out.m_normal = mul(a_normal, g_worldMat).xyz;
   Out.m_view = mul(a_pos, g_worldMat).xyz;
   //Out.m_normal = a_normal;
   //Out.m_view = a_pos;
   
   // get camera position
   //g_eyePos.x = 0;
   //g_eyePos.z = 0;
   //Out.m_view.x = 0;
   //Out.m_view.z = 0;
   Out.m_view = g_eyePos - Out.m_view;
     
   //Out.m_view = float3(0, 1, 0);
   
   return Out;
}

STANDARD_VS_OUTPUT Standard_VS_Main(float4 a_pos  : POSITION, float3 a_normal : NORMAL, float2 a_uv  : TEXCOORD0) {
   STANDARD_VS_OUTPUT ret = (STANDARD_VS_OUTPUT) 0; 
   
    float4x4 wvMatrix = mul(g_worldMat, g_viewMat);
    float4x4 wvpMatrix = mul(wvMatrix, g_projMat);

   // Output transformed position:
   ret.m_pos = mul(a_pos, wvpMatrix);
   ret.m_uv = a_uv;
   
   // Transform the input normal to world space:
/*   a_normal = normalize(mul(a_normal, g_worldMat).xyz);
   float3 view = normalize(g_eyePos - mul(a_pos, g_worldMat).xyz);
   
   float diff = dot(a_normal, g_lightDir);
   float4 ambientColor = g_ambient * g_ka * g_ka;
   float4 diffuseColor = g_diffuse * g_kd * saturate(diff);
   float3 vReflect = normalize(2.0 * diff * a_normal - g_lightDir);       
	float4 specularColor = g_specular * g_ks * pow(saturate(dot(vReflect, view)), g_specularN);
	*/
	ret.m_color.r = 1.0f;// = /*ambientColor + */diffuseColor + specularColor;
	ret.m_color.b = 1.0f; //+= g_shieldColor;
	ret.m_color.g = 1.0f; //+= g_shieldColor * 0.5f;
	ret.m_color.a = 1.0f;
   
   return ret;
}

float4 PerPixel_PS_Main(float2 a_uv : TEXCOORD0, float3 a_normal : TEXCOORD1, float3 a_view : TEXCOORD2) : COLOR {

   // Compute the reflection vector:
   a_normal = normalize(a_normal);
   

   // Compute ambient term:
   float4 ambientColor = g_ambient * g_ka;

   // Compute diffuse term:
   float diff = dot(a_normal, g_lightDir);
   float4 finalColor = tex2D(g_diffuseMap, a_uv);
   float4 colorMapColor = g_color * finalColor.a;
   finalColor.a = 1.0f;
   
	float4 diffuseColor = g_diffuse * g_kd * saturate(diff);

	// Compute specular term:
	a_view = normalize(a_view);
	
	float3 vReflect = normalize(2.0 * diff * a_normal - g_lightDir);       
	float4 specularColor = g_specular * g_ks * pow(saturate(dot(vReflect, a_view)), g_specularN);

	
	finalColor = (ambientColor + diffuseColor) * finalColor + colorMapColor + specularColor * tex2D(g_specMap, a_uv);
   finalColor.a = 1.0f;
   
   return finalColor;
}

technique PerPixel {
   pass Single_Pass {
   		ZFunc = LESS;
   		ZEnable = TRUE;
      SrcBlend = SRCALPHA;
      DestBlend = ZERO;
      FogEnable = FALSE;
      Lighting = FALSE;
      AlphaBlendEnable = True;
      CullMode = CCW;
      VertexShader = compile vs_1_1 PerPixel_VS_Main();
      PixelShader = compile ps_2_0 PerPixel_PS_Main();
   }
}

technique SimpleTerrain_NoPS {
  pass Single_Pass {
     	ZFunc = LESS;
   	  ZEnable = TRUE;
      SrcBlend = ONE;
      DestBlend = ZERO;
      FogEnable = FALSE;
      Lighting = FALSE;
      CullMode = CCW;
      //TexCoordIndex[0] = PASSTHRU;
      Texture[0] = <g_texture0>;
      
      VertexShader = compile vs_1_1 Standard_VS_Main();
      PixelShader = NULL;
   }
}




