Refraction

CS 381 Lecture, Dr. Lawlor

So curiously enough, light's speed changes depending on what the light is moving through.  This speed change changes the wavelength of the light.  For example, red light has a wavelength of 650nm in air, but the same light has a wavelength of only 260nm in diamond, because light travels about 2.5 times more slowly through diamond than through air.

This wavelength change has a very strange effect when light passes out of one material and into another material.  The wavelength changes, but wave crests can't be created or destroyed at the interface, so to make the waves match up, the light has to change direction.  Here's a picture of what's going on:
Derivation of Snell's Law

Here w1 is the wavelength in material 1, w2 is the wavelength in material 2, L is half the width of incoming light on the surface, and a1 and a2 are the angles between the wave fronts and the surface.  a1 and a2 are also the angles between the light direction and surface normal!

It's just a bit of trig to figure out that
    sin(a1) = w1 / L
and
    sin(a2) = w2 / L

And dividing the two equalities above, we get
    sin(a1)/sin(a2) = w1/w2

This is called "Snell's Law"--the ratio of sines is the ratio of wavelengths, which is also the ratio of light speeds in the two materials.  In fact, the speed of light in air divided by the speed of light in a material  is called the material's "Index of Refraction".  A higher refractive index means slower light, smaller wavelengths, and hence more light bending.  Water's index of refraction is a mild 1.3; diamond's is a high 2.4 (this is what makes diamonds sparklier than ice).

In a raytracer, it's easy enough to compute refraction.  In GLSL, there's even a builtin routine "refract" that takes the incoming (camera to object) direction, surface normal, and "eta" (1.0/index of refraction), and computes the refracted vector.  It's easy enough to drop this direction computation into a raytracer!

Fresnel Reflectance and Refractance

In addition to the direction change when entering a new material (computed by Snell's law), there's a brightness tradeoff between reflected and refracted light.  The Fresnel formulas can be used to compute the actual relative brightness of the reflected and refracted light, but they're pretty ugly to use:
However, the overall behavior of the fresnel equations is pretty important--the bottom line is that the steeper the incoming light (closer to parallel to the surface, perpendicular to the normal, the more reflection you get and the less refraction).

So there's a sort of cottage industry of "graphics-quality" approximations to the fresnel formulas.  One classic approximation looks a heck of a lot like Phong shading--
    float reflectivity=pow(1.0-clamp(dot(N,I),0.0,1.0),4.0);
That is, the reflectivity is near zero if we're looking straight down on the surface (N and I parallel, dot product near 1); and near 1.0 if we're looking at right angles to the surface (N and I near perpendicular, dot product near 0).

This sort of thing is also easy to drop into a raytracer.


Cool Demo Raytracers

Trezebees and Trezebees II are some amazingly optimized software raytracers. They run at smooth framerates on modern CPUs at resolutions up to about 400x400.

Paul Swanson recommends the hardware non-raytracing Farbrausch demos.