//#version 120
#pragma optionNV unroll all
#define MAX_LIGHTS 8
#define MAX_BONES 64

uniform vec3 cameyevector;

varying vec3 alVec[MAX_LIGHTS];
varying vec3 avVec[MAX_LIGHTS];
varying vec3 aeyeVec[MAX_LIGHTS];
//varying vec2 texCoord;
//uniform float lightsEnabled[MAX_LIGHTS];
uniform float lightsinvRadius[MAX_LIGHTS];
//uniform float lightMapSelect[MAX_LIGHTS];
attribute vec3 vTangent;

in vec3 vertexPosition;
in vec3 vertexNormal;
in vec2 textureCoord;
in vec3 vertexTangent;
in float vertexTangentHandedness;
in vec4 vertexBones;
in vec4 vertexWeights;

uniform int hasAnim[1];
uniform mat4 bonePos[MAX_BONES];
uniform mat4 boneRPos[MAX_BONES];
uniform mat4 boneRot[MAX_BONES];
uniform mat4 boneScl[MAX_BONES];

varying vec3 alightVec[MAX_LIGHTS];
//varying vec3 aeyeVec[MAX_LIGHTS];
varying vec2 texCoord;
uniform float lightsEnabled[MAX_LIGHTS];

varying vec3 vNormal;
varying vec3 vPos;
//uniform float lightsinvRadius[MAX_LIGHTS];
//uniform float lightMapSelect[MAX_LIGHTS];
//attribute vec3 vTangent;
vec3 tmpVec;

