Ray Tracing - PowerPoint - PowerPoint by juanagui

VIEWS: 187 PAGES: 14

									   Ray Tracing

       John C. Hart
         CS 319
Advanced Computer Graphics
Why Trace Rays?
• More elegant than polygon rasterization,
  especially for sophisticated physics

• Testbed for sophisticated methods
   – modeling light reflectance from e.g. skin,
     and light transport e.g. through a cloud
   – rendering using e.g. Monte Carlo
     integration from random samples
   – texturing using e.g. “hypertexture” where
     texture becomes geometric

• Easiest photorealistic global illumination
  renderer to implement

                                                  Images stolen from Henrik Wann Jensen
Why not OpenGL?
• OpenGL uses an environment map for
  reflections
• Environment map assumes object
  infinitesimally small and reflections
  infinitely far away
• Can simulate reflection from still water

                                             Boat reflected in wavy water rendered in
                                               OpenGL using an environment map
                                                    Can you find three things
           env.                                     wrong with this picture?
           map
                                                 1. Reflection doesn’t meet boat
                                                 2. Reflection behind the boat
• But not wavy water (via bump map)              3. Environment map magnified
                                                      Points translate:
Homogeneous Coords                                    1   0 0 a  x   x  a
                                                      0   1 0 b   y   y  b
                                                                          
                                                      0   0 1 c z   z  c 
                                                                           
                                                      0   0 0 1 1   1 
                Point: p = (xp, yp, zp, 1)
                                                      Directions don’t:
                Direction: d= (xd, yd, zd, 0)         1   0 0 a  x   x 
                Homogeneous w = 0 or 1 usually        0   1 0 b  y  y
                                                                   
                (unless using perspective matrix)     0   0 1 c z   z 
                                                                  
                                                      0   0 0 1 0  0 
Anchor o
                Ray r = (o,d)                         Translating rays:
  Direction d                                         Tr = (To,Td) = (To,d)
                ||d|| = 1 sometimes, sometimes not!
Dot (Inner) Product
                                               a    q
         a · b = b · a = dot(a,b) = <a,b>
                                                   b
               = x a xb + ya yb + z a z b
                    = |a| |b| cos q
• Cosine of angle between a and b
  if both unit length
• Used to “cast a shadow” of one vector onto
  another                                                    b
    – Shadow of b on a
                 (a/||a||)·(b/||b||) a                       b’
    – Shadow of a on b                              q
                 (a/||a||)·(b/||b||) b                             a
• Gram-Schmidt Orthogonization                    (a·b) a
    – Portion of b perpendicular to a          ||a|| = ||b|| = 1
           b’ = b – (a/||a||)·(b/||b||) a
                                                                     c=ab
Cross Product
                                                                a   q           ca
                   a  b = cross(a,b)
                                                                    b
     = (ya zb - za yb, za xb - xa zb, xa yb - ya xb, 0)

                       x      y     z                     Given two non-|| vectors a
                    xa      ya     za                    and b, the vectors a, a  b
                       xb    yb     zb                     and (a  b)  a form an
                                                           orthogonal right-handed
                  |a  b| = |a| |b| sin q                     coordinate system.
•   Returns direction perpendicular to the plane
    spanned by a, b (direction is right-handed)
•   If a,b unit length and perpendicular, then a  b
    is unit length
•   Used to set up coordinate systems
•   Not commutative! (a  b ≠ b  a)
Eye 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)
Pixels in World Coords
                                                 -av
• aspect ratio a = w/h                    -u
• focal length d = 1/tan(fovy/2)
                                           ll
                                                   dl
                                                fovy
         ll = eye + d l – a v – u
                                    eye

 for (j = 0; j < VRES; j++) {
   for (i = 0; i < HRES; i++) {
       p = ll + 2av (double)i/HRES + 2u
   (double)j/VRES;
       color = TraceRay(Ray(eye, p - eye));
       plot(i,j,color);
   }
 }
TraceRay
                                                         Ray r
                                                                  d
• TraceRay(r = (o,d)) returns the                   o
  intensity of light arriving at the ray
  anchor o in the opposite direction (-d)
                                                        TraceRay(r)
• Invoked with ray parameter only
   – Better if object database is global
   – Best if TraceRay is a member
      function of object database
• Returns intensity across         Color TraceRay(Ray r, int depth) {
                                      Color c = background;
  the visible spectrum                if (!depth) return c;
   – e.g. an RGB triple               if ((hit = Intersect(r)) != NULL)   {
                                        hit->depth = depth – 1;
                                        c = hit->Shade();
                                      }
                                      return c;
                                  }
Intersect
                                                      Object
• Object->Intersect(r) returns the
  intersection of a ray r with Object          r
• Returns a list of hit records,
  each containing:                            hit[0]->t

   t – ray parameter of intersection          hit[0]->x
   x – location of intersection (or can be    hit[1]->x
      computed as r(t))
   Shade() – routine to compute returned
      color of light reflected by Object at
      intersection point x
Object Hierarchy

                                   Object

                                 TraceRay()
                               Intersect() = 0


              Primitive                            Composite



 Sphere        Polygon        Patch              List        CSG
Intersect()   Intersect()   Intersect()      Intersect()   Intersect()
List
/* Only returns first intersection */
typedef std::list<Object *> ObjList;
typedef std::list<HitRecord> HitList;

class List : public Composite, std::list<Object *> {
  HitList Intersect(Ray r) {
    HitList hits,first_hit;
    double min_t = 1.e20; // or anything big
    for (obj = begin(); obj != end(); obj++) {
      hits = (*obj)->intersect(t);
      if (hits.size() && hit[0].t > 0 && hit[0].t < min_t)
        min_t = hit[0].t;
        first_hit = ObjList(hit);
      }
    }
    return first_hit;
  }
}
Shade
Color Object::Shade() {
  Color c = ka*ca;
  for (l = lights.begin(); l != lights.end(); l++) {
    ldis = length(l.pos – x);
    ldir = (l.pos – x)/ldis;
    obstructors = Intersect(ray(x,ldir));
    if (!obstructors.size() || obstructors[0].t >= ldis)
      c += kd*cd*l.color*dot(n, ldir) +
           ks*cs*l.color*pow(dot(l,Reflect(r)),specpow)
    c += kr*TraceRay(Reflect(r));
    c += kt*TraceRay(Refract(r));
  }
  return c;
}                                  Ray Reflect(Ray r) {
                   o.n               r.d.Normalize();
                                          return Ray(x,2*dot(n,-r.d)*n + r.d);
                r.d      Reflect(r)
                                      }
                                                                                n
Refract                                                        cos qi n - i
                                                                           qi             cos qi n
                                                                    i
Snell’s Law: i sin qi = t sin qt                                                             m
Let  =i /t = sin qt / sin qi
Let m = (cos qi n - i) / sin qi
                                                                                     qt
Then…                                                                                        t=?
t = sin qt m - cos qt n
                                                                                -n
  = (sin qt / sin qi) (cos qi n - i) - cos qt n
  = ( cos qi - cos qt )n -  i
                                                               cosq t  1  sin 2 q t
                                      
t   (n  i)  1  2 (1  (n  i) 2 ) n i                            1   2 sin 2 q i

Can be negative for grazing    Ray refract(Ray r) {
angles when  >1, say when       double ni = dot(n,-r.d);
going from glass to air,         double eta = current_index/new_index;
resulting in total internal      return Ray((x,eta*ni - sqrt(1 - eta*eta*(1-ni*ni)))*n + eta*r.d);
reflection (no refraction).    }

								
To top