varying vec3 normal;
varying vec3 tangent;
varying vec3 binormal;
varying vec3 taneye;
varying float pos;

void main()
{
  // position
  pos = length( (gl_ModelViewMatrix * gl_Vertex).xyz );

  // normal
  normal = normalize( gl_NormalMatrix * gl_Normal );
  // calc t and b from n
	vec3 c1 = cross(gl_Normal, vec3(0.0, 0.0, 1.0)); 
	vec3 c2 = cross(gl_Normal, vec3(0.0, 1.0, 0.0)); 
	
	if(length(c1)>length(c2))
	{
		tangent = c1;	
	}
	else
	{
		tangent = c2;	
	}
	
	tangent = normalize(tangent);
	binormal = cross(gl_Normal, tangent); 
	binormal = normalize(binormal);

  // get eye in tangent space
  vec3 modeleye = ( gl_ModelViewMatrixInverse * vec4( 0.0, 0.0, 0.0, 1.0 ) ).xyz;
  modeleye = normalize( modeleye - gl_Vertex.xyz );
  taneye.x = dot( tangent, modeleye );
  taneye.y = dot( binormal, modeleye );
  taneye.z = dot( gl_Normal, modeleye );

  tangent  = gl_NormalMatrix * tangent;
  binormal = gl_NormalMatrix * binormal;
  // end t and b calc

  // set texture coords
  gl_TexCoord[0] = gl_MultiTexCoord0; // Color Map and normal map share coords
  
  // transform position
  gl_Position = ftransform();
}