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

varying float rLogValueTransferFunction;

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

uniform vec4
  acPos[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;
  if (abs(gl_ModelViewMatrixInverse[3].w) > 1e-5)
    cView -= cCurrVertex.xyz;

  mat3 cMat;
  
  cMat[2] = uniOverrideNormal;
  cMat[0] = normalize(cross(cMat[2], vec3(0.0, 0.0, 1.0)));
  cMat[1] = normalize(cross(cMat[2], cMat[0]));

  float
    rFac = gl_Vertex.y;
  
  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);
  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);
  }
}
