#version 420 core
// UNIFORM BLOCK

#define USE_COLOR_PROP BOOL_COLOR_PROP

uniform vec4 cRadiusScale;
uniform vec3 cSceneScale;
uniform float proj33;
uniform float proj1length;

in vec4 aPos;
in vec2 aAziIncl;

#if USE_COLOR_PROP
in float aProp;
out float rLogValueTransferFunction;
#endif

out float rDiscard;

void main(void)
{
  vec3
    cCurrVertex = aPos.xyz;

  vec2
    cAziIncl = aAziIncl;

#if USE_COLOR_PROP
  rLogValueTransferFunction = aProp;
#endif

  float
    rRadiusLog = aPos.w;

  float
    rInnerOuterRadiusFac = 1.0; // this can vary between 0 and 0
  
  rDiscard = 1.0;

  // are we at novalue?
  if (rRadiusLog > 2.0)
  {
    rDiscard = 0.0;
  }

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

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

  cView = mix(cView, cViewOrthographic, min(0.999999, proj33));

  mat3 
    cMat;

  cMat[2] = normalize(vec3(cAzimuthVector.xy * sin(cAziIncl.y), -cos(cAziIncl.y))  / cSceneScale);
  cMat[0] = normalize(cross(cMat[2], vec3(cView / cSceneScale) + vec3(1e-16)));
  cMat[1] = cross(cMat[2], cMat[0]);
 

  // radius
  float
    rInnerRadius = mix(cRadiusScale.x,cRadiusScale.y,rRadiusLog),
    rOuterRadius = mix(cRadiusScale.z,cRadiusScale.w,rRadiusLog);

  float
    rRadius = mix(rInnerRadius, rOuterRadius, rInnerOuterRadiusFac);


  //2DLOG code will be replace here

  vec3 cPos = cMat * vec3(-rRadius, 0.0, 0.0);
  cPos *= cSceneScale;
  cPos += cCurrVertex;

  gl_Position = gl_ModelViewProjectionMatrix * vec4(cPos, 1.0);

// CLIPPLANE CODE
}
