[comp.graphics] Raytracing refraction

po0o+@andrew.cmu.edu (Paul Andrew Olbrich) (01/27/89)

Hi-ho,

I'm trying to add refraction to a ray tracing program I'm writing in C.  Could
someone help me out a bit?  I've pooled stuff from my Physics and Calc texts,
and I think I have it all worked out, but it doesn't look quite right.  Assuming
I really have no clear idea what I'm going, it'd be nice if someone would, in a
wonderfully idealistic way, post the equations I want to use ...

I have the following to work with:
        v = incidence vector
        n = normal vector to the surface
        i = incidence of refraction

All I want here is a three equations like r[X] = ..., r[Y] = ..., and r[Z] =
..., where r is the refracted vector.

Please? ...  Reflection was easy but I'm stumbling on this one.

The solution should handle total internal reflection, too, although that's
probably obvious if you know what you're doing.

Thank you!
                                        - Drew

---
Drew Olbrich
po0o+@andrew.cmu.edu

"Not everything worth doing is worth doing well." -- Tom West

"Love is a snowmobile speeding across the tundra and then suddenly it flips
over, pinning you underneath.  At night, the ice weasels come."  -- Matthew
Groening

mitch@bombadl.SGI.COM (Thomas P. Mitchell) (01/30/89)

In article <0XryqWy00Uo1875Ud-@andrew.cmu.edu>, po0o+@andrew.cmu.edu (Paul Andrew Olbrich) writes:
> Hi-ho,
> 
> I'm trying to add refraction to a ray tracing program I'm writing in C.  Could


Pauls note prompted me to ask others  who have worked with ray tracing
programs the following:

How do people (or do they) manage refraction when the index of
refraction is a function of the wave length of light and in a crystal
the direction of light with respect to the orientation of the crystal
itself.

The reason that this would be valuable is that to ignore it
life would be without rainbows and the fire of diamonds.

PS:

I can live with out Polaroid glasses today so I will not touch
on pleocroism (which I may no longer be able to spell).


--
Thomas P. Mitchell  (mitch@sgi.com)

thant@horus.SGI.COM (Thant Tessman) (01/31/89)

In article <0XryqWy00Uo1875Ud-@andrew.cmu.edu>, po0o+@andrew.cmu.edu (Paul Andrew Olbrich) writes:
> Hi-ho,
> 
> I'm trying to add refraction to a ray tracing program I'm writing in C.  Could
> someone help me out a bit?

Warning: I'm doing this from memory, and I've never written a ray-tracer.


             \<-theta1->|
              \         |
               \        |
incoming ray->  \       | <- surface normal
                 \      |
                  \     |
                   \    |
                    \   |        <material with index of refraction n1> 
                     \  |
                      \ |
                       \|
-------------------------------------------------- <- surface
                        |
                        | \
                        |  \
                        |   \    <material with index of refraction n2>
                        |    \
                        |     \
                        |      \  <- outgoing ray
                        |       \
                        |        \
                        |         \
                        |<-theta2->\


sin(theta1)   sin(theta2)
----------- = ----------
    n1            n2

(theta1 and theta2 are angles)

If n1 is 1 (like for a vacuum) and n2 is bigger than 1 (like glass) then 
theta2 is smaller than theta1.  Note that if it is the other way around,
that is, the ray goes from an optically dense material to an optically
less dense material, (like a ray coming out of glass), then theta2 is 
bigger than theta1.  (Maybe this is where you are screwing up?)

Also, if you are solving for sin(theta2) and you get a number bigger than 1,
it means that the ray should be totally internally reflected.  This is why 
when you look up from the bottom of a pool, everything above the water looks 
like it is in a round window above your head.

There are other considerations.  The ratio of light reflected to  
transmitted is dependent on the angle but I can't remember that stuff.

Also, reflected and transmitted light is polarized (also dependent on the 
angle).  Pelicans have polarized filters in their eyes to see fish in the 
water better.  Bees have polarized eyes to navigate with sunlight on cloudy
days.

Some crystals have different indecies of refraction for light polarized in
different directions.  When you look through them you see two images.

And on and on...

Thant Tessman (thant@sgi.com)
"make money, not war"

tomg@hpcvlx.HP.COM (Thomas J. Gilg) (01/31/89)

>> I'm trying to add refraction to a ray tracing program I'm writing in C. 

> How do people (or do they) manage refraction when the index of
> refraction is a function of the wave length of light and in a crystal
> the direction of light with respect to the orientation of the crystal
> itself.
> The reason that this would be valuable is that to ignore it
> life would be without rainbows and the fire of diamonds.

Question : is the index of refraction linear with repect to the
wavelength ?  I started writting a ray tracer and took liberty in
assuming linear response.  Any experts out there ?

As for crystal alignment, what about using the concept that a
surface is made up of micro-facets.  While this idea is used for
reflectivity, I would think a spin-off idea could handle your
problem.

     Thomas Gilg
