Docstoc

Ray Tracing Ray Triangle Intersection

Document Sample
Ray Tracing Ray Triangle Intersection Powered By Docstoc
					   Ray Tracing II

      Shadows,
Recursive Ray Tracer,
Reflection, Refraction
Pseudo codes
   RayCast-1
      Plain ray caster (direct illumination)

   RayCast-2
      RayCast-1 + shadow rays

   RayTrace-1
      Recursive ray tracer (+ reflection)

   RayTrace-2
      Recursive ray tracer (+ refraction)
Ray Tracing Issues (RayTrace-1)

      I(P) = Ilocal(P) + ksI(Pr) + kr(Pt)
               Local term    Reflected   Transmitted


1) Cast a ray
2) Determine Intersections
3) For closest Intersection:
     Extend light shadow ray + calculate local term
     Spawn Reflected Ray (go to step 2)
Reflection
   Reflection angle = view angle
Reflection
Pseudo Code for RayTrace-1
// Global Variables

rgb lsou;     // intensity of light source
rgb back;     // background intensity
rgb ambi;     // ambient light intensity

Point light        // position of light source
Object objects [n] // list of n objects in scene
Camera cam;        // camera settings

Int depth; // depth of ray tree consisting of multiple paths

Ray r;
Image RayTrace-1 (int width, int height)
{
  Image image = new Image(width, height);

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {

           r = ConstructRayThroughPixel (i, j);
           image[i][j] = GetColor (r);

       }                                     Notice that now all
    }                                        ray-objects
    return image;                            intersections are
}                                            called within
                                             GetColor()
rgb GetColor (Ray r)
{                                                       Recursive!
    Ray flec; rgb spec, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     if (objects.[hit.id].material.Ks >0) {
                         flec = ComputeReflectionRay (hit) ;
                         spec = objects.[hit.id].material.Ks* GetColor(flec);
                     } else spec = 0;
                Check for shadow // as in RayCast-2()
         intensity = local + spec;      shadow = CheckShadow (hit);
         }                              if (shadow) {
    }                                       Kd = objects[hit.id].material.kd
    depth = depth -1                        local = ambi * Kd
    return intensity;                   }
}                                       else local = Phong_illumination (r, hit);
    Recursive Ray Tracing

   Theoretically, this recursive process could
    continue indefinitely.
    Recursive Ray Tracing

   In practice, at each intersection the ray loses some of
    its contribution to the pixel (i.e. its importance
    decreases).

     if the eye ray hits a specularly reflecting surface
      with reflectivity of 50%, then only 50% of the
      energy hitting the surface from the reflected
      direction is reflected towards the pixel.

     if the next surface hit is of the same material, the
      reflected ray will have its contribution reduced to
      25%.
    Recursive Ray Tracing

   We terminate the recursion if:

     the current recursive depth > a pre-
      determined maximum depth or

     if the ray’s contribution to the pixel < some
      pre-determined threshold 
Reflection
   The maximum depth of the tree affects the handling of refraction
   If we send another reflected ray from here, when do we stop? 2
    solutions (complementary)
      Answer 1: Stop at a fixed depth.
      Answer 2: Accumulate product of reflection coefficients and stop when
       this product is too small.
rgb GetColor (Ray r)
{                                                             Recursive!
    Ray flec; rgb spec, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     if (objects.[hit.id].material.Ks >0) {
                               flec = ComputeReflectionRay (hit) ;
                               spec = objects.[hit.id].material.Ks * GetColor(flec);
                     } else spec = 0;
                Check for shadow // as in RayCast-2()
         intensity = local + spec;      shadow = CheckShadow (hit);
         }                              if (shadow) {
    }                                       Kd = objects[hit.id].material.kd
    depth = depth -1                        local = ambi * Kd
    return intensity;                   }
}                                       else local = Phong_illumination (r, hit);
                                                Memory
                                                 stack   spec = _____ *
                                                         local = ________ shadow? ___
                     __                                  intensity = _____ + _____
       0

                __
                           3                             spec = _____ *
