#version 330
in vec4 mp;
in vec3 normal, pos;
in vec2 tc;
out vec4 fragColor;

const float shininess = 200.0;
const int nbL = 4;

uniform vec4 Lp[10], Lc[10];
uniform sampler2D tex;
uniform mat4  model, view;
uniform vec4 couleur;
uniform int hasTex, corps, sol;

float sig(float x) { return 1.0 / (1.0 + exp(-6.0 * x)); }

void main(void) {
  vec4 dcolor = vec4(0.0), scolor = vec4(0.0);
  vec3 V = vec3(0.0, -1.0, 0.0);
  vec3 n = normalize(normal);
  for(int i = 0; i < nbL; i++) {
    vec3 Ld = normalize(mp.xyz - Lp[i].xyz);
    vec3 R = reflect(Ld, n);
    float intensiteDeLumiereDiffuse = clamp(dot(n, -Ld), 0.0, 1.0);
    dcolor += Lc[i] * vec4(intensiteDeLumiereDiffuse * couleur.rgb, couleur.a);
    float intensiteDuSpeculaire = pow(clamp(dot(-V, R), 0.0, 1.0), shininess);
    scolor += Lc[i] * vec4(intensiteDuSpeculaire * vec3(1.0), 0.0);
  }
  if(hasTex != 0) {
    if(corps == 1 && (tc.x > 0.75 && tc.x < 0.9))
      dcolor *= clamp(vec4(1.0, 0.0, 0.0, 0.0) +
		      texture(tex, tc), vec4(0.0), vec4(1.0));
    else if(corps == 2) /* bee */
      dcolor *= texture(tex, pos.xz * vec2(1.0, 2.0));
    else
      dcolor *= vec4(texture(tex, tc).rgb, 1.0);
  }
  fragColor = dcolor + scolor;
  if(sol == 1) {
    const float l = 0.32, cl = 1.0 - l;
    float a = pos.z > -l ? 1.0 : pow(1.0 + (pos.z + l) / cl, 5.0);
    fragColor.a = a;
  }
}
