#version 400

uniform mat4 modelViewProjMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 normalMatrix;

uniform int textureCoordGenMode; // 0 - no generating, 1 - object space, 2 - eye space, 3 - sphere
uniform vec4 planeU;
uniform vec4 planeV;

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 textureCoords;

smooth out vec3 VS_Normal;
smooth out vec3 VS_View;
smooth out vec2 VS_TexCoord;

#define pi 3.141592653589793238462643383279

void main()
{
	VS_Normal = (normalMatrix * vec4(normal, 1.0)).xyz;
	VS_View = -(modelViewMatrix * vec4(position, 1.0)).xyz; 

	//texture coordinates generating
	switch(textureCoordGenMode)
	{
		case 0:
			VS_TexCoord = textureCoords;
			break;
		case 1:
		{
			VS_TexCoord.s = position.x*planeU.x + position.y*planeU.y + position.z*planeU.z + planeU.w;
			VS_TexCoord.t = position.x*planeV.x + position.y*planeV.y + position.z*planeV.z + planeV.w;	
			break;
		}
		case 2:
			//eye space texture coordinates are computed from fragment position
			break;
		case 3:
		{
			vec3 R = normalize(-reflect(normalize(VS_View),normalize(VS_Normal)));
			VS_TexCoord.s = asin(R.x)/pi + 0.5;
			VS_TexCoord.t = asin(R.y)/pi + 0.5;
			break;
		}
		default:
			VS_TexCoord = textureCoords;
	}

	gl_Position = modelViewProjMatrix * vec4(position, 1.0);
}