//Fragment shader for terrain texturing - file "texture_terrain.frag"
 //
 
#version 150 core

#define SAMPLES_COUNT 4
 
uniform sampler2D in_texture;
uniform sampler2DShadow in_shadowmap;
uniform sampler2D in_texture2;	

uniform vec3 light_position;

in vec4 out_lightspace_position;
in vec3 out_normal;
in vec3 out_eye;
in vec2 fragment_texcoord;
out vec4 out_color;

 void main(void)
{
	float diffuse_intensity;
	float specular_intensity;
	float shadow;
	
	shadow = textureProj(in_shadowmap, out_lightspace_position);
 
	vec3 light = normalize(light_position - out_eye);
	vec3 normal = normalize(out_normal);

	diffuse_intensity = clamp(max(dot(normal, light), 0.0), 0.0, 1.0);

	if(shadow > 0.01)
	{
		vec3 reflection = normalize(reflect(-light, normal));
		specular_intensity = pow(clamp(max(dot(reflection, out_eye), 0.0), 0.0, 1.0), 30.0);
	}
	else
	{
		specular_intensity = 0.0;
	}
 
	//vec4 texel = mix(in_texture, normal_map, 0.25);
	vec4 texel_color = texture(in_texture, fragment_texcoord);
	vec4 texel_color2 = texture(in_texture2, fragment_texcoord*10.0);
	vec4 texel = mix(texel_color, texel_color2, 0.25);

	out_color = texel * 0.05 +
				texel * diffuse_intensity * clamp(shadow, 0.1, 1.0) +
				texel * specular_intensity;
 
}//end of void main(void)