// VERTEX SHADER :  TEXTURE + ENVIRONMENT + BUMP

varying vec4 vLight0;
varying vec4 vHalfAngleVec0;
varying vec4 vLight1;
varying mat3 matTBN;
varying vec3 vVertex;
varying vec3 vNormal;

void main()
{
	vVertex = gl_Vertex.xyz;
	vNormal = gl_Normal.xyz;

//	matTBN[0]	= normalize(gl_NormalMatrix * gl_MultiTexCoord1.xyz);	// tangent
	matTBN[0]	= normalize(gl_NormalMatrix * gl_Normal.xzy);	// BIG HACK tangent
	matTBN[2]	= normalize(gl_NormalMatrix * gl_Normal);				// normal

	matTBN[1]	= normalize(cross(matTBN[0], matTBN[2]));	// bitangent

	// transform the vertex position
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;


	// now get a view direction for the cubemap relfection in the pixel shader
	vec4 ecPos = gl_ModelViewMatrix * gl_Vertex;
	// simulate finite distance so that flat surface reflect more of the env map (looks better)
	vec3 vViewVec = vec3(0,0,-0.4) - ecPos.xyz*0.2;


//LIGHTING
	//Light0
	vec3 light = normalize(gl_LightSource[0].position.xyz);

	// Convert lightVec to tangent space
	vLight0.x = dot(light, matTBN[0]);
	vLight0.y = dot(light, matTBN[1]);
	vLight0.z = dot(light, matTBN[2]);

	// compute the half angle vector
	light.z += 1.0;
	vec3 halfAngleVector = normalize(light);
	vHalfAngleVec0.x = dot(halfAngleVector, matTBN[0]); 
	vHalfAngleVec0.y = dot(halfAngleVector, matTBN[1]); 
	vHalfAngleVec0.z = dot(halfAngleVector, matTBN[2]); 


	//Light1
	light = normalize(gl_LightSource[0].position.xyz);

	// Convert lightVec to tangent space
	vLight1.x = dot(light, matTBN[0]);
	vLight1.y = dot(light, matTBN[1]);
	vLight1.z = dot(light, matTBN[2]);

	// compute the half angle vector
	light.z += 1.0;


	//HACK: vViewVec is passed in other varying to bypass resource limitations
	vLight0.w			= vViewVec.x;
	vHalfAngleVec0.w	= vViewVec.y;
	vLight1.w			= vViewVec.z;
}