Illumination Models III: Ray Tracing (View Dependent Global Illumination) The University of Texas at Austin 1
Basic Definitions Ray Tracing/Casting: Setting: eyepoint, virtual screen (an array of virtual pixels, and scene are organized in convenient coordinate frames (e.g., all in view or world frames) Ray: a half line determined by the eyepoint and a point associated with a chosen pixel Interpretations: Ray is the path of photons that successfully reach the eye (we simulate selected photon transport through the scene) Ray is a sampling probe that gathers color/visibility information screen ray scene eye The University of Texas at Austin 2
Ray Tracing: Recursive Eye-screen ray is the primary ray Backward tracking of photons that could have arrived along primary Intersect with objects Determine nearest object Generate secondary rays to light sources in reflection-associated directions in refraction-associated directions Continue recursively for each secondary ray Terminate after suitably many levels Accumulate suitably averaged information for primary ray Deposit information in pixel See raytrace psuedo-code document http://www.cs.utexas.edu/ bajaj/graphics10/cs354/raytrace1.pdf The University of Texas at Austin 3
Ray Casting: Nonrecursive As above for ray tracing, but stop before generating secondary rays Apply illumination model at nearest object intersection with no regard to light occlusion Ray becomes a sampling probe that just gathers information on visibility color The University of Texas at Austin 4
Intersection Computations General Issues: Ray: express in parametric form E + t(p E) where E is the eyepoint and P is the pixel point Scene Object: direct implicit form express as f(q) = 0 when Q is a surface point, where f is a given formula intersection computation is an equation to solve: find t such that f(e + t(p E)) = 0 Scene Object: procedural implicit form f is not a given formula f is only defined procedurally A(f(E + t(p E)) = 0) yields t, where A is a root finding method (secant, Newton, bisection, etc.) The University of Texas at Austin 5
Quadric Surfaces: Surface given by Ray given by Ax 2 + Bxy + Cxy + Dy 2 + Eyz + Fz 2 + Gx + Hy + Jz + K = 0 x = x E + t(x P x E ) y = y E + t(y P y E ) z = z E + t(z P z E ) Substitute ray x,y,z into surface formula quadratic equation results for t organize expression terms for numerical accuracy; i.e., to avoid cancellation combinations of numbers with widely different magnitudes The University of Texas at Austin 6
Polygons: The plane of the polygon should be known (A,B,C,0) is the normal vector pick three successive vertices Ax + By + Cz + D = 0 v i 1 = (x i 1,y i 1,z i 1 ) v i = (x i,y i,z i ) v i+1 = (x i+1,y i+1,z i+1 ) should subtend a reasonable angle (bounded away from 0 or 180 degrees) normal vector is the cross product v i 1 v i v i+1 v i D = (Ax + By + Cz) for any vertex (x,y,z,1) of the polygon The University of Texas at Austin 7
Substitute ray x,y,z into surface formula linear equation results for t Solution provides planar point (x,y,z) is this inside or outside the polygon? The University of Texas at Austin 8
Clipping and Intersection Clipping: Remove points, line segments, polygons outside a region of interest. Need to discard everything that s outside of our window. Point clipping: Remove points outside window. A point is either entirely inside the region or not. Line clipping: Remove portion of line segment outside window. Line segments can straddle the region boundary. The Liang-Barsky algorithm efficiently clips line segments against a halfspace. Halfspaces can be combined to bound a convex region. Use outcodes to organize combination of halfspaces. Can use some of the ideas in Liang-Barsky to clip points. The University of Texas at Austin 9
Polygon clipping: Remove portion of polygon outside window. Polygons can straddle the region boundary. Concave polygons can be broken into convex. Sutherland-Hodgemen algorithm also deals with all cases efficiently. Built upon efficient line segment clipping. The University of Texas at Austin 10
Parametric representation of line: P(t) = (1 t)p 0 + tp 1 or equivalently: P(t) = P 0 + t(p 1 P 0 ) P 0 and P 1 are non-coincident points. For t R, P(t) defines an infinite line. For t [0,1],P(t) defines a line segment from P 0 to P 1. Good for generating points on a line. Not so good for testing if a given point is on a line. The University of Texas at Austin 11
Implicit representation of line: l(q) = (Q P) n P is a point on the line. n is a vector perpendicular to the line. l(q) gives us the signed distance from any point Q to the line. The sign of l(q) tells us if Q is on the left or right of the line, relative to the direction of n. If l(q) is zero, then Q is on the line. Use same form for the implicit representation of a halfspace. n P Q The University of Texas at Austin 12
Clipping a point to a halfspace: Represent a window edge implicitly... Use the implicit form of a line to classify a point Q. Must choose a convention for the normal: point to the inside. Check the sign of l(q): If l(q) > 0, the Q is inside. Otherwise clip (discard) Q; it is on, or outside. The University of Texas at Austin 13
Clipping a line segment to a halfspace: There are three cases: The line segment is entirely inside. The line segment is entirely outside. The line segment is partially inside and partially outside. n P The University of Texas at Austin 14
Do the easy stuff first: We can devise easy (and fast!) tests for the first two cases: (P 0 P) n < 0 AND (P 1 P) n < 0 = Outside (P 0 P) n > 0 AND (P 1 P) n > 0 = Inside We will also need to decide whether on the boundary is inside or outside. Trivial tests are important in computer graphics: Particularly if the trivial case is the most common one. Particularly if we can reuse the computation for the non-trivial case. The University of Texas at Austin 15
Do the hard stuff only if we have to: If line segment partially inside and partially outside, need to clip it: Represent the segment from P 0 to P 1 in parametric form: P(t) = (1 t)p 0 + tp 1 = P 0 + t(p 1 P 0 ) When t = 0,P(t) = P 0. When t = 1,P(t) = P 1. We now have the following: n 1P P P(t)=(1-t)P 0 +tp 1 P 0 The University of Texas at Austin 16
We want t such that P(t) is on l: Solving for t gives us (P(t) P) n = (P 0 + t(p 1 P 0 ) P) n = (P 0 P) n + t(p 1 P 0 ) n = 0 t = (P 0 P) n (P 0 P 1 ) n NOTE: The values we use for our simple test can be used to compute t: t = (P 0 P) n (P 0 P) n (P 1 P) n The University of Texas at Austin 17
Clipping a line segment to a window: Just clip to each of four halfspaces. Pseudo-code: given: P, n defining a window edge for each edge (A,B) = (P0,P1) weca = (A-P). n wecb = (B-P). n if ( weca < 0 AND wecb < 0 ) then reject if ( weca >= 0 AND wecb >= 0 ) then next t = weca / (weca - wecb) if (weca < 0 ) then A = A + t (B-A) else B = B + t (B-A) endif endfor NOTE: Liang-Barsky Algorithm generalizes to 3D The University of Texas at Austin 18
Line-clip Algorithm generalizes to 3D: Half-space now lies on one side of a plane. The implicit formula for a plane in 3D is the same as that for a line in 2D. The parametric formula for the line to be clipped is unchanged. The University of Texas at Austin 19
Reading Assignment and News Please review the appropriate sections related to this lecture in chapter 11, and associated exercises, of the recommended text. (Recommended Text: Interactive Computer Graphics, by Edward Angel, Dave Shreiner, 6th edition, Addison-Wesley) Please track Blackboard for the most recent Announcements and Project postings related to this course. (http://www.cs.utexas.edu/users/bajaj/graphics2012/cs354/) The University of Texas at Austin 20