第六课 Ray Tracing

Document Sample
第六课 Ray Tracing Powered By Docstoc
					第五课 Ray Tracing
Overview of the Section

   Why Ray Tracing?
   What is Ray Tracing?
   How to tracing rays?
   How to accelerating ray-tracing?
Object-ordering rendering
   Each polygon or triangle is processed in
    turn and its visibility is determined using
    algorithms such as Z-buffer
   After determining its visibility, the
    polygon or triangle is projected onto the
    viewing plane
   Polygon or triangle is shaded using
    Phong Model\
   OpenGL, rasterization (光栅化)
Object-ordering rendering
   Advantage
       Fast rendering can be implemented on
   Disadvantage
       Difficult to model lighting effects such as
        reflection or transparency
       Difficult to model lighting effects are caused
        by the interaction of objects, i.e. shadows
   Assumption
       Light travels from visible objects towards the
Ray tracing

   Question
       Do light proceed from the eye to the light
        source or from light source to the eye?
   To answer the question, it’s important to
       Light rays do travel in straight lines
       Light rays do not interfere if they cross
       Light rays travel from the light source to the
        eye but the physics are invariant under path
Ray tracing

   Image order rendering
       Start from pixel, what do you see through
        this pixel
   Allows to add more realism to the
    rendered scene by allowing effects such
       Shadows
       Transparency
       Reflections
How ray-tracing works

   Looks through each pixel (e.g. 640 x 480)
   Determines what eye sees through pixel
       Trace light ray: eye -> pixel (image plane) ->
       If a ray intersect any scene object?
           Yes, render pixel using object color
           No, it uses the background color
       Automatically solves hidden surface removal
Ray misses all objects
Ray hits an object
Ray hits an object
   Ray hits object: Check if hit point is in shadow,
    build secondary ray towards light sources
Ray hits an object
   If shadow ray hits another object before light
    source: first intersection point is in shadow of
    the second object. Otherwise, collect light
Reflected Ray
   When a ray hits an reflective object, a reflection
    ray is generated which is tested against all of
    the object in the scene
Shadows and Reflections
   If intersected object is transparent, transmitted
    ray is generated
   Reflected rays can generated other reflected

   Scene with one layer of reflection

   Scene with two layer of reflection
Ray Tree

   Reflective/transmitted rays are
    continually generated until ray
    leaves the scene without hitting any
    object or a preset recursion level
    has been reached
Ray-Object Intersections
   Express ray as equation (origin is eye, pixel
    determines direction)
       R0 = [x0, y0, z0] – origin of ray
       Rd = [xd, yd, zd] – direction of ray
   Define parametric equation of ray
       R (t) = R0 + Rd * t, with t > 0.0
   Express all objects mathematically
   Ray tracing idea:
       Put ray mathematical equation into object equation
       Determine if real solution exists
       Object with smallest hit time is object seen
Ray-Object Intersection

   Dependent on parametric equation of
   Ray-Sphere Intersections
   Ray-Plane Intersections
   Ray-Polygon Intersections
   Ray-Box Intersections
   Ray-Quadric Intersections (cylinders,
    cones, ellipsoids, paraboloids)
Accelerating Ray Tracing
   Ray tracing is very time-consuming
    because of intersection calculations
   Each intersection requires many floating
    float point operations
   Solutions
       Use faster machines
       Use specialized hardware, especially parallel
       Speed up computations by using more
        efficient algorithms
       Reduce the number of ray-object
Reducing Ray-Object Intersections

   We will look at spatial (空间的) data
       Hierarchical (层次) bounding volumes (包围盒)
       Uniform Grids (均匀格)
       Quadtree/Octree (四叉树/八叉树)
       K-d Tree/BSP tree (空间二分树)
   Good spatial data structures could speed
    up ray tracing by 10-100 times
Ray intersection
Ray Intersection
Bounding Volume

   Wrap (包住) things with bound
    volumes that hard to test for
    intersection in things that are easy to
    test. Ray can’t hit the real object
    unless it hits the bounding volume
   Bounding Box and Bounding Sphere
Bounding Volume
Hierarchical Bounding Volume

   HBV (层次包围盒)
       Limitation of bounding volume
           Still need to test ray against every object,
            O(n) time complexity, where n is the
            number of objects
       A natural extension to bounding
        volumes is a HBV

   Given the bounding volumes of the
    objects in the scene, a tree data structure
    of bounding volumes is created with the
    bounding volume of the objects at the
   Each interior node v of HBV corresponds
    to the bounding volumes that completely
    encloses the bounding volumes of all the
    children node of v

   An example
       Several triangles (right)
       HBV (below)

   Works well if you use good
    bounding volume and hierarchy
   Should give O(logn) time
   Can have mulitple classes (box,
    sphere) of bounding volumes
Uniform Grids (均匀网格)

   Data structure: a 3D array of cells that
    tile space
       Each cell lists all primitives which
        intersect with that cell
Uniform Grids

   Intersection testing
       Start tracing at cell where ray begins
       Step from cell to cell, searching for the
        first intersection cell
       At each cell, test for intersection with
        all primitives intersected with that cell
       If there is an intersection, return the
        closest one
Uniform Grids
   Advantage
       Easy to construct and easy to traverse
   Disadvantage
       Uniform grids are a poor choice if the world
        is non-homogeneous (不均匀). Many
        polygons will cluster in a small space
       How many cells to use?
           Too few -> many objects in a cell -> slow
           Too many -> many empty cells to traverse -> slow
            and large storage
       Non-uniform spatial division is better
   QuadTree
       The 2D generalization of binary tree
       Node is a square
       Recursively split square into four equal sub-
       Stop when leaves get “simple enough”
   Octree
       2D generalization of QuadTree
       Node is cube, recursively split into 8 equal sub-
Construction of Octree
   First, enclose the entire scene in a
    minimal axis-aligned box
   Built recursively in a top-down fashion,
    and stops recursion when criterion is
   These criteria include: a maximum
    number of recursion level has been
    reached, or that there is fewer than a
    threshold number of primitives in a node.
BSP tree
Construction of BSP

   Similar to octree, BSP is also
    constructed hierarchically in top-
    down fashion
       A splitting plane (perpendicular to axis
        x or y or z) is selected to subdivide a
        current leaf node into two equal size
BSP tree Examples
BSP tree Examples

   Difference between BSP and KD
       Distinguished by the position of the
        splitting plane: in BSP tree, the plane
        always lies at the mid-point of the current
        node, but the KD tree has arbitrarily
        position of the plane
   Thus, any BSP tree is a KD tree, but
    not vice versa

Shared By: