#version 330

layout( location = 0 ) in vec3 InVertex;
layout( location = 1 ) in vec3 InMovementDirection;
layout( location = 2 ) in float InMovementSpeed;
layout( location = 3 ) in float InScale;

out float vParticleScale;

uniform mat4 uModelMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uProjectionMatrix;
uniform float uTime;
uniform float uCollect;
uniform float uSpiral;
uniform vec3 uStartPosition;

void main()
{
	vec3 centerPos = ( uModelMatrix * vec4( vec3( 0.0 ), 1.0 ) ).xyz;
	vParticleScale = InScale;
	vec3 vPos = InVertex + ( InMovementDirection * ( InMovementSpeed * uTime ) );
	vec3 endPos = vPos + centerPos;
	vec3 centerDir = centerPos - vPos;
	float centerDist = length( centerDir );
	centerDir /= centerDist;
	vec3 newPos = centerDir * centerDist * ( 1.0 - uCollect );
	
	float distanceFromStart = length(newPos - uStartPosition);
	float angle = (distanceFromStart / 1000.0) * uTime * uSpiral;
	mat3 rotationMatrix = mat3(vec3(cos(angle), 0.0, sin(angle)),
								vec3(0.0, 1.0, 0.0),
								vec3(-sin(angle), 0.0, cos(angle)));
	
	newPos -= uStartPosition;
	newPos = rotationMatrix * newPos;
	newPos += uStartPosition;
	
	vec3 dirToStart = uStartPosition - newPos;
	distanceFromStart = length(dirToStart);
	dirToStart = normalize(dirToStart);
	newPos += dirToStart * (distanceFromStart * uSpiral);
	
	gl_Position = uProjectionMatrix * uViewMatrix * vec4( newPos, 1.0 );
}