#version 330 core

uniform sampler2D al_tex;
varying vec4 varying_color;
varying vec2 varying_texcoord;

uniform vec2 window_size;
uniform vec4 camera_trans;

uniform bool lights_on;

in vec4 pos;
uniform int light_num;
uniform vec3 light_pos[100];	//z is hardness
uniform vec4 light_color[100];	//w is intensity
uniform sampler2D shadow;
vec3 light_out;

void main()
{
	
	if(light_num > 0 && lights_on)light_out = vec3(0.0, 0.0, 0.0);
	else light_out = vec3(1.0, 1.0, 1.0);

	vec4 tmp = varying_color * texture2D(al_tex, varying_texcoord);
	

	//vec4 sm = texture2D(shadow, varying_texcoord);
	//if(sm.r != 1.0)light_out = vec3(0.0,0.0,0.0); else{ // - shadowMap

	if(lights_on)for(int i = 0;i < light_num && i < 100; i++){
		float intens = ((window_size.x + window_size.y)/(camera_trans.z + camera_trans.w)) * 
		light_color[i].w / 
		(light_color[i].w / light_pos[i].z + length(vec2(
		(light_pos[i].x - camera_trans.x) / camera_trans.z * window_size.x - gl_FragCoord.x, 
		(light_pos[i].y - camera_trans.y - camera_trans.w) / camera_trans.w * window_size.y + gl_FragCoord.y)));
		
		light_out += light_color[i].xyz * intens;
	}
	
	
	gl_FragColor = tmp * vec4(light_out, 1.0);
}