Bounding Volume Hierarchies
2
Heightfield 3
C++ Operator? #include <stdio.h> int main() { int x = 10; while( x --> 0 ) // x goes to 0 { printf("%d ", x); } } stackoverflow.com/questions/1642028/what-is-the-name-of-this-operator 4
C++ Operator? That's not an operator -->. That's two separate operators, -- and >. Your post is decrementing x and then comparing x and 0 with the > operator To better understand, the statement could be as follows: while( (x--) > 0 ) 5
Program 2 6
Backwards Lighting 7
Light Direction Compute hit position (P = O + tv ) Call primitive to get normal (N ) (normalized) costheta = N V if (costheta < 0) normal =-normal Color light = scene.ambient*ka foreach light source get C L and L dist= L L, L n = L cosphi = N L n if(cosphi > 0) if(!intersect with 0 < t < dist) light += C L *(Kd * cosphi) result=light*surface color 8 costheta > 0 Means N and V are on opposite sides Should flip the normal costheta < 0 Means N and V are on the same side Keep the normal
Library Calls Show up as something like this: ERROR: undefined symbol: dso_handle failed to add instruction: ORI line 54 assembler returned an error, exiting This one was fixed by removing global variables Use #define or keep things locally 9
We do have this one! Change this line: To this line: Memset @${LINKERDIR}/ln.py rt.s > ${TRAXOUTPUT} @${LINKERDIR}/ln.py rt.s ${LIBDIR}/memset.s > ${TRAXOUTPUT} 10
Inheritance _ZTV8gpSphere: #.long 0 #.long _ZTI8gpSphere #.long _ZNK8gpSphere17intersectDetailedERK12RayPrimitiveR18IntersectionRecord #.long _ZNK8gpSphere14intersectQuickERK12RayPrimitive #.size _ZTV8gpSphere, 16 We can t handle these in simhwrt yet 11
Calling Convention 12
Calling Convention 13
Common Problems void draw_line(const int& framebuffer, const int& width, const Vector& V1, const Vector& V2, const int& max_pixel) { int x0 = V1[0]; int x1 = V2[0]; int y0 = V1[1]; int y1 = V2[1]; int dx = x0 - x1; if (dx < 0) dx = -dx; int dy = y0 - y1 if (dy < 0) dy = -dy; int sx, sy; if (x0 < x1) sx = 1; else sx = -1; if (y0 < y1) sy = 1; else sy = -1; int err = dx - dy; int e2; while (x0!= x1 y0!= y1) { 14
Common Problems int x0 = V1[0]; int x1 = V2[0]; int y0 = V1[1]; int y1 = V2[1]; trax_printi(x0); trax_printi(x1); trax_printi(y0); trax_printi(y1); int dx = x0 - x1; if (dx < 0) dx = -dx; int dy = y0 - y1 if (dy < 0) dy = -dy; int sx, sy; if (x0 < x1) sx = 1; else sx = -1; if (y0 < y1) sy = 1; else sy = -1; int err = dx - dy; int e2; trax_printi(err); 15
Common Problems int x0 = V1[0]; int x1 = V2[0]; int y0 = V1[1]; int y1 = V2[1]; trax_printi(x0); trax_printi(x1); trax_printi(y0); trax_printi(y1); int dx = x0 - x1; if (dx < 0) dx = -dx; int dy = y0 - y1 if (dy < 0) dy = -dy; int sx, sy; if (x0 < x1) sx = 1; else sx = -1; if (y0 < y1) sy = 1; else sy = -1; trax_printi(dx); trax_printi(dy); int err = dx - dy; int e2; 16
Common Problems int x0 = V1[0]; int x1 = V2[0]; int y0 = V1[1]; int y1 = V2[1]; trax_printi(x0); trax_printi(x1); trax_printi(y0); trax_printi(y1); int dx = x0 - x1; if (dx < 0) dx = -dx; int dy = y0 - y1 if (dy < 0) dy = -dy; trax_printi(dx); trax_printi(dy); int sx, sy; if (x0 < x1) sx = 1; else sx = -1; if (y0 < y1) sy = 1; else sy = -1; int err = dx - dy; int e2; 17
Common Problems trax_printi(x0); trax_printi(x1); trax_printi(y0); trax_printi(y1); int dx = x0 - x1; if (dx < 0) dx = -dx; // dx = abs(dx) int dy = y0 - y1 if (dy < 0) dy = -dy; // dy = abs(dy) trax_printi(dx); trax_printi(dy); 18
Common Problems trax_printi(x0); trax_printi(x1); trax_printi(y0); trax_printi(y1); int dx = x0 - x1; if (dx < 0) dx = -dx; // dx = abs(dx) int dy = y0 - y1 if (dy < 0) dy = -dy; // dy = abs(dy) trax_printi(dx); trax_printi(dy); // Write it a different way int dx = x0 > x1? x0 - x1 : x1 - x0; int dy = y0 > y1? y0 - y1 : y1 - y0; 19
Common Problems Before void draw_line(const int& framebuffer, const int& width, const Vector& V1, const Vector& V2, const int& max_pixel) { int x0 = V1[0]; int x1 = V2[0]; int y0 = V1[1]; int y1 = V2[1]; int dx = x0 - x1; if (dx < 0) dx = -dx; int dy = y0 - y1 if (dy < 0) dy = -dy; int sx, sy; if (x0 < x1) sx = 1; else sx = -1; if (y0 < y1) sy = 1; else sy = -1; int err = dx - dy; int e2; while (x0!= x1 y0!= y1) { 20 After void draw_line(const int& framebuffer, const int& width, const Vector& V1, const Vector& V2, const int& max_pixel) { int x0 = V1[0]; int x1 = V2[0]; int y0 = V1[1]; int y1 = V2[1]; int dx = x0 > x1? x0 - x1 : x1 - x0; int dy = y0 > y1? y0 - y1 : y1 - y0; int sx, sy; if (x0 < x1) sx = 1; else sx = -1; if (y0 < y1) sy = 1; else sy = -1; int err = dx - dy; int e2; while (x0!= x1 y0!= y1) {
Box: easy Bounding boxes bounds = Min(p1, p2), Max(p1, p2) P2 Sphere: bounds = C-Vector(radius, radius,radius) C+Vector(radius, radius, radius) P1 C 21
Bounding Boxes Triangle: bounds = Min(p1,p2,p3), Max(p1,p2,p3) P1 Plane infinite bounds P2 You may want to consider removing planes from your renderer at this point Or keep them outside of your accel structures P3 22
Bounding boxes Disc/Ring: C x ± rad N 2 2 y + N z N = 1 Similar for y/z Group: Union of object bounding boxes min of mins max of maxs 23
Uses for bounding boxes Quick reject for expensive primitives To fill in grid cells Directly in Bounding Volume Hierarchy or similar 24
Bounding Volume Hierarchy Observe that intersection is like a search We know that searching is O(n) for unsorted lists but O(log n) for sorted lists How do we sort objects from all directions simultaneously? 25
Bounding volume hierarchy Organize objects into a tree Group objects in the tree based on spatial relationships Each node in the tree contains a bounding box of all the objects below it 26
BVH traversal At each level of the tree, intersect the ray with the bounding box miss: ray misses the entire subtree hit: recurse to both children 27
BVH traversal At each level of the tree, intersect the ray with the bounding box miss: ray misses the entire subtree hit: recurse to both children 28
BVH traversal At each level of the tree, intersect the ray with the bounding box miss: ray misses the entire subtree hit: recurse to both children 29
BVH traversal At each level of the tree, intersect the ray with the bounding box miss: ray misses the entire subtree hit: recurse to both children 30
BVH optimizations Stop if the current T value is closer than the BVH node 31
BVH optimizations Stop if the current T value is closer than the BVH node Traverse down side of tree that is closer to origin of the ray first 32
BVH optimizations Stop if the current T value is closer than the BVH node Traverse down side of tree that is closer to origin of the ray first Three or more way split 33
Building a BVH Determining optimal BVH structure is NP-hard problem Heuristic approaches: Cost models (minimize volume or surface area) Spatial models Top down Bottom up Categories of approaches: 34 okay bad okay
Median cut BVH Top down approach: construction Sort objects by position on axis cycle through x,y,z use center of bounding box Insert tree node with half of objects on left and half on right 35
Weghorst BVH construction Bottom up construction Add objects one at a time to tree Insert to subtree that would cause smallest increase to area 36
Weghorst BVH construction Bottom up construction Add objects one at a time to tree Insert to subtree that would cause smallest increase to area 37
Weghorst BVH construction Bottom up construction Add objects one at a time to tree Insert to subtree that would cause smallest increase to area 38
Weghorst BVH construction Bottom up construction Add objects one at a time to tree Insert to subtree that would cause smallest increase to area 39
k-d tree Recursively divide space in half Alternate coordinate axes Cycle through axes or store axis split in each node What do you do with objects split by the plane? Hard part: where do you split in each dimension? 40
BSP tree Like a k-d tree where splitting planes can be arbitrarily located Hard part: where do you split in each dimension? Harder part: how do you orient each plane? More storage/computation, tighter bounds 41
BSP/k-d tree tradeoffs Build is NP-hard problem Traversal is quick Heuristic approaches are always used Storage can be lower than BVH or grid 42
K-d traversal Keep track of intervals on ray: min: 0 max: Compute t of split plane If t < min: goto far child If t > max: goto near child Otherwise: goto both children with updated intervals 43
K-d traversal Keep track of intervals on ray: Compute t of split plane If t < min: goto far child If t > max: goto near child Min: 0 Max: Otherwise: goto both children with updated intervals 44
K-d traversal Keep track of intervals on ray: 1 2 Compute t of split plane If t < min: goto far child If t > max: goto near child Otherwise: goto both children with updated intervals 45 Min: 0 Max: Split: 1.0 Stack: 1 New min: 0 New max: 1.0
K-d traversal Keep track of intervals on ray: Compute t of split plane If t < min: goto far child If t > max: goto near child Otherwise: goto both children with updated intervals 46 3 4 Min: 0 Max: 1.0 Split: 1.3 Stack: 1 New min: 0 New max: 1.0
K-d traversal Keep track of intervals on ray: 5 6 Compute t of split plane If t < min: goto far child If t > max: goto near child Otherwise: goto both children with updated intervals 47 Min: 0 Max: 1.0 Split: 0.4 Stack: 1 5 New min: 0 New max: 0.4
K-d traversal Keep track of intervals on ray: 7 8 Compute t of split plane If t < min: goto far child If t > max: goto near child Otherwise: goto both children with updated intervals 48 Min: 0.4 Max: 1.0 Split: 0.7 Stack: 1 8 New min: 0.4 New max: 0.6
K-d traversal Keep track of intervals on ray: 7 8 Compute t of split plane If t < min: goto far child If t > max: goto near child Otherwise: goto both children with updated intervals 49 Min: 0.7 Max: 1.0 Split: Stack: 1 New min: 0.4 New max: 0.6
K-d traversal 9 10 Keep track of intervals on ray: Compute t of split plane If t < min: goto far child If t > max: goto near child Otherwise: goto both children with updated intervals 50 Min: 1.0 Max: Split: 1.5 Stack: 10 New min: 1.0 New max: 1.5
K-d traversal Keep track of intervals on ray: 11 12 Compute t of split plane If t < min: goto far child If t > max: goto near child Otherwise: goto both children with updated intervals 51 Min: 1.0 Max: 1.5 Split: 1.4 Stack: 10 11 New min: 1.0 New max: 1.4
K-d tree build Optimal solution is NP-hard Cost model based (better) Cost of traversal Cost of intersection Probability of hit Spatial median split (a little like octree) Object median split (makes balanced trees) 52
Havran 01: K-d tree build Start with bounding box of scene Select split plane along each axis Recurse Start with spatial median Move toward bounding box of child nodes Stop if node contains 2-3 objects Stop if depth > max (20-30) Backtrack Combine children with identical content 53
Acceleration Structure Summary Most common: Grid, Hierarchical Grid, k-d tree, BVH Grid based: P time deterministic build Grid resolution tradeoff Tree based: NP hard build Heuristic approaches 54
Update SimHWRT svn up make./update.sh This one will take a while 55