CS 431/636 Advanced Rendering Techniques Dr. David Breen University Crossings 149 Tuesday 6PM 8:50PM Presentation 2 4/7/09 Start Up Any questions from last time? Go over sampling image plane? Or intersection algorithms? Slide Credits Leonard McMillan, Seth Teller, Fredo Durand, Barb Cutler - MIT G. Drew Kessler, Larry Hodges - Georgia Institute of Technology John Hart - University of Illinois Rick Parent - Ohio State University More Geometry & Intersections Ray/Plane Intersection Ray is defined by R(t) = Ro + Rd*t where t 0 Ro = Origin of ray at (xo, yo, zo) Rd = Direction of ray [xd, yd, zd] (unit vector) Plane is defined by [A, B, C, D] Ax + By + Cz + D = 0 for a point in the plane Normal Vector, N = [A, B, C] (unit vector) A2 + B2 + C2 = 1 D = - N • P0 (P0 - point in plane) Ray/Plane (cont.) Substitute the ray equation into the plane equation: A(xo + xdt) + B(yo + ydt) + C(zo +zdt) + D = 0 Solve for t: t = -(Axo + Byo + Czo + D) / (Axd + Byd + Czd) t = -(N • Ro - N • P0 ) / (N • Rd) What Can Happen? N • Rd = 0 N • Rd > 0 Ro Ro t<0 t>0 Ray/Plane Summary Intersection point: (xi, yi, zi) = (xo + xdti, yo + ydti, zo + zdti) 1. 2. 3. 4. Calculate N • Rd and compare it to zero. Calculate ti and compare it to zero. Compute intersection point. Flip normal if N • Rd is positive Ray-Parallelepiped Intersection Axis-aligned From (X1, Y1, Z1) to (X2, Y2, Z2) Ray P(t)=Ro+Rdt y=Y2 y=Y1 Rd Ro x=X1 x=X2 Naïve ray-box Intersection Use 6 plane equations Compute all 6 intersection Check that points are inside box Ax+By+Cz+D ≤ 0 y=Y2 y=Y1 Rd Ro x=X1 x=X2 Factoring out computation Pairs of planes have the same normal Normals have only one non-0 component Do computations one dimension at a time Maintain tnear and tfar (closest and farthest so far) y=Y2 y=Y1 Rd Ro x=X1 x=X2 Test if parallel If Rdx = 0, then ray is parallel If Rox < X1 or Rox > x2 return false y=Y2 y=Y1 Rd Ro x=X1 x=X2 If not parallel Calculate intersection distance t1 and t2 t1 = (X1-Rox)/Rdx t2 = (X2-Rox)/Rdx t2 y=Y2 t1 y=Y1 Rd Ro x=X1 x=X2 Test 1 Maintain tnear and tfar If t1 > t2, swap if t1 > tnear, tnear = t1 if t2 < tfar, tfar = t2 If tnear > tfar, box is missed t1x tnear t2x tfar y=Y2 Rd t2y tfar Ro y=Y1 t1y x=X1 x=X2 Test 2 If tfar < 0, box is behind Rd y=Y2 t1x x=X1 tfar t2x y=Y1 x=X2 Ro Algorithm recap Do for all 3 axes Calculate intersection distance t1 and t2 Maintain tnear and tfar If tnear > tfar, box is missed If tfar < 0, box is behind If box survived tests, return intersection at tnear If tnear is negative, return tfar y=Y2 tnear tfar y=Y1 t1y Rd Ro x=X1 x=X2 Ray/Ellipsoid Intersection Ray/Cylinder Intersection Ellipsoid's surface is defined by the set of points {(xs, ys, zs)} satisfying the equation: (xs/ a)2 + (ys/ b)2 + (zs/ c)2 - 1 = 0 Cylinder's surface is defined by the set of points {(xs, ys, zs)} satisfying the equation: (xs)2 + (ys)2 - r2 = 0 Centers at origin -z0 ≤ zs ≤ z0 Ray/Ellipsoid Intersection Ray/Cylinder Intersection Substitute ray equation into surface equations This is a quadratic equation in t: At2 + Bt + C = 0 Analyze as before Solve for t with quadratic formula Plug t back into ray equation - Done Well,… not exactly Ray/Cylinder Intersection Is intersection point Pi between -Z0 and Z0? If not, Pi is not valid Also need to do intersection test with z = -Z0 , Z0 plane If (Pix)2 + (Piy)2 ≤ r2, you’ve intersected a “cap” Which valid intersection is closer? Superquadrics 2 2 2 1 2 1 x y z 1 a1 a2 a3 2 2 Bezier Patch Patch of order (n, m) can be defined in terms of a set of (n + 1)(m + 1) control points Pi+1,j+1 for integer indices i = 0 to n, j = 0 to m. (u,v) [0,1]2 n p(u,v) i 0 m n m B (u)B i j (v)Pi1, j 1 j 0 n i B (u) u (1 u) ni i n i Tesselate Patches and Superquadrics Tesselate Patches and Superquadrics Utah Teapot Modeled by 32 Bézier Patches Control points available at http://www.holmes3d.net/graphics/teapot SMF Triangle Meshes vertices triangles v v v v v v v v f f f f f f f f f f f f -1 -1 1 -1 -1 1 1 1 -1 -1 1 -1 -1 1 1 1 1 3 4 1 4 2 5 6 8 5 8 7 1 2 6 1 6 5 3 7 8 3 8 4 1 5 7 1 7 3 2 4 8 2 8 6 -1 -1 -1 -1 1 1 1 1 Draw data structure Triangle Meshes (.iv) Transformations & Hierarchical Models Homogeneous Coordinates Add an extra dimension • in 2D, we use 3 x 3 matrices • In 3D, we use 4 x 4 matrices Each point has an extra value, w x' a b c d x y' e f g h y i j k l z m n o p w z' = w' p' = Mp Homogeneous Coordinates Most of the time w = 1, and we can ignore it x' a b c d x y' e f g h y i j k l z 0 0 0 1 1 z' 1 = Translate(c,0,0) y Translate (tx, ty, tz) Why bother with the extra dimension? c Because now translations can be encoded in the matrix! x' y' z' 1 = 1 0 0 0 0 1 0 0 0 tx 0 ty 1 tz 0 1 p' p x y z 1 x Scale(s,s,s) p' y Scale (sx, sy, sz) p Isotropic (uniform) scaling: sx = sy = sz x' y' z' 1 sx 0 = 0 0 0 sy 0 0 q q' x 0 0 sz 0 0 0 0 1 x y z 1 You only have to implement uniform scaling ZRotate() y Rotation p' θ About z axis p x z x' y' z' 1 = cos sin 0 0 -sin cos 0 0 0 0 1 0 0 0 0 1 x y z 1 Rotation 0 0 0 1 0 cos -sin 0 0 sin cos 0 0 0 1 0 About x axis: x' y' z' 1 = About y axis: x' y' z' 1 cos 0 1 0 = -sin 0 0 0 sin 0 cos 0 0 0 0 1 x y z 1 x y z 1 y Rotate(k, ) Rotation θ k About (kx, ky, kz), an z arbitrary unit vector (Rodrigues Formula) kxkx(1-c)+c kykx(1-c)-kzs kxkz(1-c)+kys x' kykx(1-c)+kzs kyky(1-c)+c kykz(1-c)-kxs y' = kzkx(1-c)-kys kzky(1-c)+kxs kzkz(1-c)+c z' 0 0 0 1 where c = cos & s = sin x 0 x 0 y 0 z 1 1 How are transforms combined? Scale then Translate (1,1) (2,2) Scale(2,2) (5,3) Translate(3,1) (3,1) (0,0) (0,0) Use matrix multiplication: p' = T ( S p ) = ((TS) p) TS = 1 0 3 0 1 1 0 0 1 2 0 0 0 2 0 0 0 1 = 2 0 3 0 2 1 0 0 1 Caution: matrix multiplication is NOT commutative! Non-commutative Composition Scale then Translate: p' = T ( S p ) = TS p (1,1) (2,2) Scale(2,2) (5,3) Translate(3,1) (3,1) (0,0) (0,0) Translate then Scale: p' = S ( T p ) = ST p (8,4) (1,1) (0,0) (4,2) Translate(3,1) (3,1) Scale(2,2) (6,2) Non-commutative Composition Scale then Translate: p' = T ( S p ) = TS p TS = 1 0 3 0 1 1 0 0 1 2 0 0 0 2 0 0 0 1 = 2 0 3 0 2 1 0 0 1 Translate then Scale: p' = S ( T p ) = ST p ST = 2 0 0 0 2 0 0 0 1 1 0 3 0 1 1 0 0 1 = 2 0 6 0 2 2 0 0 1 Transformations in Ray Tracing Transformations in Modeling Position objects in a scene Change the shape of objects Create multiple copies of objects Projection for virtual cameras Animations Scene Description Scene Camera Lights Background Materials Objects Simple Scene Description File Camera { center 0 0 10 direction 0 0 -1 up 0 1 0 } Lights { numLights 1 DirectionalLight { direction -0.5 -0.5 -1 color 1 1 1 } } Background { color 0.2 0 0.6 } Materials { numMaterials <n> <MATERIALS> } Group { numObjects <n> <OBJECTS> } Hierarchical Models Logical organization of scene Simple Example with Groups Group { numObjects 3 Group { numObjects 3 Box { <BOX PARAMS> } Box { <BOX PARAMS> } Box { <BOX PARAMS> } } Group { numObjects 2 Group { Box { <BOX PARAMS> } Box { <BOX PARAMS> } Box { <BOX PARAMS> } } Group { Box { <BOX PARAMS> } Sphere { <SPHERE PARAMS> } Sphere { <SPHERE PARAMS> } } } Plane { <PLANE PARAMS> } } Adding Materials Group { numObjects 3 Group { numObjects 3 Box { <BOX PARAMS> } Box { <BOX PARAMS> } Box { <BOX PARAMS> } } Group { numObjects 2 Group { Box { <BOX PARAMS> } Box { <BOX PARAMS> } Box { <BOX PARAMS> } } Group { Box { <BOX PARAMS> } Sphere { <SPHERE PARAMS> } Sphere { <SPHERE PARAMS> } } } Plane { <PLANE PARAMS> } } Adding Transformations Using Transformations Position the logical groupings of objects within the scene Transformation in group Directed Acyclic Graph is more efficient and useful Processing Model Transformattions Goal Get everything into world coordinates Traverse graph/tree in depth-first order Concatenate transformations Can store intermediate transformations Apply/associate final transformation to primitive at leaf node What about cylinders, superquadrics, etc.? Transform ray! Transform the Ray Map the ray from World Space to Object Space r minor r major (x,y) r=1 (0,0) World Space pWS = M pOS pOS = M-1 pWS Object Space Transform Ray New origin: originOS = M-1 originWS New direction: directionOS = M-1 (originWS + 1 * directionWS) - M-1 originWS directionOS = M-1 directionWS originWS directionWS qWS = originWS + tWS * directionWS originOS directionOS qOS = originOS + tOS * directionOS World Space Object Space Transforming Points & Directions Transform point Transform direction Homogeneous Coordinates: (x,y,z,w) W = 0 is a point at infinity (direction) Map intersection point and normal back to world coordinates Transform Normals Why? They’re used for shading object color only Diffuse Shading Transforming Normals A surface normal is a property, not a geometric entity Correct normal transformation matrix: See http://www.cgafaq.info/wiki/Transforming_Normals Constructive Solid Geometry (CSG) Given overlapping shapes A and B: Union Intersection Subtraction How can we implement CSG? Points on B, Outside of A Points on A, Outside of B Points on B, Inside of A Union Points on A, Inside of B Intersection Subtraction Collect all the intersections Union Intersection Subtraction Implementing CSG Test "inside" intersections: 1. • • Find intersections with A, test if they are inside/outside B Find intersections with B, test if they are inside/outside A Overlapping intervals: 2. • • Find the intervals of "inside" along the ray for A and B Compute union/intersection/subtraction of the intervals Constructive Solid Geometry Ray Tracing CSG Models Rick Parent Ohio State U. CSG Form object as booleans of primitive objects Primitives: sphere, cube, cylinder, cone Boolean operators: union, intersection, difference Tree structure used to manage operations Leaf nodes are primitive objects Intermediate nodes specify combination operator B Union C + C Ray intersects union: at first intersection CSE 681 Min (tCmin, tBmin ) B Possible ways for 2 spans to overlap B Intersection C ++ C B First time in B and in C If ((tCmin< tBmin ) and (tCmax> tBmin ) ): tBmin Else If ((tBmin< tCmin ) and (tBmax> tCmin ) ): tCmin Else: none B Difference C -+ C First time in B not in C If ((tBmin< tCmin ): tBmin Else if (tCmax< tBmax ): tCmax Else: none B B Difference +- C C First time in C not in B If ((tCmin< tBmin): tCmin Else if (tBmax< tCmax ): tBmax Else: none B Primitives Anything that can be intersected (easily) with a ray Conics: solve analytically using R(t) Convex polyhedra A plane (a cutting plane is useful) can be used as a modeling tool (boolean operations) surface model (e.g., polyhedron) computed from CGS or Can be used as a model representation keep tree structure and ray trace directly Controlling the Combinations + - + ? T2 T1 - + Tree Structure + T3 T5 + + T4 + - T2 rectangle T1 circle T3 rectangle Tree Structure #1 T2 T1 - + Tree Structure + T3 T5 + + T4 - + T2 rectangle T1 circle T3 rectangle Tree Structure #2 Tree Structure • Intersect ray with leaf nodes (primitive objects) • Combine intersection spans according to intermediate nodes • union • intersection • difference • Might create multiple spans Union of Spans Intersection of Spans Difference of Spans Normals of CSG intersections Normal of some surface (or its negation) Union or intersection: positive normal of intersected surface Difference normals • Intersection is one of: • tmin of positive object – normal of surface • tmax of negative object – negated normal Add transformations to tree http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/model/csg.html Construction •Use bounding volumes at leaf nodes • Union bounding volumes at interior nodes Bounding Volumes T5 + + Traversal •Top-down •Test bounding volume at interior T4 + - T2 rectangle T1 circle T3 rectangle CSE 681 Example Example Example (Simon Chorley) Example (Simon Chorley) For example: Wrap Up Discuss status/problems/issues with this week’s programming assignment