Im trying to debug my simple raytracer that simulates rays from one spot crossing a lens. I just need the refraction effect for this.
I tried to do it by hand but finally i found refract
from GLSL, which is documented in GLSL 1.1 like this:
//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 < 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.
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.
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.
I only have this:
a.b = |a||b|cos(angle) (Dot product)
angleOut = eta * sin(angleIn) (Snell's Law).
Do you know where it comes from? Any document or reference to read?
The code for smallpt, that is a global illumination renderer, has an implementation of the refract function; that implementation seems to me similar to the one in your question. The implementation is explained in a presentation slides by David Cline; I attach here the relevant slide:
According to Wikipedia the formula is explained in the book:
GLASSNER, Andrews (ed.). An introduction to ray tracing. Morgan Kaufmann, 1989.
In Glassner's book the section 5 in chapter 7 (written by Paul Heckbert) is titled "Derivation of Refraction Formulas", that section is available at Heckbert's page; in that section Heckbert derives the formulas used in those two papers:
WHITTED, Turner. An improved illumination model for shaded display. Comm. ACM, 1980, 23.6.
HECKBERT, Paul S.; HANRAHAN, Pat. Beam tracing polygonal objects. In: ACM SIGGRAPH Computer Graphics. ACM, 1984. p. 119-127.
This ought to be Snell's Law. There's a derivation involving the change in velocity of imaginary 'wavefronts' in a material, which is a nice piece of physics, but probably not really necessary to understand how to use it for rendering. It's pretty universal and works in most situations, although there are cases in optics where it is not sufficient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With