attribute float frequency;
attribute float depression;
attribute float StartVertX;
attribute float StartVertZ;

varying vec3 vNormal;
varying vec4 vVertex;


vec3 computeNormal( vec3 position, vec3 tangent, vec3 binormal, float freq )
{
	mat3 J;
	
	float dist = sqrt(position.x*position.x + position.z*position.z);
	//float jac_coef = cos(freq * dist + phase) / (dist+0.00001);
	float jac_coef = (freq * dist) / (dist+0.00001);
	
	// A matrix is an array of column vectors so J[2] is the third column of J.
	
	J[0][0] = 1.0;
	J[0][1] = jac_coef * position.x;
	J[0][2] = 0.0;
	
	J[1][0] = 0.0;
	J[1][1] = 1.0;
	J[1][2] = 0.0;

	J[2][0] = 0.0;
	J[2][1] = jac_coef * position.z;
	J[2][2] = 1.0;
	
	vec3 u = J * tangent;
	vec3 v = J * binormal;
	
	vec3 n = cross(v, u);
	return normalize(n);
}

vec4 displaceVertexFunc( vec4 pos, float frequency ) 
{
	vec4 new_pos;
	float Uposx, Uposz, radDis, height, dist;
	
	new_pos.x = pos.x;
	new_pos.z = pos.z;
	new_pos.w = pos.w;
	
	//circles algorithm find the distance away from the centre and if the vertice is in the radius of the circle its 
	//y position would be changed to reflect the depression
	if(pos.x > StartVertX)
		Uposx = ((pos.x - StartVertX)*(pos.x - StartVertX));
	else
		Uposx = ((StartVertX - pos.x)*(StartVertX - pos.x));
	if(pos.z > StartVertZ)
		Uposz = ((pos.z - StartVertZ)*(pos.z - StartVertZ));
	else
		Uposz = ((StartVertZ - pos.z)*(StartVertZ - pos.z));
	
	dist = sqrt(Uposx + Uposz);
	radDis = dist*2;
	radDis = radDis/depression;
	if ((radDis <= 1.0)&&(radDis >= -1.0))
	{
		new_pos.y = pos.y - (frequency/2 + cos(radDis*3.14)*frequency/2);
	}
	else
	{
		new_pos.y = pos.y;
	}
	
	return new_pos;
}

void main(void)
{
	vec4 displacedVertex;
	vec3 displacedNormal;
	
	//
	// if the engine does not provide the tangent vector you can compute it
	// with the following chunk of code:
	//
	vec3 tangent; 
	vec3 binormal; 
	
	vec3 c1 = cross(gl_Normal, vec3(0.0, 0.0, 1.0)); 
	vec3 c2 = cross(gl_Normal, vec3(0.0, 1.0, 0.0)); 
	
	if(length(c1)>length(c2))
	{
		tangent = c1;	
	}
	else
	{
		tangent = c2;	
	}
	
	tangent = normalize(tangent);
	
	binormal = cross(gl_Normal, tangent); 
	binormal = normalize(binormal);
	
	
	
	displacedVertex = displaceVertexFunc(gl_Vertex, frequency );
   	
	gl_Position = gl_ModelViewProjectionMatrix * displacedVertex;	   	
	vVertex = gl_ModelViewMatrix * displacedVertex;
	
	//vec3 binormal = cross(gl_Normal, tangent.xyz);
	
	displacedNormal = computeNormal( displacedVertex.xyz, tangent.xyz, binormal, frequency );
   	vNormal = normalize(gl_NormalMatrix * displacedNormal);
}
