varying vec3 normal;
varying vec4 OutColor;
varying vec3 pos;

#vertexshader

const int NUMBONES = 126;
uniform vec4 Bones[NUMBONES];

attribute vec4 Weights;
attribute vec4 Joints;
attribute vec4 Normal;

void main()
{
	vec4 Pos0 = vec4(gl_Vertex.xyz, 1.0);
	int iJoint = int(Joints[0]);
	vec4 Trans0 = Bones[iJoint * 3] * Weights[0];
	vec4 Trans1 = Bones[iJoint * 3 + 1] * Weights[0];
	vec4 Trans2 = Bones[iJoint * 3 + 2] * Weights[0];
	for (int i = 1; i < 4; i++)
	{
		iJoint = int(Joints[i]);
		Trans0 += Bones[iJoint * 3] * Weights[i];
		Trans1 += Bones[iJoint * 3 + 1] * Weights[i];
		Trans2 += Bones[iJoint * 3 + 2] * Weights[i];
	}
	vec4 Pos;
	Pos.x = dot(Trans0, Pos0);
	Pos.y = dot(Trans1, Pos0);
	Pos.z = dot(Trans2, Pos0);
	Pos.w = 1.0;
	normal.x = dot(Trans0.xyz,Normal.xyz);
	normal.y = dot(Trans1.xyz,Normal.xyz);
	normal.z = dot(Trans2.xyz,Normal.xyz);
	//normal = normalize(normal.xyz);
	pos = (gl_ModelViewMatrix * Pos).xyz;
	gl_Position = gl_ModelViewProjectionMatrix * Pos;
	OutColor = gl_Color;
}

#fragmentshader

//uniform samplerCube Texture0;
uniform sampler2D Texture0;

void main()
{
	vec3 LightPos = vec3(0.0,0.0,10.0);
	vec3 LightDir = normalize(LightPos - pos);
	vec3 E = normalize(-pos); // we are in Eye Coordinates, so EyePos is (0,0,0)  
	vec3 R = normalize(-reflect(LightDir,normal));
	
	vec4 Ispec = vec4(0.5 * pow(max(dot(R,E),0.0),0.3 * 25.0));
	Ispec = clamp(Ispec, 0.0, 1.0);
	
	//vec3 color = color.xyz;//vec3(0.9, 0.9, 0.9);
	//vec4 color = textureCube(Texture0, normal); //vec3(0.15, 0.25, 0.4);
	vec4 color = texture2D(Texture0, normal.xy+vec2(0.5, 0.5));// * OutColor.x; //vec3(0.15, 0.25, 0.4);
	float f = (dot(LightDir, normal)+1.0)/2.0;
	//gl_FragColor = 0.5 * (vec4(1.0,1.0,1.0,1.0) + vec4(normal.x,normal.y,normal.z,1.0));//vec4(color*(f*0.7+0.3),1);
	gl_FragColor = f * OutColor + vec4(color.xyz*(f*0.7+0.3),1.0)*1.25;
	gl_FragColor.xyz += Ispec.xyz;

//	vec3 lightdir = normalize(vec3(1.0,1.0,1.0));
//	gl_FragColor = vec4((normal.xyz+vec3(1.0,1.0,1.0))*vec3(0.5,0.5,0.5),1.0);
}
