#version 330 core

// Inputs coming from the vertex shader
in struct fragment_data
{
    vec3 position; // position in the world space
    vec3 normal;   // normal in the world space
    vec3 color;    // current color on the fragment
    vec2 uv;       // current uv-texture on the fragment

} fragment;

// Output of the fragment shader - output color
layout(location=0) out vec4 FragColor;

// View matrix
uniform mat4 view;

struct material_structure
{
  vec3 color;  // Uniform color of the object
};
uniform material_structure material;

// Ambiant uniform controled from the GUI
uniform float ambiant;
uniform vec3 light_color;
uniform vec3 light_position;

// The coefficient diffuse must be sent as uniform in the C++ program
uniform float diffuse;

void main()
{
  vec3 current_color;
  
  vec3 L = normalize(light_position - fragment.position);
  vec3 N = normalize(fragment.normal);

  float diffuse_value = max(dot(N, L), 0.0);
  float diffuse_magnitude = diffuse * diffuse_value;
  current_color = (ambiant + diffuse_magnitude) * material.color * light_color;
  FragColor = vec4(current_color, 1.0);
}