void main(void)
{
vec3 lightVec;
vec3 eyeVec;
vec3 tmpVec;

vec4 vP;
vec4 vN;
vec4 vT;
vec4 vPa[4];
vec4 vNa[4];
vec4 vTa[4];
	//gl_Position = ftransform();
	//gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	//texCoord = gl_MultiTexCoord0.xy;
	vP=vec4(vertexPosition.xyz, 1.0);
	vN=vec4(vertexNormal.xyz, 1.0);
	vT=vec4(vertexTangent.xyz, 1.0);
    //vertexBones.x+=1;vertexBones.y+=1;vertexBones.z+=1;vertexBones.w+=1;

	if (hasAnim[0]>0){
        if (vertexBones.x>=0){

            vPa[0]=vP;
            vPa[0].x-=bonePos[int(vertexBones.x)][0][3];
            vPa[0].y-=bonePos[int(vertexBones.x)][1][3];
            vPa[0].z-=bonePos[int(vertexBones.x)][2][3];
            vPa[0]=boneRot[int(vertexBones.x)]*vPa[0];
            vPa[0].x+=bonePos[int(vertexBones.x)][0][3];
            vPa[0].y+=bonePos[int(vertexBones.x)][1][3];
            vPa[0].z+=bonePos[int(vertexBones.x)][2][3];
            vPa[0].x+=boneRPos[int(vertexBones.x)][0][3];vPa[0].y+=boneRPos[int(vertexBones.x)][1][3];vPa[0].z+=boneRPos[int(vertexBones.x)][2][3];

            vNa[0]=vN;
            vNa[0]=boneRot[int(vertexBones.x)]*vNa[0];

            vTa[0]=vT;
            vTa[0]=boneRot[int(vertexBones.x)]*vTa[0];


        }
        if (vertexBones.y>=0){
            vPa[1]=vP;
            vPa[1].x-=bonePos[int(vertexBones.y)][0][3];
            vPa[1].y-=bonePos[int(vertexBones.y)][1][3];
            vPa[1].z-=bonePos[int(vertexBones.y)][2][3];
            vPa[1]=boneRot[int(vertexBones.y)]*vPa[1];
            vPa[1].x+=bonePos[int(vertexBones.y)][0][3];
            vPa[1].y+=bonePos[int(vertexBones.y)][1][3];
            vPa[1].z+=bonePos[int(vertexBones.y)][2][3];
            vPa[1].x+=boneRPos[int(vertexBones.y)][0][3]; vPa[1].y+=boneRPos[int(vertexBones.y)][1][3]; vPa[1].z+=boneRPos[int(vertexBones.y)][2][3];

            vNa[1]=vN;
            vNa[1]=boneRot[int(vertexBones.y)]*vNa[1];

            vTa[1]=vT;
            vTa[1]=boneRot[int(vertexBones.y)]*vTa[1];

        }
        if (vertexBones.z>=0){
            vPa[2]=vP;
            vPa[2].x-=bonePos[int(vertexBones.z)][0][3];
            vPa[2].y-=bonePos[int(vertexBones.z)][1][3];
            vPa[2].z-=bonePos[int(vertexBones.z)][2][3];
            vPa[2]=boneRot[int(vertexBones.z)]*vPa[2];
            vPa[2].x+=bonePos[int(vertexBones.z)][0][3];
            vPa[2].y+=bonePos[int(vertexBones.z)][1][3];
            vPa[2].z+=bonePos[int(vertexBones.z)][2][3];
            vPa[2].x+=boneRPos[int(vertexBones.z)][0][3];vPa[2].y+=boneRPos[int(vertexBones.z)][1][3];vPa[2].z+=boneRPos[int(vertexBones.z)][2][3];

            vNa[2]=vN;
            vNa[2]=boneRot[int(vertexBones.z)]*vNa[2];

            vTa[2]=vT;
            vTa[2]=boneRot[int(vertexBones.z)]*vTa[2];
        }
        if (vertexBones.w>=0){
            vPa[3]=vP;
            vPa[3].x-=bonePos[int(vertexBones.w)][0][3];
            vPa[3].y-=bonePos[int(vertexBones.w)][1][3];
            vPa[3].z-=bonePos[int(vertexBones.w)][2][3];
            vPa[3]=boneRot[int(vertexBones.w)]*vPa[3];
            vPa[3].x+=bonePos[int(vertexBones.w)][0][3];
            vPa[3].y+=bonePos[int(vertexBones.w)][1][3];
            vPa[3].z+=bonePos[int(vertexBones.w)][2][3];
            vPa[3].x+=boneRPos[int(vertexBones.w)][0][3];vPa[3].y+=boneRPos[int(vertexBones.w)][1][3]; vPa[3].z+=boneRPos[int(vertexBones.w)][2][3];

            vNa[3]=vN;
            vNa[3]=boneRot[int(vertexBones.w)]*vNa[3];
            vTa[3]=vT;
            vTa[3]=boneRot[int(vertexBones.w)]*vTa[3];
        }

        vP=vPa[0]*vertexWeights.x+vPa[1]*vertexWeights.y+vPa[2]*vertexWeights.z+vPa[3]*vertexWeights.w;
        vN=vNa[0]*vertexWeights.x+vNa[1]*vertexWeights.y+vNa[2]*vertexWeights.z+vNa[3]*vertexWeights.w;
        vN=vec4(vN.xyz, 1.0);
        vT=vTa[0]*vertexWeights.x+vTa[1]*vertexWeights.y+vTa[2]*vertexWeights.z+vTa[3]*vertexWeights.w;
        vT=vec4(vT.xyz, 1.0);
	}

    gl_Position = gl_ModelViewProjectionMatrix * vec4(vP.xyz, 1.0); //there's a risk w wouldn't always be 1, so just to be sure

    texCoord = textureCoord;

	//vec3 n = normalize(gl_NormalMatrix * gl_Normal);
	vec3 n = normalize(gl_NormalMatrix * vec3(vN.x,vN.y,vN.z));
	vec3 t = normalize(gl_NormalMatrix * vec3(vT.x,vT.y,vT.z));
	//vec4 n4 = normalize(gl_ModelViewProjectionMatrix * vec4(vN.x,vN.y,vN.z,1.0));
	//vec4 t4 = normalize(gl_ModelViewProjectionMatrix * vec4(vT.x,vT.y,vT.z,1.0));
	//vec3 n=vec3(n4.x,n4.y,n4.z);
	//vec3 t=vec3(t4.x,t4.y,t4.z);

//bitangent = cross(normal, tangent.xyz)*tangent.w; // from http://www.gamedev.net/topic/320413-calculating-tangent-vectors-for-normal-bump-mappinghelp-again/
	vec3 b = cross(n, t) * vertexTangentHandedness;
    //b = b * vertexTangentHandedness;
	//vec3 b = cross(n, t);// * vertexTangentHandedness;
	//vec3 b = cross(t, n);

	//vec3 vVertex = vec3(gl_ModelViewMatrix * vec4(vertexPosition.xyz, 1.0));
	vec3 vVertex = vec3(gl_ModelViewMatrix * vec4(vP.xyz, 1.0));

for (int i=0; i<MAX_LIGHTS; i++)
   {
	if (lightsEnabled[i]>0.0)
	{
	tmpVec = gl_LightSource[i].position.xyz - vVertex;

	alightVec[i].x = dot(tmpVec, t);
	alightVec[i].y = dot(tmpVec, b);
	alightVec[i].z = dot(tmpVec, n);

	tmpVec = -vVertex;
	aeyeVec[i].x = dot(tmpVec, t);
	aeyeVec[i].y = dot(tmpVec, b);
	aeyeVec[i].z = dot(tmpVec, n);
	}
   }

//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
/*
for (int i=0; i<MAX_LIGHTS; i++)
   {
	if (lightsEnabled[i]>0.0)
	{
	tmpVec = gl_LightSource[i].position.xyz - vVertex;
	lightVec.x = -dot(tmpVec, t);
	lightVec.y = dot(tmpVec, b);
	lightVec.z = dot(tmpVec, n);

	tmpVec = -vVertex;
	eyeVec.x = dot(tmpVec, t);
	eyeVec.y = dot(tmpVec, b);
	eyeVec.z = dot(tmpVec, n);

	float distSqr = dot(lightVec, lightVec);
	//aatt[i] = clamp(1.0 - lightsinvRadius[i] * sqrt(distSqr), 0.0, 1.0);
	alVec[i] = lightVec * inversesqrt(distSqr);

//eyeVec = vec3(gl_ModelViewMatrix * gl_Vertex) * -1.0 + vec3(gl_ProjectionMatrixInverse * vec3(0,0,-1)); // This hack works, but it's ugly...
    eyeVec = dot(-n,vec3(0,0,-1));
    avVec[i] = normalize(eyeVec);
    avVec[i] = eyeVec;
	aeyeVec[i] = eyeVec;
	}
   }
*/
//---------------------------------------------------------------------------------------------------------------------------------------
    eyeVec = dot(-normalize(n), normalize(cameyevector));
    //eyeVec = dot(-normalize(n), vec3(0,0,-1)); //this 0,0,-1 should be replaced with camera eye vector
	aeyeVec[0] = eyeVec;
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------




   //vNormal = vertexTangent;
   vNormal = vertexNormal;
   vPos = vVertex;
}

