uniform sampler2D texture;

varying vec4 position;
varying vec3 normal;

void main()
{	
	vec3 lightVec = gl_LightSource[0].position.xyz - position.xyz;
	lightVec = normalize(lightVec);
	vec3 lightVec2 = gl_LightSource[0].position.xyz + vec3(0.5, 3, 2) - position.xyz;
	lightVec2 = normalize(lightVec2);
	float cosAlpha = max(0.0, dot(normal, lightVec));
   float cosAlpha2 = max(0.0, dot(normal, lightVec));

   vec3 R = normalize(reflect(-lightVec, normal));
   vec3 R2 = normalize(reflect(-lightVec2, normal));
   float cosTheta = max(0.0, dot(R, normalize(-position.xyz)));
   float cosTheta2 = max(0.0, dot(R2, normalize(-position.xyz)));


	gl_FragColor = texture2D(texture, gl_TexCoord[0].st) * vec4(0.7, 0.8, 1.0, 1) *
      (cosAlpha) + vec4(0.3, 0.3, 0.9, 1.0)*pow(cosTheta, 8.0);
   gl_FragColor += cosAlpha2 * vec4(0.3, 0.6, 0.8, 1)/2.0;
   gl_FragColor += pow(cosTheta2, 4.0) * vec4(0.3, 0.6, 0.8, 1)/2.0;

   // some fog according to http://www.ozone3d.net/tutorials/glsl_fog/p04.php

   const float LOG2 = 1.442695;
   float z = gl_FragCoord.z / gl_FragCoord.w;
   float density = 0.1;
   float fogFactor = exp2( -density * density * 
      z * 
      z * 
      LOG2 );
   fogFactor = clamp(fogFactor, 0.0, 1.0);

   gl_FragColor = mix(vec4(0.8, 0.83, 0.96, 1.0), gl_FragColor, fogFactor );

   gl_FragColor.a = 0.0;
}