Useful ector Information S770/870 Spring 07 Ray Tracing Implementation Related material:angel 6e: h.3 Ray-Object intersections Spheres Plane/Polygon Box/Slab/Polyhedron Quadric surfaces Other implicit/explicit surfaces ector calculus is helpful notation in ray tracing dot product (also called inner product) Let U = (u x, u y, u z ) and = (v x, v y, v z ) U U = a x * b x a y * b y a z * b z U = U cos α, where α is angle between U and where U is the length of U U / scalar (orthogonal) projection of U onto U cos α = U / scalar (orthogonal) projection of onto U cos α = U / U α U / U 03/0/7 Ray-Sphere Definitions Ray/Sphere Intersection Equation Implicit representation of a sphere of radius, r, and center = ( x c, y c, z c ): ( x - x c ) ( y - y c ) ( z - z c Parametric representation of a ray R(t) = t for t > 0 where = ( x o, y o, z o ) is the ray origin and = ( x v, y v, z v ) is the ray direction as a unit vector This is really 3 equations: x(t) = x o t x v y(t) = y o t y v z(t) = z o t z v Start with sphere equation, with center at origin x y z = r Replace x, y, and z with x(t), y(t), z(t) of ray (x o tx v ) (y o ty v ) (z o tz v Expand the square terms (x v t x v x 0 t x 0 ) (y v t y v y 0 t y 0 ) (z v t z v z 0 t z 0 ) = r ollect like terms (powers of t): at bt c = 0 a = x v y v z v = ( is a unit vector) This simplifies calculations! b = (x o )x v (y o )y v (z o )z v c = (x o ) (y o ) (z o ) r 3 4
Solve quadratic equation Get 0, or solutions: b -4c <0 : 0 intersections t = b b -4c =0 : intersection, ray is tangent to sphere; ignore b 4c t < 0 and t < 0 : on wrong side of ray, ignore t < 0 and t >0 : use t t >0 and t >0 : use t ; it has to be smaller Remember the quadratic equation?, t = b b 4c a= in this case Ray/Arbitrary Sphere Intersection Start with sphere equation for center at (x c, y c, z c ) (x ) (y ) (z Replace x, y, and z with x(t), y(t), z(t) of ray ((x o tx v ) ) ((y o ty v ) ) ((z o tz v ) Simplify terms by collecting constant expressions (tx v x o ) (ty v y o ) (tz v z o (tx v x d ) (ty v y d ) (tz v z d t where x d = x o, y d = y o and z d = z o b - 4c <= 0 t t t t t < 0, t > 0 t < 0, t < 0 t t > 0, t > 0 5 This is the same equation as sphere at origin with (x d, y d, z d ) replacing (x o, y o, z o ) 6 Efficiency Improvement Efficiency Improvement (cont) an we find miss cases more efficiently First find if is inside or outside the sphere if distance from to < r, then is inside sphere distance(, ) = ( ) ( ) But, no need to compute the square root is inside sphere if ( ) ( ) r < 0 But, ( ) ( ) = (x c x o ) (y c y o ) (z c z o ) r = c So is inside sphere if c < 0, for the c term of the quadratic equation. If is inside sphere, we know we have exactly intersection and it is t - If outside, find t c = closest point on ray to Project (- ) onto : (- ) = t c (- ) = -b/ the b from quadratic equation! t=0 If t c > 0, find d, distance from to ray L = t c d where L = R 0 = ( ) ( ) so, d = L t c = ( ) ( ) t c and d r > 0 means ray misses sphere or ( ) ( ) t c r > 0 means miss But c = ( ) ( ) r and t c = b ( ) for b and c from quadratic equation : so condition is c ( b ) > 0 c 4 b > 0 4c b > 0 b 4c 0 t c t c L t=0 - d 7 8
Efficiency Improvement (cont) Ray/Plane Intersections How does this help?. alculate c = (- ) (- )-r. alculate t c = (- ) 3. If c >0 and t c < 0, done; ray is outside and points away 4. alculate h = t c c = 4 b c 5. if h < 0, done, no int. 6. alculate h=sqrt(h ) 7. if c >0, outside: t = t c - h else if c <0, inside: t = t c h 8. Use t to get x,y,z If =(0,0,0), even simpler t c h L h d Ray: R(t) = t for t > 0 Plane (implicit) ax by cz d = 0 where a b c = ote: = ( a, b, c ) is unit normal to plane Substitute ray equations into plane equation a(x o tx v ) b(y o ty v ) c(z o tz v ) d = 0 Solve for t t = (ax o by o cz o d) ax v by v cz v = d ( ) - - t 9 0 Ray/Plane Intersections Steps Ray/Polygon Intersection. alculate v d =(-), == v d == 0 means ray is parallel to plane; no int. v d < 0 normal in opposite direction from ray; if have -sided polygons and back-face culling, done: polygon not visible!. alculate v o = (- )d 3. alculate t = v o / v d t < 0 : intersection behind ray, no int. 4. alculate (x,y,z) from ray equations. - - t. Find Ray/Plane intersection. Project polygon and point along one of major axes onto D plane. 3. Do D point-in-polygon test to determine if intersection point is inside polygon project
Point-in-Polygon Test Ray/Box Intersection. Project 3D polygon along one coordinate axis to xy or yz or xz plane. Which is best? want to avoid a thin projected polygon to maximize projected area, project along axis with maximum coefficient in plane equation: see D analogy on the right. D point-in-polygon draw horiz (or vert) line from projected intersection point to infinity; y=y int intersect y=y int with each edge (pretty simple!) count intersections with edges: odd inside be careful with vertex intersections D analogy x y - 4 = 0 x 3y - 6 = 0 Transform ray back to object space where box is oriented along major axes to origin and even to unit cube Intersect transformed ray with planes: x=0, x=, y=0, y=, z=0, z= Line/plane intersections are trivial; intersection of ray with x=0: x(t) = x o tx v // ray equation for x t 0 = (0 - x o ) / x v = -x o / x v // intersection with x=0 The t for x = is just one more addition t = ( - x o ) / x v = /x v - x o / x v = /x v t 0 3 4 Ray/Slab Intersection onvex Polyhedra Slabs are pairs of parallel planes; they are good for creating tighter bounding volumes Just two sides of a box and once get t for one slab, the t for parallel one is just one addition A polyhedron is a 3D object whose faces are planar Polyhedra can be defined by: the polygons that make up their faces the space defined by the intersection of the negative sides of all the planes that define the faces Polygon representation natural for polygon-based rendering hard to validate correctness: are there gaps between polygons? do any polygons intersect? etc. Planar representation relatively easy correctness computation hard to render (except with ray tracing) 5 6
Ray/Polyhedron Intersection Given a convex polyhedron defined by intersection of half-planes, find intersection of a ray with polyhedron: for each plane, H i P i = intersection of ray with H i if P i on - side of all other planes if is on side of H i return P i return null P 0 doesn t exist P is on side of other planes and R 0, is on side of H Good! P is on side of H, P 3 is on side of H 4, P 4 is on side of all, but R 0 is on side of H 4 H H 3 D analogy P P H 0 H H 4 P 4 P 3 7 Ray/Polyhedron Intersection - Basic algorithm has some weaknesses: If ray starts inside the polyhedron, it fails/ Easily fixed by first testing R0 against all planes; if it is on the side of all planes and changing innermost if test to: if ( is on side of H i ) or ( in polyhedron) return P i eeds extra code to recognize intersections with/near vertices, where the inside test for the other planes could be wrong due to round-off error Only works for convex polyhedra ote that the correctness of the polyhedron specification is not a problem for the rendering since a polyhedron with null volume just won t ever appear 8 Ray v. Quadric Surface Any quadric surface can be represented implicitly by " a b c d% " x% $ ' $ ' e f g h [ x y z ] $ ' $ y ' = 0 $ i j k l' $ z' $ ' $ ' # m n o p& # & Ray v. Generic Implicit Surfaces Ray intersections with implicit surfaces of degree > or non-polynomial surfaces are normally solved with numerical techniques Substitute the parametric equations for the ray into implicit equation and solve for t using the quadratic formula Just like a sphere, but much more algebra! 9 0
Ray vs. Explicit Surfaces SG Surfaces Suppose the surface is defined parametrically (in parameters): S(u,v) = [x(u,v), y(u,v), z(u,v)] an make some progress by representing the ray as the intersection of any two planes that share it: a x b y c z d = 0 a x b y c z d = 0 Intersection of planes an surface yields curves in D: (u,v) = a x(u,v) b y(u,v) c z(u,v) d = 0 (u,v) = a x(u,v) b y(u,v) c z(u,v) d = 0 Use numerical techniques in D to solve intersection in (u,v) space v u onstructive Solid Geometry Define objects as union, intersection or set difference between basic shapes, like parallelopiped and cylinders = Effective representation for analysis validity of representation, many physical properties are easily computed, and much more ot very efficient for rendering purposes, except with ray tracing Ray v. SG Other Kinds of Surfaces Given a SG tree Do a postfix tree walk: for each leaf compute ray/solid intersection in t // each primitive has 0 or ints t t for each operator combine ray classification of its children and simplify by merging B D E left child right child L R L R L R Sweep surfaces specialized algorithms exist for some specific kinds of swept objects and paths. Procedural objects parametric surfaces can be treated as procedural using subdivision techniques Fractal surfaces some can be handled by subdivision techniques but, bounding volume test can be difficult Particle systems and other true procedural surfaces have very specialized intersections olume data based on voxels: not very hard olume data based on octrees: also not hard 3 4