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

#vertexshader

const int NUMBONES = 40;
uniform mat4 Bones[NUMBONES];
uniform vec4 Colors[NUMBONES];

//attribute vec4 Vertex;



void main()
{
	vec4 Pos0 = vec4(gl_Vertex.xyz, 1.0);
	int index = int(gl_Vertex.w);

	mat4 Bone = Bones[index];
	vec4 Pos = Bone * Pos0;

	gl_Position = gl_ModelViewProjectionMatrix * Pos;
	Bone[3][0] = 0.0;
	Bone[3][1] = 0.0;
	Bone[3][2] = 0.0;
	NormalOut = gl_NormalMatrix * normalize((Bone * vec4(gl_Normal,1)).xyz);
	
	ColorOut = Colors[index];
	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_TexCoord[1] = gl_MultiTexCoord1;
	
	ModelViewPos = (gl_ModelViewMatrix * Pos).xyz;
	VertexToLight = (gl_ModelViewMatrix * vec4(100.0,100.0,100.0,1.0)).xyz - ModelViewPos.xyz;
	gl_TexCoord[0] = gl_MultiTexCoord0;
}

#fragmentshader

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 * 70.0);
	Ispec = clamp(Ispec, 0.0, 1.0);
	
	vec3 Col = ColorOut.xyz;
	vec3 c = clamp(dot(LightDir, NormalOut),0.0,1.0) * 0.3 + Col.xyz * 0.7;
	gl_FragColor = vec4(c,1);
	gl_FragColor.xyz += vec3(Ispec);
}
