11

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?

Alessandro Jacopson
  • 18,047
  • 15
  • 98
  • 153
Darkgaze
  • 2,280
  • 6
  • 36
  • 59

3 Answers3

9

You can find an explanation in section 16.5 of the book:

FOLEY, James D. (ed.). Computer graphics: Principles and practice, Second Edition in C. Addison-Wesley Professional, 1996.

These are the relevant pages, they came from this Google Books search (consulted at 2013-12-29T10:15:00+01:00):

enter image description here enter image description here

The reference [HECK84] is

HECKBERT, Paul S.; HANRAHAN, Pat. Beam tracing polygonal objects. In: ACM SIGGRAPH Computer Graphics. ACM, 1984. p. 119-127.

and has a detailed appendix with title "Reflection and Refraction Transformations".

Alessandro Jacopson
  • 18,047
  • 15
  • 98
  • 153
  • Cool! Thanks a lot for this reference. Good idea searching in google books. The book you told me had only until page 56... sadly. For sure it´s the best i´ve received ^_^ Thanks! – Darkgaze Dec 29 '13 at 11:19
  • 1
    @darkgaze I actually own a copy of Foley's 1996 book and I firstly checked its index, I then made the Google Books search hoping to find a preview... – Alessandro Jacopson Dec 29 '13 at 11:59
  • Cool, man. Thanks a lot for your time. :-) Keep that Foley's safe. It´s a very good book, indeed. – Darkgaze Dec 30 '13 at 13:03
  • Note that the third edition of the book was released last summer: http://www.amazon.com/Computer-Graphics-Principles-Practice-Edition/dp/0321399528 (not that I think refraction is explained any better, but other topics in CG have changed a lot since 1996 :-) ) – Mortennobel Jan 04 '14 at 14:09
  • @Mortennobel I agree. I own the 3rd edition but I can't find in it the explanation for the formula... – Alessandro Jacopson Jan 04 '14 at 14:20
4

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:

enter image description here

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.

Alessandro Jacopson
  • 18,047
  • 15
  • 98
  • 153
  • Wow. I definetely check it out, pal. Thanks a lot for that! Let me see if that answers my question and i check the answer as valid! – Darkgaze Dec 28 '13 at 20:39
  • 1
    Well. The book is very expensive, nowhere to be found just to see that specific page to see the explanation. Besides, these formulas are still impossible to understand for me. I start with B (the tangent) = sin / cos . Then it says the sinus of angle A is the denominator of B... That doesn't make sense with this rule. So i get stuck again. Please help! – Darkgaze Dec 28 '13 at 21:03
  • I suppose dot(D,N) is cos(theta b) because dot is a*b*cos(angle) and a and b lengths are 1 if the vector is normalized, am i right?. This is not explained at all... – Darkgaze Dec 28 '13 at 21:24
  • Besides... Do N, B and T variables relate to the Binormal/Tangent/Normal names?. That means B is perpendicular to ND plane?. O_O – Darkgaze Dec 28 '13 at 21:50
  • @darkgaze I added a new answer, please see if it fits to you. – Alessandro Jacopson Dec 29 '13 at 09:30
1

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.

Tom W
  • 5,108
  • 4
  • 30
  • 52
  • Yeah. I know. But i want to understand why :-) The reason is that i got some bad looking spherical aberration i want to understand, which is common in normal lenses when a ray crosses them, but i want to check if that happens to using Snells Law, and not the Lens-Maker formula. But thanks! – Darkgaze Dec 28 '13 at 20:40