Recursive Ray Tracing

by William Shoaff with lots of help


Recursive Ray Tracing

\put( 60,795){\circle{14}}
...put(190,590){\twlrm Image Plane}
\put(40,810){\twlrm Light Source}

...645){{\twlrm Refracted Ray}}
\put(-15,645){{\twlrm Reflected Ray}}

The Intensity Equation for Ray Tracing

Pseudocode for a Simple Ray Tracer

`;=3000 raytrace() $\{$    Select the eye position $(e_x,\, e_y,\,e_z)$;     Select window $(x_{\min},\,y_{\min}),\,(x_{\max},\,y_{\max})$;     for $(y = y_{\min};\, y \leq y_{\max};\, y\mbox{\rm ++})\: \{$         for $(x = x_{\min};\, x \leq x_{\max};\, x\mbox{\rm ++})\: \{$             eyeray = $\langle e_x - x - 1/2,\, e_y - y - 1/2,\,e_z\rangle$;             pixel = trace $(0,\, 1,\, \mbox{\it eyeray})$;         $\}$    $\}$$\}$

`;=3000 color trace $(\mbox{\it level},\, \mbox{\it weight},\, \mbox{\it ray})$ int level; float weight; Ray ray; $\{$    if $(\mbox{\it intersect}(\mbox{\it ray},\,\mbox{\it model},\,\mbox{\it hit}))$ $\:\{$        obj = hit.object;         $\vec{N} =$ obj.normal;         P= raypoint(ray, hit.t);         return $(\mbox{\it shade}(\mbox{\it level},\mbox{\it weight},\mbox{\it ray},\mbox{\it obj}, \vec{N}, P))$;     $\}$    else return $(\mbox{\it background})$; $\}$

`=3000 color shade(level, weight, ray, obj, $\vec{N}$, P) begin     color = obj.ka*Ia;     for each light do         $\vec{L} =$ unit vector from P to light;         $\vec{V} =$ unit vector from P to viewer; $\vec{H}=\frac{\vec{L}+\vec{V}}{\Vert\vec{L}+\vec{V}\Vert}$;         if $((\vec{N}\cdot\vec{L})>0)$ and (ray is not in shadow)             color += $I_{p}(\vec{N}\cdot\vec{L})*\mbox{\it obj}.k_{d}I_{p}(\vec{N}\cdot\vec{H})^{\mbox{\it obj}.\alpha}*\mbox{\it obj}.k_{s}$;     endfor;     if (level+1 < maxlevel$\,)$ then         if (obj.krg*weight > minweight) then             $\vec{R} =$ ReflectedDirection $(\mbox{ray}, \vec{N})$;             rcolor = trace(level+1, weigth*obj.krg, $\vec{R})$;             color += obj.krg*rcolor; endif;         if (obj.ktg*weigth > minweight) then             $\vec{T} =$ RefractedDirection $(\mbox{\it ray},\,\vec{N})$;             tcolor = trace(level+1, weigth*obj.ktg, $\vec{T})$;             color += obj.ktg*tcolor; endif;     endif;     return color; end color

Ray Casting For Intersections

Ray/Sphere Intersection - Algebraic Solution

Ray/Sphere Intersection - Geometric Solution

`=3000          $t_{ca}=\vec{R_oC}\cdot \vec{R}_d$;          $t^2_{hc} = r^2-((\vec{R_oC}\cdot\vec{R_oC})-t^2_{ca})$;         if (t2hc < 0) then no intersection;         else begin              $t=\sqrt{t^2_{hc}}$;              $I=R_o+(t_{ca}-t)\vec{R}_d$;         end

Computing Ray Intersection With A Polygon

Polygon Inside/Outside Testing

\put(120,780){\line( 4,-3){120}}
...V_0V_1}$ }
\put(125,700){$P$ }
\put(150,700){$\beta\vec{V_0V_2}$ }

