#version 330

uniform sampler2D readTexture;

uniform float size;

uniform float time;

uniform vec3 force_field;

in vec2 vertex;

out vec4 newParticle;

float rand(vec2 seed)
{
	return fract(sin(dot(seed.xy, vec2(12.9898, 78.233))) * 43758.5453);
}

void main(void)
{
	newParticle = texture(readTexture, vertex);

	float decay_xz = 1;

	newParticle.x += (rand(vertex*123.456)*2.0-1.0)*0.5*time * decay_xz;
	newParticle.y += -abs((rand(vertex*234.567)*2.0-1.0)*0.9*time);
	newParticle.z += (rand(vertex*345.678)*2.0-1.0)*0.5*time * decay_xz;
	newParticle.w += 0.007*time;

	float factor = 0.1;
	newParticle += vec4(force_field, 0.0) * factor;


	if (newParticle.w > 1.0)
	{
		newParticle.x = 0.0 - (rand(vertex*153.456)*2.0-1.0)*50.0;
		newParticle.y = 0.0;
		newParticle.z = 0.0 - (rand(vertex.yx*153.456)*2.0-1.0)*50.0;
		newParticle.w = rand(vertex*456.789);
	}
	
	
	//gl_PointSize = 1.0;
	
	gl_Position = vec4(vertex*2.0-vec2(1.0-1.0/size, 1.0-1.0/size), 0.0, 1.0);
}
