Post on 13-Jul-2020
CS 636 Advanced Rendering Techniques
Dr. David BreenOnlineWednesday 6PM ® 8:50PM
Presentation 34/22/20
Questions from Last Week?n Color modelsn Light modelsn Phong shading modeln Assignment 2
Slide Credits
n Leonard McMillan, Seth Teller, Fredo Durand, Barb Cutler - MIT
n David Luebke - University of Virginian Matt Pharr - Stanford Universityn Jonathan Cohen - Johns Hopkins U.n Kevin Suffern -University of Technology,
Sydney, Australia
More Geometry & Intersections
Ray/Plane IntersectionRay 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 planeNormal Vector, N = [A, B, C] (unit vector)A2 + B2 + C2 = 1D = - 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)First check that N • Rd not equal to zero!
What Can Happen?N • Rd = 0 N • Rd > 0
t < 0
Ro
t > 0
Ro
Ray/Plane SummaryIntersection point:
(xi, yi, zi) = (xo + xdti, yo + ydti, zo + zdti)
1. Calculate N • Rd and compare it to zero.2. Calculate ti and compare it to zero.3. Compute intersection point.4. Flip normal if N • Rd is positive
Ray-Parallelepiped Intersection
n Axis-alignedn From (X1, Y1, Z1) to (X2, Y2, Z2)n Ray P(t)=Ro+Rdt
y=Y2
y=Y1
x=X1 x=X2
Ro
Rd
Naïve ray-box Intersectionn Use 6 plane equationsn Compute all 6 intersectionn Check that points are inside boxn The sign of Ax+By+Cz+D tells you if point is
above or below the planey=Y2
y=Y1
x=X1 x=X2
Ro
Rd
Factoring out computation
n Pairs of planes have the same normaln Normals have only one non-0 componentn Do computations one dimension at a timen Maintain tnear and tfar (closest and farthest so
far)y=Y2
y=Y1
x=X1 x=X2
Ro
Rd
Test if parallel
n If Rdx = 0, then ray is paralleln If Rox < X1 or Rox > x2 return false
y=Y2
y=Y1
x=X1 x=X2
Ro
Rd
If not paralleln Calculate intersection distance t1 and t2
n t1 = (X1-Rox)/Rdxn t2 = (X2-Rox)/Rdx
y=Y2
y=Y1
x=X1 x=X2
Ro
Rd
t1
t2
Test 1n Maintain tnear and tfar
n If t1 > t2, swapn if t1 > tnear, tnear = t1 n if t2 < tfar, tfar = t2
n If tnear > tfar, box is missed
y=Y2
y=Y1
x=X1 x=X2
RoRd
t1x t2xtnear
t1y
t2ytfar
tfar
Test 2n If tfar < 0, box is behind
y=Y2
y=Y1
x=X1 x=X2
Ro
Rd
t1xt2xtfar
Algorithm recapn Do for all 3 axes
n Calculate intersection distance t1 and t2n Maintain tnear and tfarn If tnear > tfar, box is missed; Donen If tfar < 0, box is behind; Done
n If box survived tests, return intersection at tnearn If tnear is negative, return tfar
y=Y2
y=Y1
x=X1 x=X2RoRd
tnear
t1y
tfar
Motivation
Extra rays needed for these effectsn Distributed Ray Tracing
n Soft shadowsn Anti-aliasing (getting rid of jaggies)n Glossy reflectionn Motion blurn Depth of field (focus)
Shadowsn one shadow ray per
intersection per point light source no shadow rays
one shadow ray
Soft Shadowsn multiple shadow rays
to sample area light source one shadow ray
lots of shadow rays
Antialiasing – Supersamplingn multiple
rays per pixel
point light
area light
jaggies w/ antialiasing
n one reflection ray per intersection
perfect mirror
Reflection
θθ
Glossy Reflectionn multiple reflection rays
polished surfaceθθ
Justin Legakis
Motion Blurn Sample
objects temporally
Rob Cook
Depth of Fieldn multiple rays per pixel
Justin Legakisfocal lengthfilm
Algorithm Analysisn Ray castingn Lots of primitivesn Recursiven Distributed Ray
Tracing Effectsn Soft shadowsn Anti-aliasingn Glossy reflectionn Motion blurn Depth of field
cost ≤ height * width * num primitives * intersection cost * num shadow rays *supersampling *num glossy rays * num temporal samples *max recursion depth *. . .
can we reduce this?
Bounding Regions
Acceleration of Ray Castingn Goal: Reduce the
number of ray/primitive intersection tests
Conservative Bounding Regionn First check for an
intersection with a conservative bounding region
n Early reject
Conservative Bounding Regionsbounding
sphere
axis-aligned bounding box
arbitrary convex region (bounding half-spaces)
non-aligned bounding box
• tight ® avoidfalse positives
• fast to compute
• fast to intersect
Bounding Volumesn What makes a “good” bounding volume?
n Tightness of fit (expressed how?)n Easy to computen Simplicity of intersection n Total cost = b*B + i*I +S
• b: # times volume tested for intersection• B: cost of ray-volume intersection test• i: # times item is tested for intersection• I: cost of ray-item intersection test• S: cost to compute BV parameters
Bounding Volumesn Spheres
n Cheap intersection testn Poor fit n Somewhat costly
to fit to data
bounding sphere
Bounding Volumesn Axis-aligned bounding boxes (AABBs)
n Relatively cheap intersection testn Usually better fitn Trivial to fit to data
axis-aligned bounding box
Bounding Volumesn Oriented bounding boxes (OBBs)
n Medium-expensive intersection testn Very good fit (asymptotically better)n Medium-difficult to fit to data
oriented bounding box
Bounding Volumesn Slabs (parallel planes)
n Comparatively expensiven Very good fitn Very difficult to fit to
data
arbitrary convex region (bounding half-spaces)
Intersection with Axis-Aligned Box
n For all 3 axes, calculate the intersection distances t1 and t2
n tnear = max (t1x, t1y, t1z)tfar = min (t2x, t2y, t2z)
n If tnear> tfar, box is missed
n If tfar< 0, box is behind
n If box survived tests, report intersection at tnear
y=Y2
y=Y1
x=X1 x=X2
tnear
tfar
t1x
t1y
t2x
t2y
Bounding Box of a Triangle
(xmin, ymin, zmin)
(x0, y0, z0)
(x1, y1, z1)
(x2, y2, z2)= (min(x0,x1,x2), min(y0,y1,y2),min(z0,z1,z2))
(xmax, ymax, zmax)= (max(x0,x1,x2),
max(y0,y1,y2),max(z0,z1,z2))
Bounding Box of a Sphere
r
(x, y, z)
(xmin, ymin, zmin)= (x-r, y-r, z-r)
(xmax, ymax, zmax)
= (x+r, y+r, z+r)
Bounding Box of a Group
(xmin, ymin, zmin) = (min(xmin_a,xmin_b), min(ymin_a,ymin_b),min(zmin_a,zmin_b))
(xmax, ymax, zmax)= (max(xmax_a,xmax_b),
max(ymax_a,ymax_b),max(zmax_a,zmax_b))
(xmin_b, ymin_b, zmin_b)
(xmin_a, ymin_a, zmin_a)
(xmax_b, ymax_b, zmax_b)(xmax_a, ymax_a, zmax_a)
Acceleration Spatial Data Structures
Spatial Data Structuresn Spatial partitioning techniques classify all space into
non-overlapping portionsn Easier to generate automaticallyn Can “walk” ray from partition to partition
n Hierarchical bounding volumes surround objects in the scene with (possibly overlapping) volumesn Often tightest fit
Spatial Partitioningn Some spatial partitioning schemes:
n Regular grid (2-D or 3-D)n Octreen k-D treen BSP-tree
Acceleration Spatial Data Structures
Regular Grid
Regular Grid
Create gridn Find
bounding box of scene
n Choose grid spacing
n gridx need not = gridy
Cell (i, j)
gridy
gridx
Insert primitives into gridn Primitives
that overlap multiple cells?
n Insert into multiple cells (use pointers)
n Does the cell contain an intersection?
n Yes: return closestintersection
n No: continuen Use
algorithm to step through cells
For each cell along a ray
Preventing repeated computationn Perform the
computation once, "mark" the object
n Don't re-intersect marked objects
n If intersection t is not within the cell range, continue (there may be something closer)
Don't return distant intersections
Where do we start?n Intersect ray
with scene bounding box
n Ray origin may be inside the scene bounding box
tmin
tnext_v
tnext_h
tmin
tnext_vtnext_h
Cell (i, j)
Is there a pattern to cell crossings?n Yes, the
horizontal and vertical crossings have regular spacing
dtv = gridy / diry
dth = gridx / dirxgridy
gridx
(dirx, diry)
What's the next cell?if tnext_v < tnext_h
i += signx
tmin = tnext_v
tnext_v += dtv
elsej += signy
tmin = tnext_h
tnext_h += dth dtvdth
Cell (i, j)
tmin
tnext_v
tnext_h
Cell (i+1, j)
(dirx, diry)
if (dirx > 0) signx = 1 else signx = -1if (diry > 0) signy = 1 else signy = -1
What's the next cell? n 3DDDA – Three
Dimensional Digital Difference Analyzer
n 3D Bresenham Algorithm
Pseudo-codecreate grid insert primitives into gridfor each ray rfind initial cell c(i,j), tmin, tnext_v & tnext_hcompute dtv, dth, signx and signywhile c != NULLfor each primitive p in cintersect r with pif intersection in range foundreturn
c = find next cell
Regular Grid Discussion
n Advantages?n easy to constructn easy to traverse
n Disadvantages?n may be only sparsely filledn geometry may still be clumped in a small
number of cells
Acceleration Spatial Data Structures
Adaptive Grids
Adaptive Grids
Nested Grids Octree/(Quadtree)
n Subdivide until each cell contains no more than n elements, or maximum depth d is reached
Primitives in an Adaptive Gridn Can live at intermediate levels, or
be pushed to lowest level of grid
Octree/(Quadtree)
Adaptive Grid Discussionn Advantages?
n grid complexity matches geometric densityn Disadvantages?
n more expensive to traverse (especially octree)
k-D Treesn k-D tree pros:
n Moderately simple to generaten More adaptive than octrees
n k-D tree cons:n Less efficient to trace rays
acrossn Moderately complex data
structure
BSP Treesn BSP tree pros:
n Extremely adaptiven Simple & elegant data
structuren BSP tree cons:
n Very hard to create optimal BSP
n Splitting planes can explode storage
n Simple but slow to trace rays across
Acceleration Spatial Data Structures
Bounding Volume Hierarchy
Bounding Volume Hierarchyn What makes a “good” bounding volume
hierarchy?n Grouped objects (or volumes) should be near
each othern Volume should be minimaln Sum of all volumes should be minimaln Top of the tree is most criticaln Constructing the hierarchy should pay for
itself!
Bounding Volume Hierarchyn Find bounding box of objectsn Split objects into two groupsn Recurse
Bounding Volume Hierarchyn Find bounding box of objectsn Split objects into two groupsn Recurse
Bounding Volume Hierarchyn Find bounding box of objectsn Split objects into two groupsn Recurse
Bounding Volume Hierarchyn Find bounding box of objectsn Split objects into two groupsn Recurse
Bounding Volume Hierarchyn Find bounding box of objectsn Split objects into two groupsn Recurse
Where to split objects?n At midpoint ORn Sort, and put half of the objects on each side ORn Use modeling hierarchy
Data Structure Pseudo-codesort_axis = 0;Make_BVH(object_list, sort_axis, ptr)
struct.bbox = BoundingBox(object_list);If # of objects < Threshold
struct.obj_list = object_listElse
If ((sort_axis % 3)== 0) Sort object centroids in XElseIf ((sort_axis % 3)== 1)
Sort object centroids in YElse
Sort object centroids in ZSplit sorted list into two halvesMake_BVH(left_obj_list, sort_axis++, lptr)Make_BVH(right_obj_list, sort_axis++, rptr)struct.lptr = lptr; struct.rptr = rptr;
ptr = &struct;
Return
Intersection with BVHn Check subvolume with closer intersection first
Intersection with BVHn Don't return intersection immediately if the
other subvolume may have a closer intersection
Intersection Pseudo-codeDoes ray intersect box?intersect_BVH(box, ray, xsect_pt, t)
If no more subboxesIntersect geometry and return nearest xsect_pt & t
Intersect ray with both subboxesNo hits: return xsect_pt = Null;Sort t’sCall subbox of nearest t subbox1intersect_BVH(subbox1, ray, xsect_pt, t)If hit_subbox2?
If xsect_pt == Null || t2near <= t1farintersect_BVH(subbox2, ray, xsect_pt, t)Set nearest xsect_pt and t
Return
Bounding Volume Hierarchy Discussionn Advantages
n easy to constructn easy to traversen binary
n Disadvantagesn may be difficult to choose a good split for a noden poor split may result in minimal spatial pruning
n Hintn Alternate sorting in X, Y & Z
Transformation Hierarchy
n Group & Transformation hierarchy may not be a good spatial hierarchy
group
group
transform
transform
transform
transform
A C
D E
transformB
group
transformA B
transformC D
transformC E
Flatten
What’s the best method?n What kind of scene are you rendering?
n Teapot in a stadium vs. uniform distributionn Impact on surface tessellation on distribution
n Parameter values are critical
Shoot Fewer Raysn Adaptive depth control
n Naïve ray tracer: spawn 2 rays per intersection until max recursion limit
n In practice, few surfaces are transparent or reflective
n Stop shadow ray at first intersection between start and light source
n Just shoot the rays you needn Determine contribution of ray
• Don’t shoot rays w/ contribution near 0%
Shoot Fewer Raysn Adaptive sampling
n Shoot rays coarsely, interpolating their values across pixels
n Where adjacent rays differ greatly in value, sample more finely
n Stop when some maximum resolution is reached
Generalized Raysn Beams, cones, pencilsn Area sampling, rather than point samplingn Geometric computations are tricky
(expensive?)n Problems with reflection/refractions
Wrap Upn Discuss next programming assignment
n Add an acceleration technique• Adaptive grid• Bounding volume hierarchy
n Supersample imagen Discuss status/problems/issues with
this week’s programming assignment