/*
#define MAX_LIGHTS 8
//varying float aatt[MAX_LIGHTS];
varying vec3 alVec[MAX_LIGHTS];
varying vec3 avVec[MAX_LIGHTS];

varying vec3 eyeVec[MAX_LIGHTS];

varying vec2 texCoord;
uniform float lightsEnabled[MAX_LIGHTS];
uniform float lightsinvRadius[MAX_LIGHTS];
//uniform float lightMapSelect[MAX_LIGHTS];
attribute vec3 vTangent;

void main(void)
{
vec3 lightVec;
vec3 eyeVec;
vec3 tmpVec;
	gl_Position = ftransform();
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	texCoord = gl_MultiTexCoord0.xy;

	vec3 n = normalize(gl_NormalMatrix * gl_Normal);
	vec3 t = normalize(gl_NormalMatrix * vTangent);
	vec3 b = cross(t, n);

	vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);

for (int i=0; i<MAX_LIGHTS; i++)
   {
	if (lightsEnabled[i]>0.0)
	{
	tmpVec = gl_LightSource[i].position.xyz - vVertex;
	lightVec.x = -dot(tmpVec, t);
	lightVec.y = dot(tmpVec, b);
	lightVec.z = dot(tmpVec, n);

	tmpVec = -vVertex;
	eyeVec.x = dot(tmpVec, t);
	eyeVec.y = dot(tmpVec, b);
	eyeVec.z = dot(tmpVec, n);

	float distSqr = dot(lightVec, lightVec);
	//aatt[i] = clamp(1.0 - lightsinvRadius[i] * sqrt(distSqr), 0.0, 1.0);
	alVec[i] = lightVec * inversesqrt(distSqr);

//eyeVec = vec3(gl_ModelViewMatrix * gl_Vertex) * -1.0 + vec3(gl_ProjectionMatrixInverse * vec3(0,0,-1)); // This hack works, but it's ugly...
    eyeVec = dot(-n,vec3(0,0,-1));
    avVec[i] = normalize(eyeVec);
    avVec[i] = eyeVec;
	eyeVec[i] = eyeVec;
	}
   }
}

*/
