Texture2D shaderTextures[2];
SamplerState SampleType;

struct SPointLight
{
	float4 ambientColor;
	float4 diffuseColor;
	float4 specularColor;
	float3 position;
	float  intensityDecrease;
};

struct SDirectionalLight
{
	float4 ambientColor;
	float4 diffuseColor;
	float4 specularColor;
	float3 direction;
	float  padding;
};

struct SMaterialInfo
{
    float4 ambientColor;
    float4 diffuseColor;
    float4 specularColor;
    float specularExponent;
	uint flags;
	float2 padding;
};

cbuffer LightBuffer : register(ps, cb0)
{
	SDirectionalLight directionalLights[100];
	SPointLight pointLights[100];
	uint directionalLightsCount;
	uint pointLightsCount;
	float2 lightbuffer_padding;
};


cbuffer MaterialBuffer : register(ps, cb1)
{
	SMaterialInfo material;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
	float4 worldPosition : POSITION1;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
	float3 viewDirection : TEXCOORD1;
	int materialId : MATERIALID;
};

////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 LightFragmentShader(PixelInputType input) : SV_TARGET
{
    float4 textureColor;
    float3 lightDir;
    float lightIntensity;
    float4 ambientColorComponent = float4(.0f,.0f,.0f,1.0f);
	float4 diffuseColorComponent = float4(.0f,.0f,.0f,1.0f);
	float4 specularColorComponent = float4(.0f,.0f,.0f,1.0f);
	float3 reflection;
    float4 specular;
	float4 bumpFragment;
    float3 resultNormal;

    // Sample the pixel color from the texture using the sampler at this texture coordinate location.
    textureColor = shaderTextures[0].Sample(SampleType, input.tex);
	
	resultNormal = input.normal;
	
	if (material.flags & 1)
	{
		// Sample the pixel in the bump map.
		bumpFragment = shaderTextures[1].Sample(SampleType, input.tex);

		// Expand the range of the normal value from (0, +1) to (-1, +1).
		//bumpFragment = (bumpFragment * 2.0f) - 1.0f;

		// Calculate the normal from the data in the bump map.
		//bumpNormal = (bumpMap.x * input.tangent) + (bumpMap.y * input.binormal) + (bumpMap.z * input.normal);
		resultNormal = (resultNormal + bumpFragment)/2;
	}

	// Normalize the resulting bump normal.
	resultNormal = normalize(resultNormal);
				
	 // Calculate the amount of light on this pixel for each DIRECTIONAL LIGHT
	for (int i = 0; i < directionalLightsCount; ++i)
	{
		ambientColorComponent += clamp(directionalLights[i].ambientColor,0.0f,100.0f);
		
	    // Invert the light direction for calculations.
		lightDir = -directionalLights[i].direction;

		// Calculate the amount of light on this pixel.
		lightIntensity = saturate(dot(resultNormal, lightDir));
		if(lightIntensity > 0.0f)
		{
			// Determine the final diffuse color based on the diffuse color and the amount of light intensity.
			diffuseColorComponent += directionalLights[i].diffuseColor * lightIntensity;
				
			// Calculate the reflection vector based on the light intensity, normal vector, and light direction.
			reflection = normalize(2 * lightIntensity * resultNormal - lightDir); 

			// Determine the amount of specular light based on the reflection vector, viewing direction, and specular power.
			specularColorComponent += directionalLights[i].specularColor*pow(saturate(dot(reflection, input.viewDirection)), material.specularExponent);
		}
	}
    // Calculate the amount of light on this pixel for POINT LIGHT
	for (int i = 0; i < pointLightsCount; ++i)
	{
		lightDir = pointLights[i].position - input.worldPosition;
		float deltaIntensity = pointLights[i].intensityDecrease*length(lightDir);
		ambientColorComponent += clamp(pointLights[i].ambientColor - deltaIntensity,0.0f,100.0f);
		lightDir = normalize(lightDir);
		lightIntensity = saturate(dot(resultNormal, lightDir));
		if(lightIntensity > 0.0f)
		{
			// Determine the final diffuse color based on the diffuse color and the amount of light intensity.
			diffuseColorComponent += clamp(pointLights[i].diffuseColor - deltaIntensity,0.0f,100.0f)* lightIntensity;
				
			// Calculate the reflection vector based on the light intensity, normal vector, and light direction.
			reflection = normalize(2 * lightIntensity * resultNormal - lightDir); 

			// Determine the amount of specular light based on the reflection vector, viewing direction, and specular power.
			specularColorComponent += clamp(pointLights[i].specularColor - deltaIntensity,0.0f,100.0f)*pow(saturate(dot(reflection, input.viewDirection)), material.specularExponent);
		}
	}
	
    // Multiply the texture pixel and the final diffuse color to get the final pixel color result.
    diffuseColorComponent = (ambientColorComponent*material.ambientColor + diffuseColorComponent*material.diffuseColor) * textureColor;

	// Add the specular component last to the output color.
	specularColorComponent = saturate(diffuseColorComponent + specularColorComponent * material.specularColor);
	
    return specularColorComponent;
}


