varying vec3 vNormal;
varying vec3 vVertex;
uniform sampler2D unit0;

#define silhouetteThreshold 0.2
#define shininess 20.0

void main (void)
{
  // Texture color
	vec4 tex_color = texture2D(unit0, gl_TexCoord[0].st);

  // Silhouette Color:
  vec4 silhouetteColor = vec4(0.0, 0.0, 0.0, 1.0);

  // Lighting
  vec3 eyePos = normalize(-vVertex); 
  vec3 lightPos = gl_LightSource[0].position.xyz;

  vec3 Normal = normalize(vNormal);
  vec3 EyeVert = normalize(eyePos - vVertex);
  vec3 LightVert = normalize(lightPos - vVertex);
  vec3 EyeLight = normalize(LightVert + EyeVert);

  // Simple Silhouette
  float sil = max(dot(Normal, EyeVert), 0.0);
  if (sil < silhouetteThreshold) 
    gl_FragColor = silhouetteColor;
  else 
  {
    float multiplier = 0.4;
    
    // Specular part
    float spec = pow(max(dot(Normal, EyeLight), 0.0), shininess);
    multiplier += 0.1 * smoothstep(0.29, 0.3, spec);

    // Diffuse part
    float diffuse = max(dot(Normal, LightVert), 0.0);
    multiplier += 0.2 * smoothstep(0.49, 0.5, diffuse);
      
    gl_FragColor = multiplier * tex_color;
  }
  
  gl_FragColor.a = tex_color.a;
}
