float4x4 View : register(c4);
float4x4 Proj : register(c8);

// transformations
float4x4 WorldArray[20];

//#include "dcLighting.HLSL"

float MaterialAmbient;
float4 MaterialDiffuse;
float4 MaterialSpecular;

float MaterialSpecularSize;

struct VS_OUTPUT{
    float4 Pos				: POSITION;
    float4 Diff				: COLOR0;
    float4 Spec				: COLOR1;
    float2 Tex				: TEXCOORD0;
    float2 ShininessTex		: TEXCOORD1;
};

struct VS_INPUT{
    float3 Pos              : POSITION;
    float BlendIndices      : BLENDINDICES;

    float3 Normal           : NORMAL;
    float2 Tex              : TEXCOORD0;
};

VS_OUTPUT main(const VS_INPUT Input){
	float3 Pos = mul(float4(Input.Pos, 1), WorldArray[Input.BlendIndices]);
    float3 Normal = mul(Input.Normal, (float3x3)WorldArray[Input.BlendIndices]);
	Normal = normalize(Normal);

	Pos = mul(float4(Pos, 1), (float4x3)View);
	float3 ViewSpaceNormal = mul(Normal, (float3x3)View);
	
	float4 Diffuse = float4(MaterialDiffuse.xyz * max(MaterialAmbient, -ViewSpaceNormal.z), MaterialDiffuse.w);
	
	float3 LightDir = float3(0, 0, -1);	
	
	float3 V = ViewSpaceNormal * 2 * ViewSpaceNormal.z; 
	V += LightDir;
	normalize(V);

	float3 Specular;
	float Spec = -dot(V, LightDir);
	if (Spec > 0.5f){
		Spec = pow(Spec, MaterialSpecularSize);
		Specular = Spec * MaterialSpecular;
	}
	else Specular = 0;
	
	Diffuse = clamp(Diffuse, 0, 1);
	Diffuse.xyz += Specular;
	Diffuse = clamp(Diffuse, 0, 1);
	//Diffuse = 0;
	//Specular *= 10;

	//float4 Diffuse = ComputeDiffuseSimple(Pos, Normal);
	//float3 Specular = ComputeSpecularOverride(MaterialSpecular, MatShininess, Pos, Normal);

	VS_OUTPUT Out;
    Out.Pos  = mul(float4(Pos, 1), Proj);
    Out.Diff = Diffuse;
    Out.Spec = float4(Specular, 1);
    Out.Tex  = Input.Tex;
    Out.ShininessTex  = Input.Tex;
    return Out;
}
