varying vec4 pos3D;
varying vec3 light, normal, eye, v;

uniform int mode;

void main()
{
	
	vec3 halfV = normalize(light - v);
    
    vec3 toFresnel = normalize(eye);
    
    vec3 n = normalize(normal.xyz);
    
	float nDotL = max(dot(n, halfV), 0.0);
    
    float fresnel = pow(max(dot(n, toFresnel), 0.0), 0.5);
    
    fresnel = clamp(pow(1.0 - fresnel, 1.0), 0.0, 1.0) * 1.5;


    /*vec3 i = normalize(v - eye);
    float bias = 0.1;
    float scale = 0.1;
    float power = 10.1;
    fresnel = bias + scale * pow(1.0 + dot(i, n), power);*/ 
    
    //fresnel = (fresnel + _depth.a)* 0.5;
    //piFresnel = fresnel;
   
    vec4 color;
   
    //color.rgb = vec3(0.2, 0.0, 0.0);
    color.rgb = vec3(0.1, 0.65, 0.65);

		//diffuse
		color.rgb = mix(vec3(0.0, 0.02, 0.1), color.rgb, max(0.1, nDotL));
    color.rgb = mix(vec3(0.0, 0.02, 0.15), color.rgb, max(0.1, nDotL));
		//specular
		color.rgb = mix(color.rgb, vec3(1.0, 1.0, 1.0), clamp(pow(nDotL, 160.0) * 1.7, 0.0, 1.0));

    //color.rgb += pow(1.0 - gm, 5.0);

    color.rgb += vec3(fresnel * 1.0);
    
    if (mode == 1)
    {
      color.rgb = vec3(1.0);
      color.b = fresnel;
    }

  gl_FragData[0] = /*gl_Color **/ vec4(color.rgb, 1.0);
  
  //gl_FragData[0] = /*gl_Color **/ vec4(n.rgb * 0.5 + 0.5, 1.0);
  
  //gl_FragData[0] = vec4(vec3(dot(n, normalize(light.xyz))), 1.0);
  
  //gl_FragData[0] = vec4(vec3(pow(gl_FragCoord.z * 1.1, 10.2)), 1.0);
  
  //gl_FragDepth = gl_FragCoord.z;
}
