title: GLSL refract function explanation available?
body: <p>Im trying to debug my simple raytracer that simulates rays from one spot crossing a lens. I just need the refraction effect for this.</p>
  
  <p>I tried to do it by hand but finally i found <code>refract</code> from GLSL, which is documented in GLSL 1.1 like this:</p>
  
  <pre><code>//For a given incident vector I, surface normal N and ratio of 
  //indices of refraction, eta, refract returns the refraction vector, R. 
  //R is calculated as:
  
  k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I));
  if (k &lt; 0.0)
      R = genType(0.0); // or genDType(0.0)
  else
      R = eta * I - (eta * dot(N, I) + sqrt(k)) * N;
  
  //The input parameters I and N should be normalized in order to 
  //achieve the desired result.
  </code></pre>
  
  <p>I wonder where these equations came from. I searched over the internet and books i own (Real Time Rendering bible, or Phisically Based Rendering last ed.) and all of them refer to this implementation, without explanation, or just tell the solution.</p>
  
  <p>Trying to do it by hand from the begining using geometry brings me to other answers but obviously is hard work just for a very common function like GLSL's. So i prefer to use this optimiced notation.</p>
  
  <p>I only have this:</p>
  
  <pre><code>a.b = |a||b|cos(angle) (Dot product)
  angleOut = eta * sin(angleIn) (Snell's Law).
  </code></pre>
  
  <p>Do you know where it comes from?
  Any document or reference to read?</p>