#version 400
in vec3 position;
in vec3 velocity;
//Outs
out vec3 position_out;
out vec3 velocity_out;
//Uniforms
uniform float t;
uniform samplerBuffer tex_position;
uniform samplerBuffer tex_velocity;
//consts
const float c = 0.04;
const vec3 gravity = vec3(0.0,-0.03,0.0);
const float k = 1.0;
void main(void)
{
int N = 64;
vec3 F; //Force on the mass
vec3 v = velocity; // Final velocity;
vec3 s = vec3(0.0); //Displacement step
int index = gl_VertexID;
int ix = index%N;
int iy = index/N;
int connection[4];
if(ix == 0)
{
connection[0] = index;
}
else if(ix == N-1)
{
connection[2] = index;
}
else
{
connection[0] = index-1;
connection[2] = index+1;
}
if(iy == 0)
{
connection[1] = index;
}
else if(iy == N-1)
{
connection[3] = index;
}
else
{
connection[1] = index-N+1;
connection[3] = index+N-1;
}
if(index != 0 && index != N-1)
{
F = gravity- c*velocity;
for(int i=0; i< 4; i++)
{
if(connection[i] != index)
{
vec3 q = texelFetch(tex_position,connection[i]).xyz;
vec3 d = q-position;
float x = length(d);
F += -k*(1-x)*normalize(d);
}
}
s = velocity*t+0.5*F*t*t;
v = velocity + F*t;
}
position_out = position + s;
velocity_out = v;
}