#version 330

uniform float time;
uniform sampler2D ourTexture;
uniform int has_texture;
uniform float texture_level;
uniform int lighting;

out vec4 FragColor;
flat in int vID;
in vec2 texCoord;
in vec3 position;
in vec3 normal;


float rand(float n)
{
	return fract(sin(n) * 43758.5453123);
}

void main()
{
	vec3 color = vec3(rand(float(vID + int(time))), rand(float(vID + int(time))), rand(float(vID + int(time))));
	if (has_texture == 1) {
    	color = color * (1 - texture_level) + texture_level * texture(ourTexture, vec2(texCoord.x, 1.- texCoord.y)).rgb;
	
	}
	if (lighting == 1) {
		vec3 n = normalize(vec3(10. * cos(time), 10., 10. * sin(time)) - position);
		float ambient = 0.2;
		float phong = max(0., dot(n, normalize(normal)));
		float specular = pow(max(dot(reflect(n, normalize(normal)), normalize(vec3(0, 0, -2) - position)), 0.), 10.);
		color = color * (ambient + (1. - ambient) * phong) + vec3(1.) * specular;
	}
	FragColor = vec4(color, 1.);
}
