280 likes | 577 Vues
Ray Intersections. CS 319 Advanced Topics in Computer Graphics John C. Hart. Intersection Computation. Parametric ray: r ( t ) = o + t d t 0 Since || d || = 1, t is distance along ray Implicit object: f ( x ) = 0 f ( x ) > 0 outside, f ( x ) < 0 inside
E N D
Ray Intersections CS 319 Advanced Topics inComputer Graphics John C. Hart
Intersection Computation • Parametric ray: r(t) = o + t d • t 0 • Since ||d|| = 1, t is distance along ray • Implicit object: f(x) = 0 • f(x) > 0 outside, f(x) < 0 inside • Or vice-verse, doesn’t matter • Intersection occurs when f(r(t)) = 0 • Let F(t) = f(r(t)) • Real function of one real variable • Intersection root finding f(x) > 0 f(x) = 0 f(x) < 0 F(t) > 0 F(t) < 0 F(t) > 0 F(t) < 0
Sphere Intersection c r f(x)=(x - c)(x - c) - r2 f(r(t)) = (o + td - c)(o + td - c) - r2 = ddt2 + 2 (o-c)dt + (o-c)(o-c) - r2 D > 0 D = 0 D < 0 D = B*B - 4*AC; if (D < 0.0) return NULL; rootD = sqrt(D); t0 = 0.5*(-B - rootD)/A; t1 = 0.5*(-B + rootD)/A; if (t0 >= 0) hit->t = t0, return hit; if (t1 >= 0) hit->t = t1, return hit; return NULL; A = dd (= 1 if d unit length) B = 2 (o-c)d C = (o-c)(o-c) - r2 Hit position x = o + td Hit normal n = (x – c)/||x – c||
d o x T-1x Ellipsoid Intersection f(T-1x)=0 • Let T be a 4x4 transformation that distorts a sphere into the ellipsoid • Ellipsoid is implicit surface of f(T-1x) f(T-1r(t)) = f(T-1(o + td)) = f(T-1o + tT-1d) • Hit point needs to be in world coords • t same in both coordinates x = o + td T T-1 Ellipsoid::Intersect(Ray r) { Ray Tir = (Ti * r.o, Ti * r.d); return Sphere::Intersect(Tir); } T-1d f(x) = 0 T-1o
n n’ x Tx What about the normal? f(T-1x)=0 • Let n = [abcd] be a tangent plane • Let x = [xyz 1]Tbe a point • Plane-point duality • Planes are row vectors • Points are column vectors • Point x in plane nnx = 0 • Need to find n’ such that n’ Tx = 0 • Notice nT-1T x = 0 • New normal n’ = n T-1 = (T-1)T nT • Could also use the adjoint n’ = nT* • n’ not necessarily unit length even if n is • But we’ll need the inverse anyway T f(x) = 0
Matrix Inverse S-1 = [minors of S]T Don’t need 1/|S| if just need directionof transformed normal. Will have torenormalize anyway is S not special unitary.
Scene Graph • Hierarchical representation of all objects in scene • Transformation nodes • Intersect kids by T-1r • Returned normal (T-1)Tn • Maintain T-1 (not T) Scale 2,2,2Xlate 2,0,0 Xlate 2,2,0Scale .5,1,.5 Xlate -2,0,0 Xlate 2,0,0
Instancing Scale 2,2,2Xlate 2,0,0 Xlate 2,2,0 • Scene graph is a hierarchy • Not necessarily a tree • Directed acyclic graph (DAG) • Nodes may have multiple parents • Instance: Appearance of each node’s geometry in scene Xlate -1,0,0 Xlate 1,0,0 Scale .5,1,.5
Fun with Instancing ImagecourtesyJohnAmanatides
z x y z z z z x x x x y y y y Quadrics Ray: x(t) = ox + tdx, y(t) = oy + tdy z(t) = oz + tdz z r x y Paraboloid:x2 + y2 – z Sphere:x2 + y2 + z2 – r2 Cylinder:x2 + y2 – r2 Variations? Use the transformation trick Hyperboloid:x2 + y2– z2r2 Cone:x2 + y2 – z2
Torus z r x R • Product of two implicit circles (x – R)2 + z2 – r2 = 0 (x + R)2 + z2 – r2 = 0 ((x – R)2 + z2 – r2)((x + R)2 + z2 – r2) = (x2 – 2Rx + R2 + z2 – r2) (x2 + 2Rx + R2 + z2 – r2) = x4 + 2x2z2 + z4 – 2x2r2 – 2z2r2 + r4 – 2x2R2 + 2z2R2 – 2r2R2 + R4 = (x2 + z2 – r2 – R2)2 + 4z2R2 – 4r2R2 • Surface of rotation: replace x2 with x2 + y2 f(x,y,z) = (x2 + y2 + z2 – r2 – R2)2 + 4R2(z2 – r2) • Quartic!!! • Up to four ray torus intersections
Finding Roots • How do we find roots for degree > 2? • Newton’s Method • Converges fast • Might converge to wrong root • Root Isolation • Guarantees single root • Sturm Sequences
Sturm Sequences • f(t) is a polynomial • Create a sequence f1 = f f2 = f’ fk+1 = – rem fk-1 /fk • # of roots in [a,b] = difference in # of sign changes between fk(a) and fk(b) f1(t) = t3 + 3t2 – 1 f2(t) = 3t2 + 6t f3(t) = 2t + 1 f4(t) = 9/4 t f1 f2 f3 f4 # - - + - + 3 -3 - + - + 3 -2 + 0 - + 2 -1 + - - + 2 0 - 0 + + 1 1 + + + + 0 2 + + + + 0
Ray-Object Intersection in out • Returns intersection in a hit record • “Next” field enables hit record to hold a list of intersections • List only non-negative intersection parameters • Ray always originates outside • If first t = 0 then ray originated inside • Parity classifies ray segments • Odd segments “in” • Even segments “out” in out in in out out
Constructive Solid Geometry B • Construct shapes from primitives using boolean set operations • Union: AB, A + B, A or B • Intersection: AB, A*B, A and B • Difference: A\B, A–B, A and not B A B–A AB AB A–B
A B CSG Intersections A.t0 B.t0 A.t1 B.t1 • List of t-values for A, B w/in-out classification A.t_list = {0.9, 3.1} = {0.9in, 3.1out} B.t_list = {2.5, 4.5} = {2.5in, 4.5out} • Use dot(r.d,n) to determine in,out • Merge both lists into a single t-ordered list { 0.9 Ain Bout, 2.5 Ain Bin, 3.1 Aout Bin, 4.5 Aout Bout } • Keep track of A and B in/out classification • Use Roth table to classify t-values A+B = {0.9in, 2.5in, 3.1in, 4.5out} = {0.9, 4.5} A*B = {0.9out, 2.5in, 3.1out, 4.5out} = {2.5, 3.1} A-B = {0.9in, 2.5out,3.1out, 4.5out} = {0.9, 2.5} • Remove redundant t-values 0.9 2.5 3.1 4.5 Roth Table Op A B Res + in in in in out in out in in out out out * in in in in out out out in out out out out – in in out in out in out in out out out out
Accelerating Ray Intersections • Q: Why is basic ray tracing so slow? • A: It intersects every ray with every primitive in every object • Q: How can we make ray tracing faster? • A: Coherence Image coherence – neighboring pixels probably display same object Spatial coherence – neighboring points probably exhibit same appearance Temporal coherence– Pixels in neighboring frames probably display same object Stanford Bunny~70K triangles Do we need 70K ray-triangleintersections for each ray?
Shadow Caching s • Any interloper between surface point x and the light source s will cast a shadow • Doesn’t matter how many • Doesn’t matter which is closest • Stop ray intersections once any intersection found • Neighboring shadowed surface points x and x’ probably shadowed by the same object • Start shadow ray intersection search with object intersected in last shadow search C A B x’ x
Bounding Volume • Ray-bunny intersection takes 70K ray-triangle intersections even if ray misses the bunny • Place a sphere around bunny • Ray A misses sphere so ray A misses bunny without checking 70K ray-triangle intersections • Ray B intersects sphere but still misses bunny after checking 70K intersections • Ray C intersects sphere and intersects bunny • Can also use axis-aligned bounding box • Easier to create for triangle mesh A B C
A Bounding Volume Hierarchy B C • Associate bounding volume with each node of scene graph • If ray misses a node’s bounding volume, then no need to check any node beneath it • If ray hits a node’s BV, then replace it with its children’s BV’s (or geometry) • Breadth first search of tree • Maintain heap ordered by ray-BV intersection t-values • Explore children of nodew/least pos. ray-BV t-value Bunny BV Head BV Body BV Face BV L.Ear BV R.Ear BV
Grids • Encase object in a 3-D array of cubic cells • Each cell contains list of all triangles it contains or intersects • Rasterize ray to find which cells it intersects • 3D Bresenham algorithm • All cells that contain any part of ray • Working from first ray-cell to last… • Find least positive intersect of ray with triangles in cell’s list • If no intersection, move on to next cell
Tagging • Ray-object intersection test valid for ray with entire object • not just portion of object inside current cell • Need only intersect object once for each ray • In cell A – list = {#1} • Intersect r with #1? Yes • Miss Tag #1 with no-intersection • In cell B – list = {#2} • Intersect r with #2? Yes • ray r hits object #2 but later in cell C • Tag object #2 with intersection-at-C • In cell C – list = {#1,#2} • Intersect r with #1? No (no-intersection) • Intersect r with #2? No (intersection-at-D) • In cell D – list = {#2} • Intersect r with #2? No (intersection-at-D) A B #2 C D #1 r
Other Partitioning Structures • Octree • Ray can parse through large empty areas • Requires less space than grid • Subdivision takes time • Binary Space Partition (BSP) Tree • Planes can divide models nearly in half • Trees better balanced, shallower • Added ray-plane intersections