//varying vec3 normal;
//varying vec4 color;

varying vec3 NormalOut;
varying vec3 VertexToLight;
varying vec4 ColorOut;
varying vec3 PosOut;
varying vec3 ModelViewPos;

#vertexshader



void main()
{
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	NormalOut = gl_NormalMatrix * gl_Normal;
	
	ColorOut = gl_Color;
	PosOut = gl_Position.xyz;
	
	ModelViewPos = (gl_ModelViewMatrix * gl_Vertex).xyz;
	VertexToLight = (gl_ModelViewMatrix * vec4(100.0,100.0,100.0,1.0)).xyz - ModelViewPos;
	gl_TexCoord[0] = gl_MultiTexCoord0;
}

#fragmentshader

uniform sampler2D texture;

void main()
{
	vec3 LightPos = vec3(100.0,100.0,100.0);
	vec3 LightDir = VertexToLight;
	vec3 E = normalize(-ModelViewPos); // we are in Eye Coordinates, so EyePos is (0,0,0)  
	vec3 R = normalize(-reflect(LightDir,NormalOut));
   
	float Ispec = 0.2 * pow(max(dot(R,E),0.0),0.3 * 100.0);
	Ispec = clamp(Ispec, 0.0, 1.0);
	
	vec2 TexCoord = gl_TexCoord[0].xy;
	vec3 Col = texture2D(texture, TexCoord).xyz;//lerp(ColorOut.xyz,texture2D(texture, TexCoord).xyz, 1.0);
	vec3 c = clamp(dot(LightDir, NormalOut),0.0,1.0) * Col.xyz;
	gl_FragColor.xyz = c * (1.0 - pow(PosOut.z,0.5) * 0.06);//,1.0);//vec4(1.0 - pow(PosOut.z,0.5) * 0.07);//
	gl_FragColor.xyz += vec3(Ispec);
	gl_FragColor.w = 1.0;
}
