varying vec4 TexCoord;
varying float fogFactor; 


uniform sampler2D colormap1;
uniform sampler2D colormap2;
uniform sampler2D shadow;
uniform sampler2D color;

uniform sampler2D underwater;
uniform sampler2D tex1;
uniform sampler2D tex2;
uniform sampler2D tex3;
uniform sampler2D tex4;
uniform sampler2D tex4_height;
uniform sampler2D tex4_normal;
uniform sampler2D tex5;
uniform sampler2D tex6;
uniform sampler2D tex7;
uniform sampler2D tex8;


varying vec3 lightDir;
varying vec3 halfVector;

uniform float Strength;
uniform float nighta;



void main(void)
{
  vec2 TexCoord0 = vec2(TexCoord[0],TexCoord[1]);
  vec2 TexCoord1 = vec2(TexCoord[2],TexCoord[3]);

  // vec2 TexCoord0 = vec2(TexCoord[0],TexCoord[1]);
  // vec2 TexCoord1 = vec2(TexCoord[2],TexCoord[3]);
   vec4 a1 = texture2D(colormap1, TexCoord1.st).rgba; // szinterkep
   vec4 a2 = texture2D(colormap2, TexCoord1.st).rgba; // szinterkep
   
   vec3 sd = texture2D(shadow, TexCoord1.st).rgb; // arnyek
   vec4 ct = texture2D(color, TexCoord1.st).rgba; // color
   vec3 d1 = texture2D(tex1, TexCoord0.st).rgb;
   vec3 d2 = texture2D(tex2, TexCoord0.st).rgb;
   vec3 d3 = texture2D(tex3, TexCoord0.st).rgb;
   vec3 d4 = texture2D(tex4, TexCoord0.st).rgb;
   vec3 h4 = texture2D(tex4_height, TexCoord0.st).rgb;
   vec3 n4 = texture2D(tex4_normal, TexCoord0.st).rgb;
   vec3 d5 = texture2D(tex5, TexCoord0.st).rgb;
   vec3 d6 = texture2D(tex6, TexCoord0.st).rgb;
   vec3 d7 = texture2D(tex7, TexCoord0.st).rgb;
   vec3 d8 = texture2D(tex8, TexCoord0.st).rgb;
   vec3 du = texture2D(underwater, TexCoord0.st).rgb;
   

   
   float ar1;
   float ar2;
   float ag1;
   float ag2;
   float ab1;
   float ab2;
   
   
   if (a1.r < 0.5)  {
	ar1 = a1.r*2;
	ar2 = 0.0;
   } else {
    ar1 = 0.0;
	ar2 = a1.r;	
   }  
   
   if (a1.g < 0.5)  {
	ag1 = a1.g*2.0;
	ag2 = 1.0-ag1;
   } else {
    ag2 = a1.g;
	ag1 = 1.0-ag2;
   }
   
   if (a1.b < 0.5)  {
	ab1 = a1.b*2.0;
	ab2 = 0.0;
   } else {
	ab1 = 0.0;
	ab2 = a1.b;
   }
   
   
   
   
   
  
   
   // BumpMapping ======================================================
   /*
    vec3 n = normalize(texture2D(tex4_normal, TexCoord0.st).xyz  - 1.0);
    vec3 l = normalize(lightDir);
    vec3 h = normalize(halfVector);

    float nDotL = max(0.0, dot(n, l));
    float nDotH = max(0.0, dot(n, h));
    float power = (nDotL == 0.0) ? 0.0 : pow(nDotH, gl_FrontMaterial.shininess);
    
    vec4 ambient = gl_FrontLightProduct[0].ambient;
    vec4 diffuse = gl_FrontLightProduct[0].diffuse * nDotL;
    vec4 specular = gl_FrontLightProduct[0].specular * power;
    vec4 color = gl_FrontLightModelProduct.sceneColor + ambient + diffuse + specular;
    
    gl_FragColor = color * texture2D(tex4, TexCoord0.st);*/
   
  
   
   
   
  
   
   
   
   
   //gl_FragColor.rgb = d1;

   //vec3 finalColor = ((d1*a1.r + d2*a1.g + d3*a1.b)*a1.a + d4*(1-a1.a))*(sd*2*(nighta+0.3))*(ct);   

    vec3 finalColor = (  (d1*a1.r + d2*a1.g + d3*a1.b)*a1.a + d4*(1.0-a1.a) + (d5*a2.r + d6*a2.g + d7*a2.b)*a2.a +d8*(1.0-a2.a) +du*0.5  )*(sd*2.0*(nighta+0.3))*(ct) ;   

   //vec3 finalColor = d1*ar1 + d2*ar2 ;//((d1*ar1 + d2*ag1 + d3*ab1 + d5*ar2 + d6*ag2 + d7*ab2)*a1.a + d4*(1-a1.a))*(sd*2*(nighta+0.3))*(ct);   
   
   gl_FragColor.rgb = mix(gl_Fog.color.rgb, finalColor, fogFactor );
   
   //gl_FragColor.rgb = (((d1*a1.r + d2*a1.g + d3*a1.b)*a1.a + d4*(1-a1.a))*(sd*2)) * gl_Fog.color.rgb;
   
   //gl_FragColor.rgb = vec3(1,0,0);
   //gl_FragColor.rgb = 
   //              ((d1 * a1.r * (1.0 - a1.g) * (1.0 - a1.b) + d2 * a1.g * (1.0 - a1.b) + d3 * a1.b + d4 ) /
   //              (a1.r + a1.g + a1.b - a1.r * a1.g - a1.r * a1.b - a1.g * a1.b + a1.r * a1.g * a1.b) *a1.a)*(sd*3);
 
}

