Title: Ray Tracing
1Ray Tracing
- Alon Efrat
- CS 534
- Advanced Computer Graphics
- (based on a course by John C. Hart)
2Why Write a Ray Tracer?
- More elegant than polygon scan conversion
- Testbed for numerous techniques and effects
- modeling
- rendering
- texturing
- Easiest photorealistic renderer to implement
3Overview
- Vector arithmetic
- Lookat viewing coordinates
- Ray casting
- Shadows
- Reflection
- Refraction
4Homogeneous Coordinates
- Point
- o (xo, yo, zo, 1)
- Direction
- d (xd, yd, zd, 0)
- Points translate, directions do not
- Homogeneous coordinate always zero or one (no
perspective distortion) - Ray
- r (o,d) ((xo, yo, zo,1),(xd, yd, zd,0))
- Ray direction d always unit length
- Not absolutely necessary
- Makes life easier
5Dot Product
b
- a b dot(a,b) lta,bgt
- xa xb ya yb za zb
- a b cos q
- Cosine of angle between a and bif both unit
length - Used to cast a shadow of one unit vector onto
another
q
a
(ab) a
6Cross Product
q
- a ? b cross(a,b)
- (ya zb - za yb, za xb - xa zb, xa yb - ya xb,
0) - a ? b a b sin q
- Returns direction perpendicular to the plane
spanned by a, b - Used to set up coordinate systems
7Eye LUV Coordinates
l (lookat - eye)/(lookat - eye)
v (l ? up)/(l ? up)
u v ? l
up
u
eye
l
v
lookat
- Really called Gram-Schmidt Orthonormalization
8Pixels in World Coordinates
ll eye dl - av - u
aspect ratio a w/h
focal length d 1/tan(fovy/2)
ll
eye
9Casting Rays through Pixels
for (j 0 j lt VRES j) for (i 0 i lt
HRES i) p ll 2av (double)i/HRES 2u
(double)j/VRES d (p - eye)/p - eye r
(eye,d) color TraceRay(r) plot(i,j,color
)
10TraceRay Attributes
- Invoked with ray parameter
- Better if object database is global
- Best if TraceRay is a member function of object
database object (yikes) - Returns a color vector (R,G,B,?)
- ? indicates transparency
- color ? foreground (1- ?) background
11Object Hierarchy
12TraceRay Definition
Color TraceRay(Ray r, int depth) HitObject
ho color c background if (intersects(r,ho))
c ho.shade(lights) / casts a shadow ray
/ c ho.ksTraceRay(ho.reflect(r),
depth-1) c ho.krTraceRay(ho.refract(r),
depth-1) return c
13List Definition
class List public Composite Object
item100 int n boolean intersects(Ray r,
HitObject ho) min_t 1.e20 for (i 0 i
lt n i) if (itemi.intersects(r,o) o.t lt
min_t) min_t o.t ho
o combine_shade(ho) return (min_t lt
1.e20)
14shade Definition
Color shade(Lights lights) Color c ka for
(l 0 l lt lights.n l) ldir
normalized(lightsl.pos - hit) if
(!intersects(ray(hit,ldir))) c
kdcddot(n, ldir) return(c)
15reflect Definition
Ray reflect(Ray r) Ray bounce bounce.o
hit bounce.d 2.0dot(n,-r.d)n
r.d return(bounce)
16Calculating Refraction
Snells Law ?i sin ?i ?t sin ?t
cos ?i n - i
Let ? ?i /?t sin ?t / sin ?i
t sin ?t m - cos ?t n
(sin ?t / sin ?i) (cos ?i n - i) - cos ?t n
(? cos ?i - cos ?t )n - ? i
m (cos ?i n - i) / sin ?i
17refract Definition
Ray refract(Ray r) Ray t double ni
dot(n,-r.d) eta current_index/new_index t.o
hit t.d (etani - sqrt(1.0 -
etaeta(1-nini)))n etar.d return(t)