eye                                    __
                                                         local = ________ shadow? ___
      __                                                 intensity = _____ + _____
                     __
           __
                          __       1                     spec = _____ *
                2                                        local = ________ shadow? ___
                                                         intensity = _____ + _____


 0          (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                         spec = _____ *
 1          (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2          (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = _____ + _____
 3          (kd, ks, Phong) = (0.3, 0.8, 0.8)
            (back, ambi) = (0.32, 0.7)           depth = __________________
                                              Memory
                                               stack




      0


                         3
eye




                                 1

            2




 0        (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                       spec = _____ *
 1        (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2        (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = _____ + _____
 3        (kd, ks, Phong) = (0.3, 0.8, 0.8)
          (back, ambi) = (0.32, 0.7)           depth = 0
                                               Memory
                                                stack




       0


                          3
eye
      P1



                                  1

             2




 0         (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                        spec = _____ *
 1         (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2         (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = _____ + _____
 3         (kd, ks, Phong) = (0.3, 0.8, 0.8)
           (back, ambi) = (0.32, 0.7)           depth = 1
                                               Memory
                                                stack




       0


                          3
eye
      P1



                                  1

             2




 0         (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                        spec = _____ *
 1         (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2         (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = _____ + _____
 3         (kd, ks, Phong) = (0.3, 0.8, 0.8)
           (back, ambi) = (0.32, 0.7)           depth = 1
rgb GetColor (Ray r)
{
    Ray flec; rgb spec, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     if (objects.[hit.id].material.Ks >0) {
                               flec = ComputeReflectionRay (hit) ;
                               spec = objects.[hit.id].material.Ks * GetColor(flec);
                     } else spec = 0;
                Check for shadow // as in RayCast-2()
         intensity = local + spec;      shadow = CheckShadow (hit);
         }                              if (shadow) {
    }                                       Kd = objects[hit.id].material.kd
    depth = depth -1                        local = ambi * Kd
    return intensity;                   }
}                                       else local = Phong_illumination (r, hit);
                                               Memory
                                                stack




       0


                          3
eye
      P1
                 R1


                                  1

             2




 0         (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                        spec = 0.8 *
 1         (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2         (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = _____ + _____
 3         (kd, ks, Phong) = (0.3, 0.8, 0.8)
           (back, ambi) = (0.32, 0.7)           depth = 1
                                               Memory
                                                stack




       0


                          3
eye
      P1
                 R1


                                  1                     spec = _____ *
             2                                          local = ________ shadow? ___
                                                        intensity = _____ + _____


 0         (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                        spec = 0.8 *
 1         (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2         (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = _____ + _____
 3         (kd, ks, Phong) = (0.3, 0.8, 0.8)
           (back, ambi) = (0.32, 0.7)           depth = 1
rgb GetColor (Ray r)
{
    Ray flec; rgb spec, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     if (objects.[hit.id].material.Ks >0) {
                               flec = ComputeReflectionRay (hit) ;
                               spec = objects.[hit.id].material.Ks * GetColor(flec);
                     } else spec = 0;
                Check for shadow // as in RayCast-2()
         intensity = local + spec;      shadow = CheckShadow (hit);
         }                              if (shadow) {
    }                                       Kd = objects[hit.id].material.kd
    depth = depth -1                        local = ambi * Kd
    return intensity;                   }
}                                       else local = Phong_illumination (r, hit);
rgb GetColor (Ray r)
{
    Ray flec; rgb spec, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     if (objects.[hit.id].material.Ks >0) {
                               flec = ComputeReflectionRay (hit) ;
                               spec = objects.[hit.id].material.Ks * GetColor(flec);
                     } else spec = 0;
                Check for shadow // as in RayCast-2()
         intensity = local + spec;      shadow = CheckShadow (hit);
         }                              if (shadow) {
    }                                       Kd = objects[hit.id].material.kd
    depth = depth -1                        local = ambi * Kd
    return intensity;                   }
}                                       else local = Phong_illumination (r, hit);
rgb GetColor (Ray r)
{
    Ray flec; rgb spec, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     if (objects.[hit.id].material.Ks >0) {
                               flec = ComputeReflectionRay (hit) ;
                               spec = objects.[hit.id].material.Ks * GetColor(flec);
                     } else spec = 0;
                Check for shadow // as in RayCast-2()
         intensity = local + spec;      shadow = CheckShadow (hit);
         }                              if (shadow) {
    }                                       Kd = objects[hit.id].material.kd
    depth = depth -1                        local = ambi * Kd
    return intensity;                   }
}                                       else local = Phong_illumination (r, hit);
                                               Memory
                                                stack




       0


                          3
eye
      P1
                 R1


                        R2        1                     spec = 0.2 *
             2                                          local = ________ shadow? ___
                                                        intensity = _____ + _____


 0         (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                        spec = 0.8 *
 1         (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2         (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = _____ + _____
 3         (kd, ks, Phong) = (0.3, 0.8, 0.8)
           (back, ambi) = (0.32, 0.7)           depth = 2
                                               Memory
                                                stack




       0


                          3                             spec = 0.4 *
eye                                   R3
                                                        local = ________ shadow? ___
      P1                                                intensity = _____ + _____
                 R1


                        R2        1                     spec = 0.2 *
             2                                          local = ________ shadow? ___
                                                        intensity = _____ + _____


 0         (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                        spec = 0.8 *
 1         (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2         (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = _____ + _____
 3         (kd, ks, Phong) = (0.3, 0.8, 0.8)
           (back, ambi) = (0.32, 0.7)           depth = 3
                                               Memory
                                                stack   spec = _____ *
                                                        local = ________ shadow? ___
                                                        intensity = 0.32
       0


                          3                             spec = 0.4 *
eye                                   R3
                                                        local = ________ shadow? ___
      P1                                                intensity = _____ + _____
                 R1


                        R2        1                     spec = 0.2 *
             2                                          local = ________ shadow? ___
                                                        intensity = _____ + _____


 0         (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                        spec = 0.8 *
 1         (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2         (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = _____ + _____
 3         (kd, ks, Phong) = (0.3, 0.8, 0.8)
           (back, ambi) = (0.32, 0.7)           depth = 4
rgb GetColor (Ray r)
{
    Ray flec; rgb spec, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     if (objects.[hit.id].material.Ks >0) {
                               flec = ComputeReflectionRay (hit) ;
                               spec = objects.[hit.id].material.Ks * GetColor(flec);
                     } else spec = 0;
                Check for shadow // as in RayCast-2()
         intensity = local + spec;      shadow = CheckShadow (hit);
         }                              if (shadow) {
    }                                       Kd = objects[hit.id].material.kd
    depth = depth -1                        local = ambi * Kd
    return intensity;                   }
}                                       else local = Phong_illumination (r, hit);
rgb GetColor (Ray r)
{
    Ray flec; rgb spec, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     if (objects.[hit.id].material.Ks >0) {
                               flec = ComputeReflectionRay (hit) ;
                               spec = objects.[hit.id].material.Ks * GetColor(flec);
                     } else spec = 0;
                Check for shadow // as in RayCast-2()
         intensity = local + spec;      shadow = CheckShadow (hit);
         }                              if (shadow) {
    }                                       Kd = objects[hit.id].material.kd
    depth = depth -1                        local = ambi * Kd
    return intensity;                   }
}                                       else local = Phong_illumination (r, hit);
rgb GetColor (Ray r)
{
    Ray flec; rgb spec, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     if (objects.[hit.id].material.Ks >0) {
                               flec = ComputeReflectionRay (hit) ;
                               spec = objects.[hit.id].material.Ks * GetColor(flec);
                     } else spec = 0;
                Check for shadow // as in RayCast-2()
         intensity = local + spec;      shadow = CheckShadow (hit);
         }                              if (shadow) {
    }                                       Kd = objects[hit.id].material.kd
    depth = depth -1                        local = ambi * Kd
    return intensity;                   }
}                                       else local = Phong_illumination (r, hit);
                                               Memory
                                                stack



                  S1
       0


                          3                             spec = 0.4 *       0.32
eye                                   R3
                                                        local = 0.7 * 0.1 shadow? T
      P1                                                intensity = 0.07 + 0.128
                 R1


                        R2        1                     spec = 0.2 *
             2                                          local = ________ shadow? ___
                                                        intensity = _____ + _____


 0         (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                        spec = 0.8 *
 1         (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2         (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = _____ + _____
 3         (kd, ks, Phong) = (0.3, 0.8, 0.8)
           (back, ambi) = (0.32, 0.7)           depth = 3
rgb GetColor (Ray r)
{
    Ray flec; rgb spec, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     if (objects.[hit.id].material.Ks >0) {
                               flec = ComputeReflectionRay (hit) ;
                               spec = objects.[hit.id].material.Ks * GetColor(flec);
                     } else spec = 0;
                Check for shadow // as in RayCast-2()
         intensity = local + spec;      shadow = CheckShadow (hit);
         }                              if (shadow) {
    }                                       Kd = objects[hit.id].material.kd
    depth = depth -1                        local = ambi * Kd
    return intensity;                   }
}                                       else local = Phong_illumination (r, hit);
rgb GetColor (Ray r)
{
    Ray flec; rgb spec, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     if (objects.[hit.id].material.Ks >0) {
                               flec = ComputeReflectionRay (hit) ;
                               spec = objects.[hit.id].material.Ks * GetColor(flec);
                     } else spec = 0;
                Check for shadow // as in RayCast-2()
         intensity = local + spec;      shadow = CheckShadow (hit);
         }                              if (shadow) {
    }                                       Kd = objects[hit.id].material.kd
    depth = depth -1                        local = ambi * Kd
    return intensity;                   }
}                                       else local = Phong_illumination (r, hit);
rgb GetColor (Ray r)
{
    Ray flec; rgb spec, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     if (objects.[hit.id].material.Ks >0) {
                               flec = ComputeReflectionRay (hit) ;
                               spec = objects.[hit.id].material.Ks * GetColor(flec);
                     } else spec = 0;
                Check for shadow // as in RayCast-2()
         intensity = local + spec;      shadow = CheckShadow (hit);
         }                              if (shadow) {
    }                                       Kd = objects[hit.id].material.kd
    depth = depth -1                        local = ambi * Kd
    return intensity;                   }
}                                       else local = Phong_illumination (r, hit);
rgb GetColor (Ray r)
{
    Ray flec; rgb spec, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     if (objects.[hit.id].material.Ks >0) {
                               flec = ComputeReflectionRay (hit) ;
                               spec = objects.[hit.id].material.Ks * GetColor(flec);
                     } else spec = 0;
                Check for shadow // as in RayCast-2()
         intensity = local + spec;      shadow = CheckShadow (hit);
         }                              if (shadow) {
    }                                       Kd = objects[hit.id].material.kd
    depth = depth -1                        local = ambi * Kd
    return intensity;                   }
}                                       else local = Phong_illumination (r, hit);
                                               Memory
                                                stack



                  S1
       0


                          3                             spec = 0.4 *       0.32
eye                                   R3
                                                        local = 0.7 * 0.1 shadow? T
      P1                                                intensity = 0.07 + 0.128
                 R1


                        R2        1                     spec = 0.2 *
             2                                          local = ________ shadow? ___
                                                        intensity = _____ + _____


 0         (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                        spec = 0.8 *
 1         (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2         (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = _____ + _____
 3         (kd, ks, Phong) = (0.3, 0.8, 0.8)
           (back, ambi) = (0.32, 0.7)           depth = 3
                                                Memory
                                                 stack



                    S1
       0


                           3
eye                                    R3
      P1
                    R1
           S2
                         R2        1                     spec = 0.2 *        0.198
                2                                        local = 0.7 + 0.6 shadow? T
                                                         intensity = 1.3 + 0.0396


 0          (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                         spec = 0.8 *
 1          (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2          (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = _____ + _____
 3          (kd, ks, Phong) = (0.3, 0.8, 0.8)
            (back, ambi) = (0.32, 0.7)           depth = 2
                                                Memory
                                                 stack



                     S1
       0

                S3
                           3
eye                                    R3
      P1
                     R1
           S2
                          R2       1

                2




 0          (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                         spec = 0.8 *      1.3396
 1          (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = 0.8 shadow? F
 2          (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = 0.8 + 1.07168
 3          (kd, ks, Phong) = (0.3, 0.8, 0.8)
            (back, ambi) = (0.32, 0.7)           depth = 1
                                                Memory
                                                 stack



                     S1
       0

                S3
                           3
eye                                    R3
      P1
                     R1
           S2
                          R2       1

                2




 0          (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                         spec = _____ *
 1          (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2          (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = 1.87168
 3          (kd, ks, Phong) = (0.3, 0.8, 0.8)
            (back, ambi) = (0.32, 0.7)           depth = 0
                                                  Memory
                                                   stack




          0


                             3
eye


  intensity = 1.87168

                                     1

                2




 0            (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                           spec = _____ *
 1            (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2            (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = 1.87168
 3            (kd, ks, Phong) = (0.3, 0.8, 0.8)
              (back, ambi) = (0.32, 0.7)           depth = 0
                                              Memory
                                               stack




      0


                         3
eye




                                 1

            2




 0        (kd, ks, Phong) = (0.5, 0.7, 0.5)
                                                       spec = _____ *
 1        (kd, ks, Phong) = (0.1, 0.4, 0.3)            local = ________ shadow? ___
 2        (kd, ks, Phong) = (0.6, 0.2, 0.7)            intensity = _____ + _____
 3        (kd, ks, Phong) = (0.3, 0.8, 0.8)
          (back, ambi) = (0.32, 0.7)           depth = 1
Pseudo codes
   RayCast-1
      Plain ray caster (direct illumination)

   RayCast-2
      RayCast-1 + shadow rays

   RayTrace-1
      Recursive ray tracer (+ reflection)

   RayTrace-2
      Recursive ray tracer (+ refraction)
Ray Tracing Issues (RayTrace-2)

      I(P) = Ilocal(P) + ksI(Pr) + kr(Pt)
               Local term    Reflected   Transmitted


1) Cast a ray
2) Determine Intersections
3) For closest Intersection:
     Extend light shadow ray + calculate local term
     Spawn Reflected Ray (go to step 2)
     Spawn Transmitted Ray (go to step 2)
Refraction
   From “Color and Light in Nature” by Lynch and Livingston
Refraction
                                                             ˆ         ˆ ˆ
                                                             N cosi  I N
                   sin  t t
 Snell’s Law                  r                                            ˆ
                   sin  i i                                  ˆ
                                                               I           i N cosi
                                                                                          ˆ
                                                                                          M
ˆ           ˆ          ˆ
T  sin  t M  cos t N
           ˆ             ˆ                                                     t
   ˆ  ( N cos i  I )
   M                                                                                  ˆ
               sin  i                                                   ˆ
                                                                        N            T

ˆ sin  t ( N cos  I )  cos N
T           ˆ            ˆ      ˆ
                                                               Note that I is the negative of
   sin  i
                       i       t
                                                                   the incoming ray
ˆ                       ˆ     ˆ
T  (r cosi  cost ) N r I
              ˆ ˆ
     cos i  N  I
                                                                 ˆ ˆ
     cos t  1  sin 2  t  1  r2 sin 2  i  1  r2 (1  ( N  I ) 2 )

T  r ( N  I )  1  r2 (1  ( N  I ) 2 )  N  r I
ˆ        ˆ ˆ                      ˆ ˆ ˆ               ˆ
                                              
Refraction
                                    ˆ         ˆ ˆ
                                    N cosi  I N
Snell’s Law                                         ˆ
                                     ˆ
                                     I           i N cosi
                                                                ˆ
 sin  t t
                                                                M

            r
 sin  i i                                    ˆ
                                                     t
                                                            ˆ
                                                            T
                                              N

              Index of Refraction    Note that I is the negative of
                                         the incoming ray




T  r ( N  I )  1  r2 (1  ( N  I ) 2 )  N  r I
ˆ        ˆ ˆ                      ˆ ˆ ˆ               ˆ
                                              
Refraction
                               I = N cos Өi – M sin Өi
                  N            M = (N cos Өi – I) / sin Өi
     – M sin Өi
                               T = – N cos ӨT + M sin ӨT
ηi
     I       Өi N cos Өi
                                 = – N cos ӨT + (N cos Өi – I) sin ӨT / sin Өi
ηT                         M     = – N cos ӨT + (N cos Өi – I) ηr
                                 = [ ηr cos Өi – cos ӨT ] N – ηr I
                  ӨT             = [ ηr cos Өi – √1 – sin2 ӨT ] N – ηr I
            -N         T
                                 = [ ηr cos Өi – √1 – ηr2 sin2 Өi ] N – ηr I
                                 = [ ηr cos Өi – √1 – ηr2 (1 – cos2 Өi ) ] N – ηr I
                                 = [ ηr (N · I) – √1 – ηr2 (1 – (N · I)2 ) ] N – ηr I
 Snell-Descartes Law:
 ηi sin Өi = ηT sin ӨT
                               • Total internal reflection when
 sin ӨT   ηi                     the square root is imaginary
        =    = ηr
 sin Өi   ηT                   • Don’t forget to normalize!
Refraction & Sidedness of Objects
   Make sure you know whether you’re entering or leaving
    the transmissive material:
                        N                       I
          I
                                    ηi = material
        ηi=1                             index
        ηT = material               ηT= 1
             index

                                                        T
                            T
                                                    N
Ray   P
                                        P             ?



                           indexmedium
                   index 
                           indexmaterial


ˆ   ( N  I )  1   2 (1  ( N  I ) 2 )  N   I
T  r ˆ ˆ                         ˆ ˆ ˆ               ˆ
                       r
                                                    r
P   NewRay
        indexmedium
index 
        indexmaterial       entry_position


                                                            P

 Attention to the order!
                              indexmaterial                     ?
                      index 
                              indexmedium


 ˆ   ( N  I )  1   2 (1  ( N  I ) 2 )  N   I
 T  r ˆ ˆ                         ˆ ˆ ˆ               ˆ
                        r
                                                     r
entry_position


                    P




                 NewRay
Pseudo Code for RayTrace-2
// Global Variables (same as in RayTrace-1)

rgb lsou;     // intensity of light source
rgb back;     // background intensity
rgb ambi;     // ambient light intensity

Point light        // position of light source
Object objects [n] // list of n objects in scene
Camera cam;        // camera settings

Int depth; // depth of ray tree consisting of multiple paths

Ray r;
// Same as in RayTrace-1

Image RayTrace-2 (int width, int height)
{
   Image image = new Image(width, height);

    for (int i = 0; i < width; i++) {
         for (int j = 0; j < height; j++) {

            r = ConstructRayThroughPixel (i, j);
            image[i][j] = GetColor (r);

        }
    }
    return image;
}
rgb GetColor (Ray r)
{                                                           Recursive!
    Ray flec, frac; rgb spec, refr, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     Check for reflection // as in RayTrace-1()

                   if (objects.[hit.id].material.Ks[j] >0) {
                             flec = ComputeReflectionRay (hit) ;
                             spec = Ks[j] * GetColor (flec);
                   } else spec = 0;

                 Check for shadow // as in RayCast-2()
         intensity = local + spec;
         }
    }
    depth = depth -1
    return intensity;
}
rgb GetColor (Ray r)
{                                                           Recursive!
    Ray flec, frac; rgb spec, refr, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     Check for reflection // as in RayTrace-1()
                                      if (objects.[hit.id].material.Ks >0)
                   if (objects.[hit.id].material.Ks[j] >0) {
                                      {
                             flec = ComputeReflectionRay (hit) ;;
                                      flec = ComputeReflectionRay (hit)
                                        Ks[j] * GetColor (flec);
                             spec =spec = objects.[hit.id].material.Ks * GetColor (flec);
                   } else spec = 0;} else spec = 0;

                 Check for shadow // as in RayCast-2()
         intensity = local + spec;       shadow = CheckShadow (hit);
         }                               if (shadow) {
    }                                        Kd = objects[hit.id].material.kd
    depth = depth -1                         local = ambi * Kd
    return intensity;                    }
}                                        else local = Phong_illumination (r, hit);
rgb GetColor (Ray r)
{
    Ray flec, frac; rgb spec, refr, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     Check for reflection // as in RayTrace-1()

                   if (objects.[hit.id].material.Kr >0) {
                             frac = ComputeRefractionRay (hit) ;
                             refr = objects.[hit.id].material.Kr * GetColor (frac);
                   } else refr = 0;

                 Check for shadow // as in RayCast-2()
         intensity = local + spec + refr;
         }
    }
    depth = depth -1
    return intensity;
}
rgb GetColor (Ray r)
{                                                           Recursive!
    Ray flec, frac; rgb spec, refr, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     Check for reflection // as in RayTrace-1()

                   if (objects.[hit.id].material.Kr[j] >0) {
                             frac = ComputeRefractionRay (hit) ;
                             refr = objects.[hit.id].material.Kr * GetColor (frac);
                   } else refr = 0;

                 Check for shadow // as in RayCast-2()
         intensity = local + spec + refr;
         }
    }
    depth = depth -1
    return intensity;
}
rgb GetColor (Ray r)
{
    Ray flec, frac; rgb spec, refr, local, intensity;
    depth = depth +1
    if (depth >= 5) intensity = back;
    else {
          Intersection hit = FindIntersection (r);
          if (hit.id = -1) intensity = back;
          else {
                     Reflection

                   if (objects.[hit.id].material.Kr[j] >0) {
                  Refraction
                             frac = ComputeReflectionRay (hit) ;
                             refr = Ks[j] * GetColor (frac);
                  Shadowing= 0;
                   } else refr

         intensity = local + spec + refr;
         }
    }
    depth = depth -1
    return intensity;
}

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:71
posted:3/26/2011
language:English
pages:59