#version 330 core

// matrixes
uniform mat4 MVP;			// camera transform modelview
uniform mat4 ModelMatrix;	// local model transform (is needed currently to render vehicles)
uniform mat4 ProjMatrix;	// current projection matrix

uniform sampler2D Texture0;
uniform sampler2D Texture1;
uniform sampler2D Texture2;
uniform sampler2D Texture3; // depth

in vec4 compat_TexCoord;
out vec4 FragColor;

void main() {
	
    // Retrieve existing data from textures and combine the pixels.
    vec4 fragment = texture(Texture0, compat_TexCoord.st).rgba;
    
    vec4 light = texture(Texture1, compat_TexCoord.st).rgba;
    
    float minimum_lighting = 0.7;
    float min_shadow = 0.4;
    
    // test: day mode
    //minimum_lighting = 1.0;
    //min_shadow = 0.0;
    
    light.r = clamp((light.r + minimum_lighting), minimum_lighting, 2.0);
    light.g = clamp((light.g + minimum_lighting), minimum_lighting, 2.0);
    light.b = clamp((light.b + minimum_lighting), minimum_lighting, 2.0);
    light.a = clamp((light.a + minimum_lighting), minimum_lighting, 2.0);
    
    // if we want no lights:
    // light = vec4(1.0, 1.0, 1.0, 1.0);
    
    float Shadow = 1.0 - ( ((texture(Texture2, compat_TexCoord.st).r) ) * min_shadow ); // we'+re taking RED channel from the shadow, we have 2 more availible :) leave alpha alone.
    
    //Shadow  = clamp(Shadow * (light.a + 1.0), 0.0, 1.0);
    
    // vec4 shadowcolor = vec4(0, 0, 0, Shadow);
    
    //vec4 globalilumination = vec4(1.0, 1.0, 1.0, 0.5);
    
   	FragColor = vec4(fragment.r * Shadow, fragment.g * Shadow, fragment.b * Shadow, fragment.a) * light;
    
    // FragColor = light;
    
}