`;=3000 Polygon-Inside-Outside-Test $\{$    for i=0 to N         project $(X_i,\,Y_i,\,Z_i)$ onto dominant plane             creating a list of vertices $(U_i,\,V_i)$;     translate intersection point to the origin;         call the new points $(U'_i,\, V'_i)$;     Number-Cross =0;     if V'0 < 0 then Sign-Hold = -1;     else Sign-Hold =1;     for a = 0 to N-1          $b = (a+1) \bmod N$;         if V'b < 0 then Next-Sign-Hold =-1;         else Next-Sign-Hold =1;         if Sign-Hold $\neq$ Next-Sign-Hold then             if U'a >0 and U'b >0 then                 Number-Cross += 1;             if U'a >0 or U'b > 0 then                 compute edge intersection with U' axis;                 if U'a - V'a(U'b-U'a)/(V'b-V'a) > 0 then                     Number-Cross += 1;         Sign-Hold = Next-Sign-Hold;     if Number-Cross is odd then         intersection is inside the polygon; $\}$

Ray-Box Intersection

\put(120,760){\line( 0,-1){180}}
...,565){$x_{b2}$ }
\put( 55,620){$y_{b1}$ }
\put( 55,700){$y_{b2}$ }

Shadows in Ray Tracing

\put(140,660){\vector( 0, 1){140}}...
...ut(330,560){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm 3}}}

Efficient Considerations in Ray Tracing

Adaptive Depth Control

Bounding Volumes in Ray Tracing

Hierarchical Bounding Volumes

`;=3000 bounding-volume-hierarchy-intersect(ray, node) Ray ray; Node node; $\{$    if (node is a leaf) then         intersect(ray, node.object);     else if $(\mbox{\it intersect(ray, boundingvolume(node))})$         for (each child of node)             bounding-volume-hierarchy-intersect(ray, child); $\}$

Spatial Subdivision for Ray Tracing

Auxiliary Data Structures for Spatial Subdivision


\put(100,740){\line( 1, 0){ 80}}\...
...t(165,545){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm 47}}}

`;=3000 findnode $(x,\,y,\,z)$$\{$    node =1;     while ( node-subdivided = TRUE ) do         if $(x > \mbox{\it node-center-x})$             if $(y > \mbox{\it node-center-y})$                 if $(z > \mbox{\it node-center-z})$                     node $= 10*\mbox{\it node}+6$;                 else node $= 10*\mbox{\it node}+2$;             else                 if $(z > \mbox{\it node-center-z})$                     node $= 10*\mbox{\it node}+8$;                 else node $= 10*\mbox{\it node}+4$;         else             if $(y > \mbox{\it node-center-y})$                 if $(z > \mbox{\it node-center-z})$                     node $= 10*\mbox{\it node}+5$;                 else node $= 10*\mbox{\it node}+1$;             else                 if $(z > \mbox{\it node-center-z})$                     node $= 10*\mbox{\it node}+7$;                 else node $= 10*\mbox{\it node}+3$;     end while; return node $\}$

`;=3000 octree-intersect (ray $\vec{R})$ $\{$     $Q=\vec{R}.\mbox{\it origin}$;     do begin /* walk through the voxels         voxel = findnode(Q);         for each object in the voxel's list do             intersect(ray, object);         if no intersection has been found then             Q= point in next voxel pierced by ray;     until intersection found or Q outside world $\}$

\put( 80,660){\framebox (200,120){...
...215,670){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm $C$ }}}

Uniform Spatial Subdivision

Extending the DDA Algorithm

\put( 60,700){\framebox (80,80){}}...
...,642){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm pierced}}}

`;=3000 2d-dda-extended $(\mbox{\it point}\:R_o,\,\mbox{\it ray}\:R_d)$$\{$     $x = \lfloor x_o\rfloor$;      $y = \lfloor y_o\rfloor$;      s1 = yd/xd;      e = yo+s1*(x-xo);     do $\:\{$        pierced$(x,\,y)$;         e= e+s1;         if $(e \geq y+1.0)$ then             y=y+1;             if (e > y) pierced$(x,\,y)$;         x = x+1.0;     $\}$ until ray leaves scene; $\}$

3D-DDA Algorithm for Uniform Octree Traversal

\put( 95,695){\line( 3, 1){150}}
...ebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm $R_o+t\vec{R}_d$ }}}

Binary Space Partitioning

\put(140,780){\line(-1,-1){ 60}}
...585){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm $x<512$ }}}

Summary of Space Partitioning Methods

\put( 80,800){\circle{30}}
\put( 4...
...ebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm (Bipartite Graph)}}}

Generalized Rays

Cone Tracing

Beam Tracing

\put( 80,780){\line( 2,-3){ 80}}
...akebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm Clipped polygon}}}

Ray Coherence

\put( 72,768){\circle{30}}
...\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm Cached object}}}

Distributed Ray Tracing

\put( 80,660){\line( 1, 0){160}}
...760){$\vec{N}$ }
\put(175,720){$\phi_i$ }
\put(130,720){$\phi_r$ }

\put( 60,620){\framebox (160,160){...
...{\raisebox{0pt}[0pt][0pt]{\twlrm Example prototype time pattern}}}


Summary of Distributed Ray Tracing

Adaptive Supersampling

Backward Ray Tracing

William Shoaff