tomg%hp-pcd@hplabs.hp.com

jwl@ernie.Berkeley.EDU (James Wilbur Lewis) (01/31/89)

In article <101880003@hpcvlx.HP.COM> tomg@hpcvlx.HP.COM (Thomas J. Gilg) writes:
>Question : is the index of refraction linear with repect to the
>wavelength ?  I started writting a ray tracer and took liberty in
>assuming linear response.  Any experts out there ?

No, it's not linear.  There's a table in Fundamentals of Physics 
(Halliday and Resnick) giving the index of refraction of fused quartz
at various wavelengths.  Between 300 and 800 nm  the index varies
somewhat like 1/(lambda - some constant), with an index of 1.48 at around 
350 nm dropping off to about 1.452 at 800 nm.  The curve probably doesn't 
extrapolate; I would expect a strong dependence on the crystalline structure 
of the medium, discontinuities corresponding to bandgap boundaries, and other
nasty stuff.  

A linear approximation over the visible range probably wouldn't be
too bad, though.

-- Jim Lewis
   U.C. Berkeley

markv@uoregon.uoregon.edu (Mark VandeWettering) (02/01/89)

In article <27839@ucbvax.BERKELEY.EDU> jwl@ernie.Berkeley.EDU.UUCP (James Wilbur Lewis) writes:
>In article <101880003@hpcvlx.HP.COM> tomg@hpcvlx.HP.COM (Thomas J. Gilg) writes:
>>Question : is the index of refraction linear with repect to the
>>wavelength ?  I started writting a ray tracer and took liberty in
>>assuming linear response.  Any experts out there ?

	Well, not me, but I do have a couple of papers by F. Kenton
	Musgrave, who has modified a raytracer to implement _dispersion_
	for effects like rainbows.  He uses a form of stratrified
	sampling over the frequency range. 

	In the January IEE CG&A, there are some pictures done with the
	system, and they look pretty good (read: much better than my
	raytracer :-).  

	Perhaps Mr. Musgrave could comment?  Are you there?

sumane@anaconda.stanford.edu (Thilaka Sumanaweera) (02/01/89)

> Is index of refraction linear with wavelength?

Maxwell's equations yeild the following:
Complex index of refraction of a given material:

		M = n - jK

where,	n = m  e  { 1 + sqrt[ 1 + square( L s / 2 pi c e) ] } / 2
             r  r

	K = m  e  { -1 + sqrt[ 1 + square( L s / 2 pi c e) ] } / 2
             r  r



	m = Relative permeability of the material
         r
	
	e = Ralative permittivity of the material
         r
	L = wavelength of light
	s = conductivity of the material
	pi= 3.14...
	c = speed of light in the material
	e = permittivity of the material

Source: Principles of Optics - Born and Wolf, Pergamon Press 1959

For dielectric materials like glass, s = 0. Therefore K = 0
leaving only the real part for a good approximation.

Hope this is helpful.

Thilaka

coifman@yale.UUCP (Ronald Coifman) (02/02/89)

In article <101880003@hpcvlx.HP.COM> tomg@hpcvlx.HP.COM (Thomas J. Gilg) writes:
>>> I'm trying to add refraction to a ray tracing program I'm writing in C. 
>
>> How do people (or do they) manage refraction when the index of
>> refraction is a function of the wave length of light and in a crystal
>> the direction of light with respect to the orientation of the crystal
>> itself.
>> The reason that this would be valuable is that to ignore it
>> life would be without rainbows and the fire of diamonds.
>
>Question : is the index of refraction linear with repect to the
>wavelength ?  I started writting a ray tracer and took liberty in
>assuming linear response.  Any experts out there ?

  As I had noted some weeks ago in this news group, my Masters thesis was 
on modelling dispersion (refraction as a function of frequency) so I am 
something of an expert on the topic.

  The answer to the above question is "no".  Dispersive power, or amount of 
change of index of refraction with frequency, is modulated by absorption
bands in the transparent material.  The index of refraction curves
asymptotically to infinity at the frequency of the absortion feature.
However, my practical experience has indicated that only an optical lens
designer or some such (overly sensitive!) person notices the difference
between a linear and a nonlinear dispersion curve model.  Besides, it's
not all that easy to get the empirical data for a good nonlinear dispersion
curve.

  A noteable exception to this is the rainbow, where the nonlinearity of the
dispersion curve of water has a very noticeable effect.

  For a thorough discussion of all this, see "Prisms and Rainbows: a Dispersion
Model for Computer Graphics" by F. K. Musgrave in the upcoming Graphics Inter-
face Conference proceedings (June of '89).  Or, as I offered before, email me
a request and I'll send you draft copies of that paper (and another on disper-
sion, if you like).  See also Spencer Thomas, "Dispersive Refraction in Ray
Tracing" in the Jan. '86 issue of Visual Computer (if you can find it!)

					Ken Musgrave
