/*
 * fragment shader for phong shading
 *
 * does simple phong lighting
 * TODO: set lighting parameters with uniforms or uniform buffer objects
 *
 * written by:
 * christian moellinger <ch.moellinger@gmail.com>
 * 11/2010
 */

#version 330 core

out vec4 out_Color;
in vec3 my_Normal;
in vec3 my_Texcoord;
in vec3 my_LightDir;
in vec3 my_EyeDir;
in mat3 NormalMatrix2;
in vec4 my_WorldPosition;

uniform sampler2D color_texture;

/*
layout(shared) uniform lightingParam
{
    vec4 LightPosition;
    vec3 LightColorDiffuse;
    vec3 LightColorSpecular;
    vec3 MaterialDiffuse;
    vec3 MaterialSpecular;
    float MaterialShininess;
};*/

void main(void)
{
    vec4 texture_value = texture(color_texture, my_Texcoord.st);

    vec4 light_value = vec4(0.1, 0.1, 0.1, 1.0);

    //these things should set per uniforms
    vec3 lightDiffuse = vec3(1.0);
    vec3 MaterialDiffuse = vec3(0.8);
    float MaterialShininess = 100.0;
    vec3 MaterialSpecular = vec3(1.0);
    vec3 LightSpecular = vec3(1.0);
    //end

    vec3 N = normalize(NormalMatrix2 * normalize(my_Normal));

    vec3 L = normalize(my_LightDir);

    float lambertTerm = dot(N,L);

    if(lambertTerm > 0.0)
    {
	    light_value += vec4(lightDiffuse * MaterialDiffuse * lambertTerm, 1.0);

	    vec3 E = normalize(my_EyeDir);
	    vec3 R = reflect(-L, N);

	    float specular = pow(max(dot(R, E), 0.0), MaterialShininess);

	    light_value += vec4(LightSpecular * MaterialSpecular * specular, 1.0);
    }

  //write distance to y in alpha value, this is used in water shader to determinate if the shifting of the textures must be clamped
  float y_under_water = -my_WorldPosition.y;
  out_Color = vec4((light_value * texture_value).xyz, y_under_water);
}
