CS380: Introduction to Computer Graphics Ray tracing Chapter 20 Min H. Kim KAIST School of Computing Modeling SUMMARY 2 1
Types of coordinate function Explicit function: Line example: Implicit function: Circle example: y = f (x) y = mx +b f (x, y)=0 (x a) 2 +( y b) 2 r 2 =0 Explicit parametric function: x = x(t) w(t) ; y = y(t) w(t) Line example: x = a 0 +a 1 t; y = b 0 +b 1 t Circle example: x = a+rcos(t); y = b+rsin(t) 3 Subdivision surfaces First, let f be a face in M i surrounded by the vertices v j (and let m f be the number of such vertices). We set the geometry of each new face-vertex v f in M i+1 to be (m f = 4) v f = 1 m f The centroid of the vertices in M i defining that face. j v j 4 2
Subdivision surfaces Next, let e be an edge in M i connecting the vertices v 1 and v 2, and separating the faces f 1 and f 2. We set the geometry of the new edge-vertex in M i+1 to be: v e = 1 4 (v 1 +v 2 +v f 1 +v f2 ) 5 Subdivision surfaces Finally let v be a vertex in M i connected to the n v vertcies v j (n v =4) and surrounded by the n v faces f j. Then, we set the geometry of the new vertexvertex in M i+1 to be: v v = n 2 v v + 1 2 v n v n j + 1 2 v v v v j n v For an ordinary vertex, with valence n v =4, this becomes: v j + v f 16 j 16 j j v v = 1 2 v + 1 1 j v f j 6 3
Tangent, Normal, Curvature Tangent (time parametric) Normal (length parametric) For T (s) = (x'(s), y'(s)), normal is defined as! n(s) = ( y'(s), x'(s)) Curvature (length parametric): k(s): signed curvature,! n(s): normal T (t) = α '(t) α '(t) κ(s) = k(s) dt ds = k(s)! n(s) 7 Chapter 20 RAY TRACING 8 4
Photo vs. Graphics 9 Photo vs. Graphics 10 5
Photo vs. Graphics 11 Photo vs. Graphics (a) (b) (c) Fiero Ardi Al Dean Jorge Jimenez Answers: Graphics Graphics Graphics All of them are purely computer graphics images, created by the latest graphics techniques 12 6
Fundamental elements for rendering Geometry Material model Light Rendering Virtual photography 13 Fundamental elements for rendering Geometry 3D scanning, 3D modeling Color, Texturing, BRDFs Material model Light Color, spectrum Rendering Virtual photography Global illumination 14 7
Cornell Box rendering Meyer, Rushmeier, Cohen, Torrance and Greenberg, ACM TOG, 1986 15 Ray tracing result 16 8
Ray tracing Different approach to organizing the rendering process It is pixel and ray based, instead of triangle based. Historically, it has not been hardware accelerated, and is used for offline rendering. It is very flexible and so various optical effects can be put in very easily. It is at the heart of photorealistic methods of light simulation. We will cover it only briefly. 17 Ray tracing pipeline Courtesy of Hendrik Lensch 18 9
Ray tracing pipeline Ray-Generation Ray-Traversal Intersection Shading Framebuffer Courtesy of Hendrik Lensch 19 Ray tracing pipeline Ray-Generation Ray-Traversal Intersection Shading Framebuffer Courtesy of Hendrik Lensch 20 10
Ray tracing pipeline Ray-Generation Ray-Traversal Intersection Shading Framebuffer Courtesy of Hendrik Lensch 21 Ray tracing pipeline Space subdivision Ray-Generation Ray-Traversal Intersection Shading Framebuffer Courtesy of Hendrik Lensch 22 11
Ray tracing pipeline Space subdivision Ray-Generation Ray-Traversal Intersection Shading Framebuffer Courtesy of Hendrik Lensch 23 Ray tracing pipeline Ray-Generation Ray-Traversal Intersection Shading Framebuffer Courtesy of Hendrik Lensch 24 12
Ray tracing pipeline Ray cast only Ray-Generation Ray-Traversal Intersection Shading Framebuffer Courtesy of Hendrik Lensch 25 Ray tracing pipeline Ray-Generation Ray-Traversal Intersection Shading Framebuffer Courtesy of Hendrik Lensch 26 13
Ray tracing pipeline Ray-Generation Ray-Traversal Intersection Shading Framebuffer Courtesy of Hendrik Lensch 27 Ray tracing pipeline Ray cast with shadow Ray-Generation Ray-Traversal Intersection Shading Framebuffer Courtesy of Hendrik Lensch 28 14
Ray tracing pipeline Ray-Generation Ray-Traversal Intersection Shading Framebuffer Courtesy of Hendrik Lensch 29 Ray tracing pipeline Color mixture Ray-Generation Ray-Traversal Intersection Shading Framebuffer Courtesy of Hendrik Lensch 30 15
Ray tracing pipeline Ray-Generation Ray-Traversal Intersection Shading Framebuffer Courtesy of Hendrik Lensch 31 Ray tracing 32 16
Rasterization vs. ray-casting Rasterization is organized therefore: initialize z-buffer for all triangles for all pixels covered by the triangle compute color and z if z is closer than what is already in the z-buffer update the color and z of the pixel In basic ray casting, we reverse the loop orders to obtain: for all pixels on the screen for all objects seen in this pixel if this is the closest object seen at the pixel compute color and z set the color of the pixel 33 Rasterization The rasterization algorithm has the nice property that each triangle in the scene is touched only once, and in a predictable order. Good for memory usage Use in Pixar Renderman Setup is reduced over a triangle 34 17
Ray-casting Shading is automatically deferred, until after visibility is done. We can maintain depth order of fragments with minimal memory. Letting us easily model non-refractive transparency Letting us easily model constructive solid geometry (CSG). Using root finding, we can directly intersect rays with smooth geometries and do not need to triangulate. 35 Ray-casting Easy to use ray infrastructure for tons of generalizations Shadows reflection Note: many of these things can be done to varying degrees in a rasterize, but with some extra effort expended. Also note that with programmable GPUs shading is the bigger cost, not the rasterization, and that the GPUs can be used to do ray calculations as well. 36 18
RAY CASTING 37 Intersection The main computation needed in ray tracing is computing the intersection of a geometric ray ( p, d) with an object in the scene. Here p (the eye frame origin, COP) is the start of the ray, which goes off in the direction d d p 38 19
Ray-plane Q P R 39 Courtesy of Doug A. Bowman Implicit surface (a plane) P = (1, 1, 1), Q = (1, 2, 0), R = (-1, 2, 1). We seek the coefficients of an equation Ax + By + Cz = D, where P, Q and R satisfy the equations, thus: A + B + C = D A + 2A + 0C = D -A + 2B + C = D Subtracting the first equation from the second and then adding the first equation to the third, we eliminate a to get B - C = 0 4B + C = 2D Adding the equations gives 5B = 2D, or B = (2/5)D, then solving for C = B = (2/5)D and then A = D - B - C = (1/5)D. So the equation (with a nonzero constant left in to choose) is D(1/5)x + D(2/5)y + D(2/5)z = D, so one choice of constant gives x + 2y + 2z = 5 or another choice would be (1/5)x + (2/5)y + (2/5)z = 1 Given the coordinates of P, Q, R, there is a formula for the coefficients of the plane that uses determinants or cross product. 40 20
Ray-plane Plane described by the equation We start by representing every point along the ray using a single parameter λ Plugging this into the plane equation, we get x y z = p x p y p z Eye origin + λ d x d y d z Pixel ray vector 0 = A(p x + λd x ) + B(p y + λd y ) + C(p z + λd z ) + D = λ(ad x + Bd y + Cd z ) + (Ap x + Bp y + Cp z + D) Ax +By +Cz +D =0 d p 41 Ray-plane And we see that λ = (Ap x + Bp y + Cp z + D) (Ad x + Bd y + Cd z ) Given a triangle coordinate (P,Q,R), we can determine A, B, C, D per triangle. λ tells us where along the ray the intersection point is Negative valued λ are backwards along the ray. Comparisons between λ values can be used to determine which, among a set of planes, is the first one intersected along a ray. ABCD per triangle, d xyz per pixel, p xyz per camera 42 21
Ray-plane λ<0 λ>0 Min Courtesy H. Kim (KAIST) of Doug A. Foundations Bowman of 3D Computer Graphics, S. Gortler, MIT Press, 2012 43 Ray-triangle (Step1) we compute the A, B, C, D of the plane supporting the triangle, and compute the rayplane intersection of (x, y, z) with the ray (d x, d y, d z ) as above. (Step2) we need a test to determine if the intersection point is inside or outside of the triangle. We can build such a test using the counter clockwise calculation 44 22
Ray-triangle Suppose we wish to test if a point outside of a triangle Δ( p 1 p 2 p 3 ) in 2D Consider the three sub triangle and Δ( q p 2 p 3 ) is inside or When q is inside of Δ( p 1 p 2 p 3 ), then all three sub-triangles will agree on their clockwisedness. When q is outside, then they all disagree. q Δ( p 1 p 2 q),δ( p 1 q p 3 ) 45 RECAP: Math of backface culling Let Δ( p 1 p 2 p 3 ) be the three vertices of the triangle projected down to the (x n, y n,0) plane. Define the vector a = p 3 p 2 and b = p 1 p 2 Next compute the cross! product c = a! b! If the three vertices are counterclockwise in the! plane, then c will be in the +z n direction. 46 23
RECAP: Math of backface culling So the area of the triangle is: Area = 1 a b Taking account of the direction, 2 we could calculate the direction of the cross product of these two vectors. (x 3 n x 2 n )(y 1 n y 2 n ) (y 3 n y 2 n )(x 1 n x 2 n ) If this is negative, the polygon looks backward. 47 Ray-sphere Suppose we have a sphere with radius R and center c modeled as the set of points [x, y,z] t that satisfy the equation Plugging the below into the above: We get (x c x ) 2 + (y c y ) 2 + (z c z ) 2 r 2 = 0 x y z = p x p y p z + λ 0 = (p x + λd x c x ) 2 + (p y + λd y c y ) 2 + (p z + λd z c z ) 2 r 2 0 = (d x 2 + d y 2 + d z 2 )λ 2 + (2d x (p x c x ) + 2d y (p y c y ) + 2d z (p z c z ))λ + (p x c x ) 2 + (p y c y ) 2 + (p z c z ) 2 r 2 d x d y d z 48 24
Ray-sphere We can then use the quadratic formula to find the real roots λ of this equation. If there are two real roots, these represent two intersections, as the ray enters and exits the sphere. If there is one (doubled) real root, then the intersection is tangential. If there are no real roots, then the ray misses the sphere. As above, any of these intersections may be backwards along the ray. At the intersection, the normal of the sphere at [x, y,z] t is in the direction [x c x, y c y,z c z ] t This fact may be useful for shading calculations. 49 Ray-sphere intersection λ>0 λ<0 Min Courtesy H. Kim (KAIST) of Doug A. Foundations Bowman of 3D Computer Graphics, S. Gortler, MIT Press, 2012 50 25
RAY TRACING 51 Secondary rays To determine if a scene point is in shadow, one follows a shadow ray from the observed point towards the light to see if there is any occluding geometry. Another easy calculation that can be done is mirror reflection (and similarly refraction). In this case, one calculates the bounce direction bounce ray off in that direction: B( w) = 2( w n) n w 52 26
Reflected ray B( w) = 2( w n) n w Min Courtesy H. Kim (KAIST) of Doug A. Foundations Bowman of 3D Computer Graphics, S. Gortler, MIT Press, 2012 53 Shadow ray Min Courtesy H. Kim (KAIST) of Doug A. Foundations Bowman of 3D Computer Graphics, S. Gortler, MIT Press, 2012 54 27
Refracted ray Min Courtesy H. Kim (KAIST) of Doug A. Foundations Bowman of 3D Computer Graphics, S. Gortler, MIT Press, 2012 55 Scene Min Courtesy H. Kim (KAIST) of Doug A. Foundations Bowman of 3D Computer Graphics, S. Gortler, MIT Press, 2012 56 28
Three sources of light Min Courtesy H. Kim (KAIST) of Doug A. Foundations Bowman of 3D Computer Graphics, S. Gortler, MIT Press, 2012 57 Directly from light source Min Courtesy H. Kim (KAIST) of Doug A. Foundations Bowman of 3D Computer Graphics, S. Gortler, MIT Press, 2012 58 29
Reflection Min Courtesy H. Kim (KAIST) of Doug A. Foundations Bowman of 3D Computer Graphics, S. Gortler, MIT Press, 2012 59 Reflection Min Courtesy H. Kim (KAIST) of Doug A. Foundations Bowman of 3D Computer Graphics, S. Gortler, MIT Press, 2012 60 30
Refraction Min Courtesy H. Kim (KAIST) of Doug A. Foundations Bowman of 3D Computer Graphics, S. Gortler, MIT Press, 2012 61 Three sources of light together Min Courtesy H. Kim (KAIST) of Doug A. Foundations Bowman of 3D Computer Graphics, S. Gortler, MIT Press, 2012 62 31
Ray-tracing algorithm (backwards) Min Courtesy H. Kim (KAIST) of Doug A. Foundations Bowman of 3D Computer Graphics, S. Gortler, MIT Press, 2012 63 Examples The color of the point hit by this ray is then calculated and used to determine the color of the original point on the mirror. This idea can be applied recursively some number of times to simulate multiple mirror. Ray-casting only Ray-casting + shadow Rec. ray-tracing + shadow 64 32
Early rejection When computing the intersection between a ray and the scene, instead of testing every scene object for intersection with the ray, we may use auxiliary data structures to quickly determine that some set of objects is entirely missed by the ray. For example, one can use a simple shape (say a large sphere or box) that encloses some set of objects. Bounding volume hierarchy (BVH) Given a ray, one first calculates if the ray intersects this volume. If it does not, then clearly this ray misses all of the objects in the bounded set, and no more ray intersection tests are needed. This idea can be further developed with hierarchies and spatial data structure. 65 Bounding volume hierarchy (BVH) 66 33
Space subdivision 67 34