#define LIGHT_SOURCES 2
#define BORDER_SIZE_X 0.0001
#define BORDER_SIZE_Y 0.0001
#define BORDER_MULT_X 1.0
#define BORDER_MULT_Y 1.0
varying vec3 normal, lightDir[LIGHT_SOURCES], eyeVec;
varying float att[LIGHT_SOURCES];
varying vec3 waterNormal;
varying float height;
uniform float time;
uniform sampler2D tex;


void main (void)
{
    //vec4 in_color= gl_FrontMaterial.diffuse;// * texture2D(tex,vec2(gl_TexCoord[0]));
    vec4 in_color= texture2D(tex,vec2(gl_TexCoord[0]));
    in_color.a = gl_FrontMaterial.diffuse.a;
    /*safloat color_add =0.0;
    if(gl_TexCoord[0].x > (1.0-BORDER_SIZE_X))
            color_add +=(gl_TexCoord[0].x-(1.0-BORDER_SIZE_X))*BORDER_MULT_X;
    else if(gl_TexCoord[0].x < BORDER_SIZE_X)
            color_add +=(BORDER_SIZE_X-gl_TexCoord[0].x)*BORDER_MULT_X;
    if(gl_TexCoord[0].y > (1.0-BORDER_SIZE_Y))
            color_add +=(gl_TexCoord[0].y-(1.0-BORDER_SIZE_Y))*BORDER_MULT_Y;
    else if(gl_TexCoord[0].y < BORDER_SIZE_Y)
            color_add +=(BORDER_SIZE_Y-gl_TexCoord[0].y)*BORDER_MULT_Y;
    in_color +=color_add*2.0;*/
    //in_color.a +=0.5;
    vec4 final_color_sum = vec4(0.0,0.0,0.0,0.0);
    for(int i=0;i<LIGHT_SOURCES;i++){
        vec4 final_color = 
        (gl_FrontLightModelProduct.sceneColor * gl_FrontMaterial.ambient) + 
        (gl_LightSource[i].ambient * gl_FrontMaterial.ambient)*att[i];

        vec3 N = normalize(normal);
        vec3 L = normalize(lightDir[i]);
        
        float lambertTerm = dot(N,L);
        
        if(lambertTerm > 0.0){
                final_color +=  gl_LightSource[i].diffuse * in_color * 
                lambertTerm * att[i];      
                vec3 E = normalize(eyeVec);
                vec3 R = reflect(-L, N);
                
                float specular = pow( max(dot(R, E), 0.0), 
                gl_FrontMaterial.shininess );
                
                final_color += gl_LightSource[0].specular * 
                gl_FrontMaterial.specular * specular * att[i];     
        }
        final_color_sum +=final_color;//*0.7+(0.3*final_color*sin(i+sin(time)));
    }
    final_color_sum.a = in_color.a;
    //final_color_sum.a = 0.75;
    final_color_sum.a = final_color_sum.a+0.05*height;
    gl_FragColor = final_color_sum;                 
}