#define PI 3.14159265

uniform vec4 uniOverallColor;
uniform vec4 cRadiusScale;
uniform vec3 cSceneScale;
uniform vec3 uniOverrideNormal;

varying float rLogValueTransferFunction;
varying float rTexU;

varying vec3 v;
varying vec3 vObjPos;
varying vec3 cNormal;
varying vec4 cNoValueColor;

uniform vec4
  acPos[64];

uniform vec2
  acAziInc[64];

uniform float
  arProp[64];

void main()
{
  int
    iVert = int(gl_Vertex.x);

  vec4
    cPrevVertex = acPos[iVert - 1],
    cCurrVertex = acPos[iVert],
    cNextVertex = acPos[iVert + 1];

  rLogValueTransferFunction = arProp[iVert];
  
  vec3
    cView = gl_ModelViewMatrixInverse[3].xyz - cCurrVertex.xyz,
    cViewOrthographic = gl_ModelViewMatrixInverse[2].xyz;

  cView = mix(cView, cViewOrthographic, min(0.999999, gl_ProjectionMatrix[3].w));

  vec2
    cAziIncl = acAziInc[iVert];

  mat3 
    cMat;

  vec2
    cAzimuthVector = vec2(sin(cAziIncl.x), cos(cAziIncl.x));

  cMat[2] = normalize(vec3(cAzimuthVector.xy * sin(cAziIncl.y), -cos(cAziIncl.y))  / cSceneScale);
  cMat[0] = REPLACE_WITH_VIEW_OR_MAT;
  cMat[1] = normalize(cross(cMat[2], cMat[0]));
 
  float
    rFac = gl_Vertex.y;

  REPLACE_CONSISTENT_ASMIUTH
    
  rTexU = rFac * (1.0 / (2.0 * 3.14159265));
 
  if (REPLACE_UV_CALCULATION)
  {
    rTexU = gl_Vertex.z;
  }
  
  rFac -= 0.5 * 3.14159265;

  vec2 cDir = vec2(sin(rFac), cos(rFac));
  
  // CurrVertex.w = relative factor between radius range
  vec3
    cInnerRadius,
    cOuterRadius;

  // Handle novalues
  if (cPrevVertex.w > 3480000000000000000000.0) cPrevVertex.w = cCurrVertex.w;
  if (cNextVertex.w > 3480000000000000000000.0) cNextVertex.w = cCurrVertex.w;
  
  cInnerRadius = vec3(cRadiusScale.y - cRadiusScale.x) * vec3(cPrevVertex.w, cCurrVertex.w, cNextVertex.w) + vec3(cRadiusScale.x);
  cOuterRadius = vec3(cRadiusScale.w - cRadiusScale.z) * vec3(cPrevVertex.w, cCurrVertex.w, cNextVertex.w) + vec3(cRadiusScale.z);

  vec3
    cRadius = (cOuterRadius - cInnerRadius) * gl_Vertex.z + cInnerRadius;

  //2DLOG code will be replace here
  
  vec3 cPos = cMat * vec3(cDir * cRadius.y, 0.0);
  cPos *= cSceneScale;
  
  cPos += cCurrVertex.xyz;
  
  gl_FrontColor = uniOverallColor;
  cNoValueColor = gl_Color;
  
  if (gl_Vertex.w > 0.0)
  {
    cNormal = cMat[1];
  }
  else
  {
    cNormal = cMat * normalize(vec3(cDir, (cRadius.x - cRadius.z) / distance(cPrevVertex.xyz, cNextVertex.xyz)));
    cNormal /= cSceneScale;
    //cNormal = normalize(cNormal * cSceneScale);
    
    if (gl_Vertex.w < 0.0)
    {
      cNormal *= vec3(-1.0);
    }
  }
  
  cNormal = gl_NormalMatrix * cNormal;
  
  gl_Position = gl_ModelViewProjectionMatrix * vec4(cPos, 1.0);
  
  gl_ClipVertex = gl_ModelViewMatrix * vec4(cPos.xyz,1.0);

  vObjPos = (gl_ModelViewMatrix * vec4(cPos, 1.0)).xyz;

  v = vObjPos = vec3(gl_ModelViewMatrix * vec4(cPos.xyz, 1.0));
  vObjPos.z -=  gl_ModelViewMatrix[3][2];
  
  // Discard due to novalue?
  if (acPos[iVert].w > 3480000000000000000000.0)
  {
    gl_Position = vec4(0.0,0.0,1.0,0.0);
  }
}
