#version 120

uniform int sphereMap;

attribute vec3 tangent1Attrib;
attribute vec3 tangent2Attrib;

varying vec3 normal;
varying vec3 tangent1;
varying vec3 tangent2;
varying vec4 ecPos;

void main()
{
	tangent1 = normalize(gl_NormalMatrix * tangent1Attrib);
	tangent2 = normalize(gl_NormalMatrix * tangent2Attrib);

	normal = normalize(gl_NormalMatrix * gl_Normal);
	ecPos = gl_ModelViewMatrix * gl_Vertex;

	if(sphereMap == 0)
	{
		gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
		gl_TexCoord[1] = gl_TexCoord[0];
	}
	else
	{
		vec3 u = normalize( vec3(gl_ModelViewMatrix * gl_Vertex) );
		vec3 n = normalize( gl_NormalMatrix * gl_Normal );
		vec3 r = reflect( u, n);

		float m = 2.0 * sqrt( r.x*r.x + r.y*r.y + (r.z+1.0)*(r.z+1.0) );
		gl_TexCoord[0].s = r.x/m + 0.5;
		gl_TexCoord[0].t = r.y/m + 0.5;

		gl_TexCoord[1] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
	}

   	gl_Position = ftransform();
}
