////////////////////////////////////////////////////////////////////////////////
// Filename: light.ps
////////////////////////////////////////////////////////////////////////////////

/////////////
// DEFINES //
/////////////
#define NUM_LIGHTS 4

/////////////
// GLOBALS //
/////////////
Texture2D shaderTexture : register(t0);
SamplerState SampleType : register(s0);
cbuffer LightBuffer
{
    float4 ambientColor;
    float3 lightDirection;
    float padding;
    float specularPower;
    float4 specularColor;
};

cbuffer LightColorBuffer
{
    float4 diffuseColor[NUM_LIGHTS];
};


//////////////
// TYPEDEFS //
//////////////
struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
    float3 lightPos[NUM_LIGHTS] : TEXCOORD1;
};


////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 LightPixelShader(PixelInputType input) : SV_TARGET
{
    float4 textureColor;
    float3 lightDir;
    float4 color;
    float3 reflection;
    float4 specular;
    float lightIntensity[NUM_LIGHTS];
    float4 colorArray[NUM_LIGHTS];
    float4 colorSum;
    int i;

    // Sample the pixel color from the texture using the sampler at this texture coordinate location.
    textureColor = shaderTexture.Sample(SampleType, input.tex);

    for(i=0; i<NUM_LIGHTS; i++)
    {
        // Calculate the different amounts of light on this pixel based on the positions of the lights.
        lightIntensity[i] = saturate(dot(input.normal, input.lightPos[i]));

        // Determine the diffuse color amount of each of the four lights.
        colorArray[i] = diffuseColor[i] * lightIntensity[i];
    } 

    // Initialize the sum of colors.
    colorSum = float4(0.0f, 0.0f, 0.0f, 1.0f);

    // Add all of the light colors up.
    for(i=0; i<NUM_LIGHTS; i++)
    {
        colorSum.r += colorArray[i].r;
        colorSum.g += colorArray[i].g;
        colorSum.b += colorArray[i].b;
    }

    // Multiply the texture pixel by the combination of all four light colors to get the final result.
    color = saturate(colorSum) * textureColor;

    return color;
}