#version 130

#define NUM_LIGHTS 5

// ----------------------------------------------------------------------------
// Constants and Structs
// ----------------------------------------------------------------------------

struct FloatArray {
	float Float;
};


// ----------------------------------------------------------------------------
// Output values to the Fragment Shader
// ----------------------------------------------------------------------------

out vec2 Vertex_UV;
out vec3 Vertex_Normal;
out vec4 Vertex_Position;
out vec3 Vertex_Eyevector;

out vec3 Vertex_LightDir[NUM_LIGHTS];
out vec3 Vertex_LightColor[NUM_LIGHTS];
out float Vertex_LightRange[NUM_LIGHTS];

out vec3 Vertex_AmbientColor;


// ----------------------------------------------------------------------------
// Attributes from the main program
// ----------------------------------------------------------------------------

uniform vec2 texscale;
uniform vec2 texoffset;
uniform float ambFactor;
uniform FloatArray lightradius[NUM_LIGHTS];


// ----------------------------------------------------------------------------
// Main Vertex Shader
// ----------------------------------------------------------------------------

void main()
{
	// Normal, Fragment and UV coordinates
	Vertex_Normal = normalize(gl_NormalMatrix * gl_Normal);
	Vertex_Position = gl_ModelViewMatrix * gl_Vertex;
	Vertex_UV = (gl_MultiTexCoord0.xy * texscale) + texoffset;
	
	// Eye vector
	vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
	vec3 ecPosition3 = (vec3(ecPosition)) / ecPosition.w;
	Vertex_Eyevector = -normalize(ecPosition3);

	// Light properties
	for (int i = 0; i < NUM_LIGHTS; ++i)
	{
		Vertex_LightDir[i] = gl_LightSource[i].position.xyz - Vertex_Position.xyz;
		Vertex_LightColor[i] = gl_LightSource[i].diffuse.rgb;
		Vertex_LightRange[i] = lightradius[i].Float * lightradius[i].Float;
	}
	
	// Ambient color
	Vertex_AmbientColor = gl_LightModel.ambient.rgb * ambFactor;

	gl_Position = ftransform();
}