float3 LightAmbient : register(c14);
float3 LightDir[3] : register(c15);
float4 LightDiffuse[3] : register(c18);
float4 LightSpecular : register(c21);

float4 MatEmissive : register(c22);
float4 MatAmbient : register(c23);
float4 MatDiffuse : register(c24);
float4 MatSpecular : register(c25);
float MatShininess : register(c26);

float4 ComputeDiffuse(float3 Pos, float3 Normal){
	float SelfIllum = MatEmissive.x;

	float3 Diffuse = 0;
	for (int i = 0; i < 3; i++){
		Diffuse += LightDiffuse[i] * max(0, dot(Normal, LightDir[i]));
	}
	
	float4 FinalDiffuse;
	FinalDiffuse.rgb = LightAmbient * SelfIllum;
	FinalDiffuse.rgb += Diffuse * (1.0f - SelfIllum);
	
	//FinalDiffuse.rgb = SelfIllum;
	FinalDiffuse.a = MatDiffuse.a;
	return FinalDiffuse;
}	
	
float4 ComputeDiffuseSimple(float3 Pos, float3 Normal){
	float3 FinalAmbient = MatEmissive;

	// Compute lighting
	float4 Diffuse = float4(FinalAmbient, 1);
	for (int i = 0; i < 3; i++){
		Diffuse.rgb += LightDiffuse[i] * max(0, dot(Normal, LightDir[i]));
	}
	return Diffuse;
}

float4 ComputeDiffuseSimpleNoFog(float3 Pos, float3 Normal){
	float3 FinalAmbient = MatEmissive;

	// Compute lighting
	float4 Diffuse = float4(FinalAmbient, 1);
	for (int i = 0; i < 3; i++){
		Diffuse.rgb += LightDiffuse[i] * max(0, dot(Normal, LightDir[i]));
	}
	return Diffuse;
}

float4 ComputeDiffuseEmissiveOverrideSimple(float4 Emissive, float3 Pos, float3 Normal){
	float3 FinalAmbient = Emissive;

	// Compute lighting
	float4 Diffuse = float4(FinalAmbient, 1);
	for (int i = 0; i < 3; i++){
		Diffuse.rgb += LightDiffuse[i] * max(0, dot(Normal, LightDir[i]));
	}
	return Diffuse;
}

float4 ComputeDiffuseEmissiveOverrideSimpleNoFog(float4 Emissve, float3 Pos, float3 Normal){
	float3 FinalAmbient = Emissve;

	// Compute lighting
	float4 Diffuse = float4(FinalAmbient, 1);
	for (int i = 0; i < 3; i++){
		Diffuse.rgb += LightDiffuse[i] * max(0, dot(Normal, LightDir[i]));
	}
	return Diffuse;
}

float4 ComputeDiffuseEmissiveOverride(float4 Emissive, float3 Pos, float3 Normal){
	float3 FinalAmbient = Emissive; // + (MatAmbient * LightAmbient);

	// Compute lighting
	float4 Diffuse = float4(FinalAmbient, MatDiffuse.a);
	for (int i = 0; i < 3; i++){
		Diffuse.rgb += LightDiffuse[i] * MatDiffuse * max(0, dot(Normal, LightDir[i]));
	}
	return Diffuse;
}


float3 ComputeSpecular(float3 Pos, float3 Normal){
	float3 V = -normalize(Pos);

	float3 N = -normalize(mul(Normal, (float3x3)View));
	float3 R = normalize(2 * dot(N, LightDir[0]) * N - LightDir[0]);
	
	float3 Specular = LightSpecular/* * MatSpecular*/ * pow(max(0, dot(R, V)), MatShininess*2);

	return Specular;
}

float3 ComputeSpecularOverride(float4 MatSpecular, float MatShininess, float3 Pos, float3 Normal){
	float3 V = -normalize(Pos);

	float3 N = -normalize(mul(Normal, (float3x3)View));
	float3 R = normalize(2 * dot(N, LightDir[0]) * N - LightDir[0]);
	
	float3 Specular = LightSpecular * MatSpecular * pow(max(0, dot(R, V)), MatShininess);

	return Specular;
}