//
// Basic Shading Effect File
//

float4 MaterialAmbient : MATERIALAMBIENT = {0.1f, 0.1f, 0.1f, 1.0f};
float4 MaterialDiffuse : MATERIALDIFFUSE = {0.8f, 0.8f, 0.8f, 1.0f};
float4 MaterialSpecular : MATERIALSPECULAR = {1.0f, 1.0f, 1.0f, 7.0f};
float4x3 TransformMatrix;
float4x4 ProjectionMatrix;
float	 alpha	= 1.0f;

// light information
vector LightVector;
vector LightPos;
vector EyePos;
float4 LightColor;

// direction of light from sky (view space)
float3 DirFromSky < string UIDirectional = "Direction from Sky"; > = { 0.0f, -1.0f, 0.0f };            

// light intensity
float4 I_a = { 0.5f, 0.5f, 0.5f, 1.0f };    // ambient
float4 I_b = { 0.0f, 0.0f, 0.0f, 1.0f };    // ground
float4 I_c = { 1.0f, 1.0f, 1.0f, 1.0f };    // sky

int    power   = 8;

///////////////////////////////////////////////////////
struct VS_INPUT
{
    float3  Pos             : POSITION;
    float3  Normal          : NORMAL;
    float2  Tex0            : TEXCOORD0;
};

struct VS_OUTPUT
{
    float4  Pos     : POSITION;
    float4  Diffuse  : COLOR0;
    float4  Specular : COLOR1;
    float2  Tex0    : TEXCOORD0;
};

// Vertex Shader
VS_OUTPUT VShade(VS_INPUT i)
{
	VS_OUTPUT   o;
	
	// perform transformation
	float3 P = mul(float4(i.Pos, 1), (float4x3)TransformMatrix);
	o.Pos  = mul(float4(P, 1), ProjectionMatrix);
	i.Normal = normalize(i.Normal);
	
	// perform lighting
    float3 L = -LightVector;                                // diffuse direction
    float3 Y = -DirFromSky;                                 // hemisphere up axis
    //DEL float3 N = normalize(mul(i.Normal, (float3x3)TransformMatrix));   // normal (view space)
	float3 N = i.Normal;
    float3 R = normalize(2 * dot(N, L) * N - L);            // reflection vector (view space)
    float3 V = -normalize(P);                               // view direction (view space)

    float4 Amb  = MaterialAmbient * I_a;
    float4 Hemi = MaterialAmbient * lerp(I_b, I_c, (dot(N, Y) + 1) / 2);
    float  temp = 1 - max(0, dot(N, L));
    float4 Diff = MaterialDiffuse * LightColor * (1 - temp * temp);
    float4 Spec = MaterialSpecular * MaterialSpecular * pow(max(0, dot(R, V)), power);

    o.Diffuse = Diff + Hemi + Amb;            // diffuse + ambient/hemisphere
    o.Diffuse.a = alpha;
    o.Specular = Spec;                        // specular

	
	// copy texture coord
    o.Tex0  = i.Tex0;
	
	return o;
}


//////////////////////////////////////
// Techniques specs follow
//////////////////////////////////////
technique t0
{
    pass p0
    {
        VertexShader = compile vs_2_0 VShade();
    }
}