#version 440
#extension GL_ARB_shading_language_packing : require

layout (location=0) in uvec2 vertexPositionI;
layout (location=1) in uvec2 vertexNormalI;
layout (location=2) in uint vertexUVI;
layout (location=3) in uvec2 vertexTangentI;
//layout (location=4) in float vertexBright;

out vec3 normal;
out vec2 uv;
out vec3 tangent;
out vec4 origPos;
out float bright;

uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;

uniform float g_time;

uniform float g_bright = 1.0;
uniform float g_uvScale = 1.0;


vec4 rotateXZ(vec4 p, float a) {
  vec4 r = p;
  r.x = cos(a)*p.x - sin(a)*p.z;
  r.z = sin(a)*p.x + cos(a)*p.z;
  return r;
}

vec3 rotateXZ3(vec3 p, float a) {
  return rotateXZ(vec4(p, 0.0), a).xyz;
}

vec4 rotateXY(vec4 p, float a) {
  vec4 r = p;
  r.x = cos(a)*p.x - sin(a)*p.y;
  r.y = sin(a)*p.x + cos(a)*p.y;
  return r;
}

vec3 rotateXY3(vec3 p, float a) {
  vec3 r = p;
  r.x = cos(a)*p.x - sin(a)*p.y;
  r.y = sin(a)*p.x + cos(a)*p.y;
  return r;
}

vec4 rotateYZ(vec4 p, float a) {
  vec4 r = p;
  r.y = cos(a)*p.y - sin(a)*p.z;
  r.z = sin(a)*p.y + cos(a)*p.z;
  return r;
}
vec3 rotateYZ3(vec3 p, float a) {
  vec3 r = p;
  r.y = cos(a)*p.y - sin(a)*p.z;
  r.z = sin(a)*p.y + cos(a)*p.z;
  return r;
}


vec4 up4(uvec2 v) {
    vec4 r;
    r.xy = unpackHalf2x16(v.x);
    r.zw = unpackHalf2x16(v.y);
    return r;
}
vec3 up3(uvec2 v) {
    return up4(v).xyz;
}
vec2 up2(uint v) {
    return unpackHalf2x16(v);
}

uint p2(vec2 v) {
    return packHalf2x16(v);
}
uvec2 p4(vec4 v) {
    uvec2 r;
    r.x = packHalf2x16(v.xy);
    r.y = packHalf2x16(v.zw);
    return r;
}
uvec2 p3(vec3 v) {
    uvec2 r;
    r.x = packHalf2x16(v.xy);
    r.y = packHalf2x16(vec2(v.z, 0.0));
    return r;
}

void main() {
    vec3 vertexPosition = up3(uvec2(vertexPositionI));
    vec3 vertexNormal = up3(uvec2(vertexNormalI));
    vec2 vertexUV = up2(uint(vertexUVI));
    vec3 vertexTangent = up3(uvec2(vertexTangentI));



  gl_Position = vec4(vertexPosition*0.20, 1.0);

 // gl_Position.y = abs(gl_Position.y);

  vec4 posse = gl_Position*1.0;


  origPos = posse*1.0;

  float timeMul = 0.10;

 
  float times = g_time*0.1*timeMul;
  float times2 = g_time*0.2*timeMul;
  float times3 = g_time*timeMul;

  normal = vertexNormal;
  tangent = vertexTangent;
 // gl_Position.xyz += normal*1.0*(1.0+sin(posse.y*0.4+g_time*3.4));


  float makkara = 0.0*clamp(0.50+1.50*sin(times3*0.4), 0.0, 1.0);

  makkara = 0.0;

  float yk = 1.0;




  vec4 pos1 = rotateXZ(gl_Position, -times3*0.20+yk*posse.y*sin(times2)+0.0*posse.z);
  vec4 pos2 = rotateXZ(gl_Position, -(times3+0.1)*0.20+yk*posse.y*sin(times2+0.02)+0.0*posse.z);

  origPos = pos2-pos1;
/*
  gl_Position = rotateXZ(gl_Position, -g_time*0.20+yk*posse.y*sin(times2)+0.0*posse.z);
  gl_Position.xz *= mix(0.10, 1.0, makkara);
  gl_Position.y *= (1.0+makkara*1.0);
  normal = rotateXZ3(normal, -g_time*0.20+yk*posse.y*sin(times2)+0.0*posse.z);
  tangent = rotateXZ3(tangent, -g_time*0.20+yk*posse.y*sin(times2)+0.0*posse.z);
*/
//  gl_Position.xz *= 1.0+sin(cos(posse.y+g_time*2.0)+g_time*9.2)*0.02;

 // normal = (modelViewMatrix * vec4(normal, 0.0)).xyz;

    vec4 posNR = gl_Position;

    float mk = 1.0;
   gl_Position.y *= 1.0;
   gl_Position.xz *= 1.0;
   /*
   gl_Position = rotateXY(gl_Position, times3*0.0+mk*posse.y*(cos(times)))+0.10*rotateYZ(gl_Position, 0.01*posse.y*(cos(times)));
   normal = rotateXY3(normal, times3*0.0+mk*posse.y*(cos(times)))+0.10*rotateYZ3(normal, 0.01*posse.y*(cos(times)));
   tangent = rotateXY3(tangent, times3*0.0+mk*posse.y*(cos(times)))+0.10*rotateYZ3(tangent, 0.01*posse.y*(cos(times)));
*/
   //   gl_Position = 2.0*rotateYZ(gl_Position, 0.0030*posse.y*(cos(times)));
  // normal = rotateXZ3(normal, -times3*0.20+yk*posse.y*sin(times2)+0.0*posse.z);
  // tangent = rotateXZ3(tangent, -times3*0.20+yk*posse.y*sin(times2)+0.0*posse.z);


   // bright = vertexUV.x+g_bright;// vertexBright;
   bright = g_bright;// vertexBright;


   gl_Position = mix(posNR, gl_Position, makkara);


  uv = vertexUV;

}
