Previous | Next --- Slide 38 of 49
Back to Lecture Thumbnails
kayvonf

traceRay is a recursive function that computes the closest intersection of the ray ray with the scene geometry contained within the sub-tree rooted by node. As the ray is traced through the subtree, the struct isect maintains information about the closest intersection found so far. This information consists of the distance along the ray to the closest intersection, and a reference to the scene primitive that yielded this intersection.

traceRay works as follows:

  1. It first determines if the ray intersects the bounding box of node. If it does not intersect the bounding box, or if an intersection does exist, but is strictly farther than an intersection found so far, then he function returns. This "early out" is valid because it is not possible for any geometry inside node to generate a closer intersection.

  2. If the node is an interior node, traceRay then recurses on the nodes for the two children.

  3. If the node is a leaf node, traceRay checks for intersections between the ray and each primitive in the leaf (see call to intersect() function). If an intersection exists and that intersection is the closest scene intersection found so far, this information is stored in isect.

To give you a sense of how traceRay is used to make a picture, the overall basic structure of a ray tracer is given below. Notice how iterations of the outermost loop are independent and can be performed in parallel.

BVHNode root;

for each pixel p in image:  // iterations of this loop are independent
   Ray r = createRay(p)     // r is a ray traveling from the image pixel
                            // through the camera's pinhole aperture
   ClosestIectInfo isect;
   traceRay(r, root, isect);
   if (isect indicates a hit)
      color p according to isect.primitive;