uniform vec3 fvLightPosition;

uniform vec3 fvEyePosition;

uniform vec4 vViewDirection;

uniform float fTime0_X;

varying vec3 ViewDirection;



varying vec2 Texcoord;

varying vec3 LightDirection;

   

attribute vec3 rm_Binormal;

attribute vec3 rm_Tangent;

   

void main( void )

{

   vec4 posi = gl_Vertex;

   posi.y += -2.0+sin(fTime0_X + gl_Vertex.z/10.0)+sin(fTime0_X + gl_Vertex.x/10.0);

  // float dx =   10/10*cos(fTime0_X + gl_Vertex.x/10.0);

//   float dz =   10/10*cos(fTime0_X + gl_Vertex.z/10.0);

   

   //gl_Normal += normalize(cross(vec3(1, dx, 0), vec3(dy, 1, 0)));

   //gl_Normal = normalize(gl_Normal);



   

   mat4 mv = gl_ModelViewMatrix;

   mv[3][0] = 0.0;

   mv[3][2] = 0.0;



   float angle = atan(vViewDirection.x / vViewDirection.z);

   if (vViewDirection.z > 0.0)

      angle+=3.14;



   // WARNING (ROW MAJOR ORDER?)

   mat4 mr = mat4(vec4(1.0,0.0,0.0,0.0),vec4(0.0,1.0,0.0,0.0),vec4(0.0,0.0,1.0,0.0),vec4(0.0,0.0,0.0,1.0));

   mr[0][0] = cos(angle);    mr[0][2] = -sin(angle);

   mr[2][0] = sin(angle);   mr[2][2] = cos(angle);

  

   Texcoord    = gl_MultiTexCoord0.xy;



   gl_Position = gl_ProjectionMatrix  * mv * mr *  posi;

   //gl_Position.x = gl_ProjectionMatrix * gl_Vertex.y;

   //gl_Position.y = gl_ProjectionMatrix * gl_Vertex.z;

       

   vec4 fvObjectPosition = gl_ModelViewMatrix * posi;

   //fvObjectPosition.z += sin(fTime0_X);

   

   vec3 fvViewDirection  = fvEyePosition - fvObjectPosition.xyz;

   vec3 fvLightDirection = fvLightPosition - fvObjectPosition.xyz;

     

   vec3 fvNormal         = gl_NormalMatrix * gl_Normal;

   vec3 fvBinormal       = gl_NormalMatrix * rm_Binormal;

   vec3 fvTangent        = gl_NormalMatrix * rm_Tangent;

      

   ViewDirection.x  = dot( fvTangent, fvViewDirection );

   ViewDirection.y  = dot( fvBinormal, fvViewDirection );

   ViewDirection.z  = dot( fvNormal, fvViewDirection );

   

   LightDirection.x  = dot( fvTangent, fvLightDirection.xyz );

   LightDirection.y  = dot( fvBinormal, fvLightDirection.xyz );

   LightDirection.z  = dot( fvNormal, fvLightDirection.xyz );

   

}