varying vec3 center;
varying vec2 coord;

varying float radius;

uniform mat4 pm;
uniform mat4 mvm;
             
varying vec4 position;

void main(void)
{
  vec3 p;
  p.xy = 2.0 * coord - 1.0;
  float mag = dot(p.xy, p.xy);
  if (mag > 1.0)
    discard;
  mag = length(p.xy);
  p.z = sqrt(1.0 - mag * mag);
  //p = normalize(p);
  //vec3 viewPos = vec3(gl_ModelViewProjectionMatrix * vec4(center, 1.0)) - p;
  //vec3 viewCen = vec3(gl_ModelViewProjectionMatrix * vec4(center, 1.0));

  /*vec4 viewPos4 = gl_ModelViewProjectionMatrix * vec4(center, 1.0) - vec4(p, 1.0);
  vec4 viewCen4 = pm * mvm * vec4(center, 1.0);
  
  vec3 viewPos = viewPos4.xyz / viewPos4.w;
  vec3 viewCen = viewCen4.xyz / viewCen4.w;

  vec3 pos3D = vec3(vec4(center + p, 1.0) * gl_ModelViewMatrix);
  vec3 cen3D = vec3(vec4(center, 1.0) * gl_ModelViewMatrix);*/
  
  vec4 viewPos4 = pm * mvm * vec4(center, 1.0);
  vec3 viewPos = viewPos4.xyz /*/ viewPos4.w*/ - p;
  
  vec4 viewCen4 = pm * mvm * vec4(center, 1.0);
  vec3 viewCen = viewCen4.xyz /*/ viewCen4.w*/;

  vec3 pos3D = vec3(vec4(center + p, 1.0) * mvm);
  vec3 cen3D = vec3(vec4(center, 1.0) * mvm);
  
  //object space normlas 
  gl_FragData[0] = 0.5 + 0.5 * (vec4(normalize(pos3D - cen3D), 1.0) ) * 1.0;
  
  vec3 normalVector = viewPos - viewCen4.xzy;
  /*normalVector.z = 1.0 - normalVector.z;
  normalVector.y = 1.0 - normalVector.y;
  normalVector.x = 1.0 - normalVector.x;*/  
 
  //gl_FragData[0] = 0.5 + 0.5 * (vec4(normalVector, 1.0) ) * 1.0;
  
  gl_FragData[0] = (vec4(0.5 + 0.5 * p, 1.0) );
  
  
  gl_FragData[1] = gl_Color;
  //gl_FragData[2] = vec4(vec3((16.0 - viewPos.z) * 0.05), gl_Color.a);
      
  float far=1.0;//gl_DepthRange.far;
  float near=0.0;//gl_DepthRange.near;

  vec4 eye_space_pos = mvm * vec4(center + p * radius * 0.5, gl_Color.a);
  vec4 clip_space_pos = pm * eye_space_pos;
  float ndc_depth = clip_space_pos.z / clip_space_pos.w;

  //float depth = (((far-near) * ndc_depth) + near + far) / 2.0;
  

  float fragmentDepth = radius * 0.5 * p.z;
  float depth = -(center.z - near) / (far - near)  
  + radius * 0.5 - fragmentDepth;
  
  float nd = (gl_FragCoord.z - near) / (far - near);
  depth = gl_FragCoord.z + (p.z * radius - near) / (far - near);
  
  
  vec4 t = pm * mvm * vec4(center.xyz, 1.0);

  //depth = t.z / t.w + fragmentDepth * radius;
  //depth = t.z / t.w;
  depth = (t.z - 0.1) / (16.0 - 0.1);  

  //far = 3.0;
    float z_b = depth;
    float z_n = 2.0 * z_b - 1.0;
    float z_e = 2.0 * near * far / (far + near - z_n * (far - near)) * 0.5;  
   
  gl_FragData[2] = vec4(vec3(depth), 1.0);
  
  gl_FragData[3] = vec4(0.5 + 0.5 * (center +
  (vec4(p,1.0) * pm * mvm).xyz)  
  * 0.1, gl_Color.a);
      
  
    float A = pm[2].z;
    float B = pm[3].z;
    //gl_FragDepth  = 0.5 * (-A*depth + B) / depth + 0.5;  
    //gl_FragDepth  = z_e;
    

    //vec4 zVector = vec4(0.0, 0.0, radius, 0.0);
    //vec4 eyeRadius = pm * mvm * zVector;
    
    //vec4 objectSpaceShifted = inverse(mvm) * inverse(pm) * (vec4(0.0, 0.0, -radius, 0.0));    
    //vec4 backToTheEye = pm * mvm * vec4(normalize(center - objectSpaceShifted), 0.0);
    
    
    vec4 ir = inverse(pm) * vec4(0.0, 0.0, -1.0, 0.0);
    vec4 be = pm * vec4(normalize(center - ir) * radius, 0.0);
    
     
    
    //gl_FragDepth = gl_FragCoord.z - p.z * length(zVector);
    gl_FragDepth = gl_FragCoord.z - p.z * radius;
    //gl_FragDepth = gl_FragCoord.z;
    
    //gl_FragData[0] = vec4(0,0,0,1);
    
    
    //gl_FragData[0] = vec4(vec3(gl_FragCoord.z - p.z * backToTheEye.z + backToTheEye.z), 1.0);
    //gl_FragData[0] = gl_Color * vec4(vec3(p.z), 1.0);
    
    
   //vec4 ssPos = pm * mvm * vec4(center + p, 1.0);
   vec4 ssPos = vec4(position.xyz, 0.0);
   
   //ssPos.z *= -1;
   gl_FragData[4] = vec4(ssPos.xyz, depth);
   
   //gl_FragDepth = ssPos.z; 

    //gl_FragDepth = p.z;
}