-- 
_____________________________________________________________________
Ken Musgrave			arpanet: musgrave-forest@yale.edu
Yale U. Math Dept.		
Box 2155 Yale Station		Primary Operating Principle:
New Haven, CT 06520				Deus ex machina

ph@miro.Berkeley.EDU (Paul Heckbert) (02/02/89)

Here's some C code to compute the refracted ray direction.

Aside to Thant:
    Actually, Snell's law is n1*sin(theta1)=n2*sin(theta2);
    you were using the reciprocals of the indices of refraction.

Below is an excerpt of some notes I wrote for the Intro to Ray Tracing
SIGGRAPH tutorial.  (These notes are coming out soon as a book from Academic
Press, by Glassner, Arvo, Cook, Haines, Hanrahan, and Heckbert.
Included in the book is a derivation of the following formulas
from Snell's Law, which I would have included here except it's written in
eqn and troff and uses paste-up figures).

---------------------------------

Below is C code for SpecularDirection and TransmissionDirection,
routines which compute secondary ray directions.
The following formulas generate unit output vectors if given unit input vectors.

/*
 * SpecularDirection: compute specular direction R from incident direction
 * I and normal N.
 * All vectors unit.
 */
SpecularDirection(I, N, R)
Point I, N, R;
{
    VecAddS(-2.*VecDot(I, N), N, I, R);
}

/*
 * TransmissionDirection: compute transmission direction T from incident
 * direction I, normal N, going from medium with refractive index n1 to
 * medium with refractive index n2, with refraction governed
 * by Snell's law:  n1*sin(theta1) = n2*sin(theta2).
 * If there is total internal reflection, return 0, else set T and return 1.
 * All vectors unit.
 */
TransmissionDirection(n1, n2, I, N, T)
double n1, n2;
Point I, N, T;
{
    double eta, c1, cs2;

    eta = n1/n2;			/* relative index of refraction */
    c1 = -VecDot(I, N);			/* cos(theta1) */
    cs2 = 1.-eta*eta*(1.-c1*c1);	/* cos^2(theta2) */
    if (cs2<0.) return 0;		/* total internal reflection */
    VecComb(eta, I, eta*c1-sqrt(cs2), N, T);
    return 1;
}

where
    double VecDot(A, B)		dot product: returns A.B
    VecComb(a, A, b, B, C)	linear combination: C = aA+bB
    VecAddS(a, A, B, C)		add scalar multiple: C = aA+B

    typedef double Point[3];	/* xyz point data type */
    Point A, B, C;
    double a, b;

-------------

Whitted gives formulas for the refracted ray direction in his classic
paper on ray tracing: "An Improved Illumination Model for Shaded Display",
CACM, June 1980, but the formulas above compute faster.  It's a fun exercise
in trig and vector algebra to prove that Whitted's formulas are equivalent.

Paul Heckbert, CS grad student
508-7 Evans Hall, UC Berkeley		UUCP: ucbvax!miro.berkeley.edu!ph
Berkeley, CA 94720			ARPA: ph@miro.berkeley.edu

dave@onfcanim.UUCP (Dave Martindale) (02/03/89)

In article <101880003@hpcvlx.HP.COM> tomg@hpcvlx.HP.COM (Thomas J. Gilg) writes:
>Question : is the index of refraction linear with repect to the
>wavelength ?  I started writting a ray tracer and took liberty in
>assuming linear response.  Any experts out there ?

I'm far from an expert.  However, I do know that at least some optical design
is done using a polynomial model of refractive index (n) as a function
of wavelength (w) (my terminal doesn't do lambda!).

The polynomial is:  n**2 = A0 + A1 * w**2 + A2 * w**(-2) + A3 * w**(-4)

			+ A4 * w**(-6) + A5 * w**(-8)

Note that the polynomial doesn't give you n, but n squared.  

The glass manufacturer gives you A0 through A5.  For BK 7, a commonly-used
crown glass, the constants are:

	A0	 2.2718929
	A1	-1.0108077e-2
	A2	 1.0592509e-2
	A3	 2.0816965e-4
	A4	-7.6472538e-6
	A5	 4.9240991e-7

These constants are applicable when the wavelength w is in microns.
For other units of W, scale the constants appropriately.

The wavelength of green light is 0.55 microns; substituting that into
the formula will quickly show which of the terms are insignificant for
your purposes.

I've also seen formulas for the refractive index of synthetic fused silica
and for saphhire, and they are *not* polynomials.  So if you're going to
be general about modelling refractive index, you probably have to allow
for an arbitrary function, not just a polynomial.

By the way, does anyone have a ray-tracer that is intended for optical
design rather than producing pictures?  Are you willing to give it away?
If so, I want it!

	Dave Martindale

pkh@vap.vi.ri.cmu.edu (Ping Kang Hsiung) (02/05/89)

There is an article by Prof. Kunii and his colleagues
about this on the most recent Visual Computer.

--