#version 330


uniform mat4 proj, view, model; 

uniform vec2 windowSize;
// w of positional light is distance of emission (linear)
uniform vec4 Lp;
uniform vec3 directional;
uniform int objectId;

uniform sampler2D myTexture;
uniform int hasTexture;
uniform vec4 diffuse_color;
uniform vec4 specular_color;
uniform vec4 ambient_color;
uniform vec4 emission_color;
uniform float shininess;

uniform sampler2D noise;
uniform vec3 noiseMov;

in vec2 gsoTexCoord;
in vec3 gsoNormal;
in vec4 gsoModPosition;
flat in float isGrass;
in float gHeight;
in vec2 grassUV;
out vec4 fragColor;

vec2 rotate(in vec2 vector, float angle){
	return vec2(vector.x * cos(angle) - vector.y * sin(angle),
				vector.x * sin(angle) - vector.y * cos(angle));
}

float calculate_damage(){
	float val = texture(noise, rotate((gl_FragCoord.xy/windowSize) + (vec2(noiseMov.x,noiseMov.y)), clamp(length(noiseMov), 0.0, 1.0))).r;
	if(val > (noiseMov.z / 100.0)){
		return 1.0;
	} else {
		return val;
	}
}

float calculate_vignette(){
	// We consider that y < x == true
	vec2 ratioedSize = vec2(windowSize.y * 4/3, windowSize.y);
	float xMargin = (windowSize.x - ratioedSize.x) / 2;
	
	// On rajoute les barres noires pour le format 4:3
	if(gl_FragCoord.x < xMargin || gl_FragCoord.x > (windowSize.x - xMargin))
		return 0.0;

	return clamp(0.9-(length(gl_FragCoord.xy - ((ratioedSize/2) + vec2(xMargin,0.0))) / length(ratioedSize/2)), 0.0, 1.0);
}

// Source - https://stackoverflow.com/q/12964279
// Posted by Grumdrig, modified by community. See post 'Timeline' for change history
// Retrieved 2026-05-07, License - CC BY-SA 3.0
float rand(in vec2 co){
  return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

float sigmoid(in float x){
	return 1/(1+exp(-6.5*(x-0.5)));
}

void main(void) {
	if(isGrass != 0.0){
		vec2 randCoord = floor(grassUV * 15)/15;
		if(rand(randCoord) <= gHeight/20.0)
			discard;
	}
	vec3 Ld = normalize(directional);
	
    float diffuse = clamp(dot(-normalize(gsoNormal), Ld), 0.0, 1.0);
    vec4 specularReflection = specular_color * pow(max(0.0, dot(normalize(reflect(-Ld, gsoNormal)), normalize(vec3(-gsoModPosition)))), shininess);
    vec4 diffuseReflection = ambient_color * 0.5 + diffuse_color * diffuse;
    vec4 col = diffuseReflection;// + specularReflection;

    // Ajout de la light positionelle
    vec3 distToLp = gsoModPosition.xyz - (view * vec4(Lp.xyz,1.0)).xyz;
    Ld = normalize(distToLp);
    diffuse = clamp(dot(-normalize(gsoNormal), Ld), 0.0, 1.0);
    specularReflection = specular_color * pow(max(0.0, dot(normalize(reflect(-Ld, gsoNormal)), normalize(vec3(-gsoModPosition)))), shininess);
    diffuseReflection = ambient_color + diffuse_color * diffuse;
    col += (diffuseReflection + specularReflection) * clamp(Lp.w-(length(distToLp)), 0.0,1.0);
    
    
    if(hasTexture != 0)
		col *= texture(myTexture, gsoTexCoord);
	if(isGrass != 0.0){
		col *= clamp((gHeight+5) / 25, 0.0, 1.0);
	}
	if(objectId == 1){
		col += vec4(0.05);
	}
	float gray =  0.2126*col.x + 0.7152*col.y + 0.0722*col.z;
	gray = sigmoid(gray);
	vec4 gray_col = vec4(gray,gray,gray, col.a);

	fragColor = vec4(vec3(gray_col * calculate_vignette() * calculate_damage()), 1.0);
	//fragColor = col;
}
