// Version du GLSL
#version 150 core

// Entrées
in vec3 vertex;
in vec3 normale;
in vec2 coordTexture;

// Uniform
uniform sampler2D texture;
uniform vec3 eyePos;
uniform vec3 posLight[10];
uniform int overLight; // Dépassement de la couleur originale du pixel
uniform int nbLight; // Nombre de lumières (maxi 10)

// Sortie
out vec4 out_Color;

// Fonction main
void main()
{
    out_Color = vec4(0.0, 0.0, 0.0, 1.0);
    vec4 pxColor = texture2D(texture, coordTexture);
    float ambientColor = 0.2;
    float loin;
    float cosTheta;

    vec4 colorLight;
    if ( overLight ) colorLight = vec4(1.0, 0.5, 0.0, 1.0);
    else colorLight = vec4(1.0, 1.0, 1.0, 1.0);
    colorLight *= pxColor;

    int i;
    for ( i=0; i<nbLight; i++)
    {
        /// Cosinus
        cosTheta = clamp( dot( normale, posLight[i]-vertex ), 0.0, 1.0 );
        //float cosAlpha = clamp( dot( reflect( normalize(vertex-posLight[i]), normale), normalize(eyePos-vertex) ), 0.0, 1.0 );
        //float viewDistance = 5.0f;

        /// Distance
        loin = distance( vertex, posLight[i] );
        //loin = (0.5-(0.5/(viewDistance*viewDistance*viewDistance))*loin*loin*loin);

        /// Couleurs

        /// Calcule si nécessaire
        if ( loin < 30 )
        {
            /// Calcul lumière diffuse
            float power = cosTheta * 100 / (loin*loin);
            if ( overLight == 0 ) power = clamp( power, 0.0, 1.0 );
            out_Color += colorLight * power * 0.4;

            /// Calcul lumière spéculaire
            /*power = pow(cosAlpha,5) * 1000 / (loin*loin);
            if ( overLight == 0 ) power = clamp( power, 0.0, 1.0 );
            out_Color +=  colorLight * power * 0.4;*/ // Lumière spéculaire
        }
    }

    /// Lumière ambiente
    out_Color += pxColor * ambientColor;

    /// Correction ransparence
    out_Color.w = pxColor.w;
}
