title: Raytracer Refraction Bug
body: <p>I'm writing a raytracer in C++, and I've been having some issue with refractions. I'm rendering a sphere and a ground plane, and the sphere should refract. However, it looks more like a sphere within a sphere: the "outer" sphere looks to be shaded properly, but not refracting, while the "inner" sphere looks like it's being self-shadowed. Here's a link to what it looks like: <a href="https://imgur.com/QVGkeBT" rel="noreferrer">http://imgur.com/QVGkeBT</a>.</p>
  
  <p>Here's the relevant code.</p>
  
  <pre><code>//inside main raytrace function
       if(refraction &gt; 0.0f){ //the surface is refractive
          //calculate refraction vector
          Ray refract(intersection,
            objList[bestObj]-&gt;refractedRay(
              ray.dir,intersection,&amp;cos_theta,&amp;R0));
          //recurse
          refrColor = raytrace(refract);
        }
        else{ //no refraction
          refrColor = background;
        }
  
  
  //refractedRay(vec3,vec3,float*,float*)
    //...initialize variables, do geometric transforms
    //into air out of obj
    if(dot(ray,normal) &lt; 0){
      n1 = ior;
      n2 = 1.0f;
      *cos = dot(ray,-normal);
    }
    //into obj out of air
    else{
      n1 = 1.0f;
      n2 = ior;
      *cos = dot(ray,normal);
      normal = -normal;
    }
  
    //check value under sqrt
    float n = n1/n2;
    float disc = 1-(pow(n,2)*(1-pow(*cos,2)));
    if(disc &lt; 0){ //total internal reflection
      return ray - 2*-(*cos)*normal; //reflection vector
    }
  
    return (n*ray)+(((n*(*cos))-sqrt(disc))*normal);
  </code></pre>
  
  <p>The sphere used to look worse, then I remembered to normalize my vectors and it looks like this. Previously, it looked like only the inner sphere all throughout. Inside the main raytrace function, I do the refraction the same way as reflection, just using the refracted ray instead. I've also tried modifying the incoming point of intersection and ray with epsilon to check for self-refracting as you can get in shadowing.</p>
  
  <p>Any help would be appreciated :)</p>