//#version 330

uniform sampler1D spherePositions;
uniform sampler1D sphereColors;
uniform sampler2D sphereNeighbors;
uniform int sphereCount;
uniform int neighborCount;

vec4 hitSphere;

float raySphere(vec3 ray, vec3 dir, vec3 center, float radius, float closest)
{
  vec3 rc = ray-center;
  float c = dot(rc, rc) - (radius*radius);
  float b = dot(dir, rc);
  float d = b*b - c;
  float t = -b - sqrt(abs(d));
  if (d < 0.0 || t < 0.0 /*|| t > closest*/) {
    return -closest;
  } else {
    return t;
  }
}

float raySphere2(vec3 eye, vec3 dir, vec3 center, float radius)
{
  center = -center;
  float a = dot(dir, dir);
  float b = 2.0 * dot(eye - center, dir);
  float c = dot(eye - center, eye - center) - (radius * radius);
  
  float d = b*b - 4.0*a*c;
  
  if (d >= 0.0)
  {
    float t1 = (-b + sqrt(d)) / (2.0 * a);
    float t2 = (-b - sqrt(d)) / (2.0 * a);
    
    /*if (t1 < 0.0 && t2 < 0.0)
    {
      return -1;
    }
    else
    {
      return 1;
    }*/
    if (t1 > 0.0 && t2 > 0.0)
    {
      return min(t1, t2);
    }
    else
    {
      return -1;
    }
  }
  else
  {
	   return -1;
	}

}


float cube(vec3 p,float s,float t){
	return max(max(abs(p.x)-s,abs(p.y)-s),abs(p.z)-s);
}
float sphere(vec3 p,float r){
	return sqrt(p.x*p.x+p.y*p.y+p.z*p.z)-r;
}

int wang_hash(int seed)
{
    seed = (seed ^ 61) ^ (seed >> 16);
    seed *= 9;
    seed = seed ^ (seed >> 4);
    seed *= 0x27d4eb2d;
    seed = seed ^ (seed >> 15);
    return seed;
}
                 
float rand(int value)
{         
	return float(wang_hash(value)) * (1.0 / 4294967296.0);
}

float dst(vec3 p)
{
  return sphere(p + hitSphere.xyz, hitSphere.w);
}


varying vec3 eye,dir;



//layout(location = 0) out vec4 color;
//layout(location = 1)*/ out vec3 normal;
//layout(location = 2)*/ out float depth;


//layout(location = 0) out vec4 outNormal;
//layout(location = 1) out vec4 outColor;

void main(){
	int i;
	vec3 col,p,n,halfv;
	float mn,e,j,dist=0.,ndotl,t,q;
   
	p=eye;

	col=vec3(0);
    
  float minx = 999.5;
  float closestHit = 9999.0;
  int closestHitIndex = 0;
  
  vec3 useColor = vec3(1,0,0);
  float useId;
    
  int maxSphereCnt = 2;
  
  bool found = false;
  
  float th = 0.0;
  
  dir = normalize(dir);
        
  for (int i = 0; i < sphereCount; i+=1)
  {
    vec4 texel = texelFetch(spherePositions, i, 0);
    vec3 p_ = texel.rgb / 200.0;
    float r_ = texel.a / 200.0;
    
           
        float hit2 = raySphere2(eye, dir, p_, r_ * 1.0);
        if (hit2 > 0.0 && hit2 < closestHit)
        {
          th = hit2;
          closestHit = hit2;
           
                 
      hitSphere = vec4(p_, r_);
      
          vec4 colorTexel = texelFetch(sphereColors, i, 0); 
          useColor = colorTexel.rgb;
          useId = colorTexel.a;
          
          useColor = vec3(useId / 3000.0, 0.0, 0.0);
                    
          found = true;      
        }  
  }  
  
  if (!found)  
  {
    return;
  }
     
  p = eye + dir * th;
  
	e=0.005;
		    
    float d = dst(p);
        
		/*n.x=dst(p+vec3(e,0.0,0.0))-d;
		n.y=dst(p+vec3(0.0,e,0.0))-d;
		n.z=dst(p+vec3(0.0,0.0,e))-d;
		n=normalize(n);*/

  n = normalize(p + hitSphere.xyz);
  
  /*n = p.xyz / 5.0;
  n = -hitSphere.xyz / 5.0;*/
  
 
  

  //normal = 0.5 + n * 0.5;
  gl_FragData[0] /*outNormal*/ = vec4(0.5 + n * 0.5, 1.0);
  
  
  //gl_FragData[0] = vec4(dir.xyz, 1.0);

  //color          
  gl_FragData[1] /*outColor*/ = vec4(useColor, 1);

  //depth          
  vec3 pos3D = eye + dir * th;
  pos3D = vec3(vec4(pos3D, 1.0) * gl_ModelViewMatrix);
  gl_FragData[2] = vec4(vec3((1.0 - pos3D.z) * 0.15), 1.0);
}