Bounding Volume Hierarchies - my.eng.utah.edu

55
Bounding Volume Hierarchies CS 6965 Fall 2011

Transcript of Bounding Volume Hierarchies - my.eng.utah.edu

Page 1: Bounding Volume Hierarchies - my.eng.utah.edu

Bounding Volume Hierarchies

CS 6965 Fall 2011

Page 2: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965 2

Page 3: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Heightfield3

Page 4: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

C++ Operator?

4

#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

Page 5: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

C++ Operator?

5

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 )

Page 6: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Program 26

Page 7: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Backwards Lighting7

Page 8: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Light Direction

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

Compute hit position (P= O+ tV

)Call primitive to get normal (N

) (normalized)

costheta = N⋅V

if (costheta < 0) normal =-normalColor light = scene.ambient*Kaforeach light source get CL and L

dist= L

,Ln

=L

L

cosphi = N⋅ Ln

if(cosphi > 0) if(!intersect with 0 < t < dist) light += CL * (Kd * cosphi)result=light*surface color

Page 9: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Library Calls

9

• Show up as something like this:

• This one was fixed by removing global variables

• Use #define or keep things locally

ERROR: undefined symbol: __dso_handlefailed to add instruction: ORIline 54assembler returned an error, exiting

Page 10: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Memset• We do have this one!

• Change this line:

• To this line:

10

@${LINKERDIR}/ln.py rt.s > ${TRAXOUTPUT}

@${LINKERDIR}/ln.py rt.s ${LIBDIR}/memset.s > ${TRAXOUTPUT}

Page 11: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Inheritance

• We can’t handle these in simhwrt yet

11

_ZTV8gpSphere:# .long 0# .long _ZTI8gpSphere# .long _ZNK8gpSphere17intersectDetailedERK12RayPrimitiveR18IntersectionRecord# .long _ZNK8gpSphere14intersectQuickERK12RayPrimitive# .size _ZTV8gpSphere, 16

Page 12: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Calling Convention 12

Page 13: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Calling Convention13

Page 14: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Common Problems

14

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) {

Page 15: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Common Problems

15

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);

Page 16: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Common Problems

16

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;

Page 17: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Common Problems

17

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;

Page 18: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Common Problems

18

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);

Page 19: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Common Problems

19

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;

Page 20: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Common Problems

20

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) {

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) {

Before After

Page 21: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Bounding boxes• Box: easy

• bounds = Min(p1, p2), Max(p1, p2)

• Sphere:

• bounds = C-Vector(radius, radius,radius)

• C+Vector(radius, radius, radius)

21

P1

P2

C

Page 22: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Bounding Boxes• Triangle:

• bounds = Min(p1,p2,p3), Max(p1,p2,p3)

• Plane

• infinite bounds

• You may want to consider removing planes from your renderer at this point

• Or keep them outside of your accel structures

22

P1

P2P3

Page 23: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Bounding boxes• Disc/Ring:

• Group:

• Union of object bounding boxes

• min of mins

• max of maxs

Cx ± rad Ny2 + Nz

2

N

= 1Similar for y/z

23

Page 24: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Uses for bounding boxes

• Quick reject for expensive primitives

• To fill in grid cells

• Directly in Bounding Volume Hierarchy or similar

24

Page 25: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 26: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 27: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 28: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 29: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 30: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 31: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

BVH optimizations

• Stop if the current T value is closer than the BVH node

31

Page 32: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 33: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 34: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Building a BVH• Determining optimal BVH structure is

NP-hard problem

• Heuristic approaches:

• Cost models (minimize volume or surface area)

• Spatial models

• Categories of approaches:

• Top down

• Bottom up

34

okay okay

bad

Page 35: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Median cut BVH construction

• Top down approach:

• 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

Page 36: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 37: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 38: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 39: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 40: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 41: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 42: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

BSP/k-d tree tradeoffs• Build is NP-hard problem

• Heuristic approaches are always used

• Traversal is quick

• Storage can be lower than BVH or grid

42

Page 43: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

K-d traversal• Keep track of intervals on ray:

• min: 0

• max: ∞

• Compute t of split plane

• If t > max: goto near child

• If t < min: goto far child

• Otherwise: goto both children with updated intervals

43

Page 44: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

K-d traversal• Keep track of intervals on ray:

• Compute t of split plane

• If t > max: goto near child

• If t < min: goto far child

• Otherwise: goto both children with updated intervals

44

Min: 0Max: ∞

Page 45: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

K-d traversal• Keep track of intervals on ray:

• Compute t of split plane

• If t > max: goto near child

• If t < min: goto far child

• Otherwise: goto both children with updated intervals

45

Min: 0Max: ∞

Split: 1.0Stack: 1

New min: 0New max: 1.0

12

Page 46: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

K-d traversal• Keep track of intervals on ray:

• Compute t of split plane

• If t > max: goto near child

• If t < min: goto far child

• Otherwise: goto both children with updated intervals

46

Min: 0Max: 1.0

Split: 1.3Stack: 1

New min: 0New max: 1.0

3 4

Page 47: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

K-d traversal• Keep track of intervals on ray:

• Compute t of split plane

• If t > max: goto near child

• If t < min: goto far child

• Otherwise: goto both children with updated intervals

47

Min: 0Max: 1.0

Split: 0.4Stack: 1 5

New min: 0New max: 0.4

56

Page 48: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

K-d traversal• Keep track of intervals on ray:

• Compute t of split plane

• If t > max: goto near child

• If t < min: goto far child

• Otherwise: goto both children with updated intervals

48

Min: 0.4Max: 1.0

Split: 0.7Stack: 1 8

New min: 0.4New max: 0.6

7 8

Page 49: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

K-d traversal• Keep track of intervals on ray:

• Compute t of split plane

• If t > max: goto near child

• If t < min: goto far child

• Otherwise: goto both children with updated intervals

49

Min: 0.7Max: 1.0

Split:Stack: 1

New min: 0.4New max: 0.6

7 8

Page 50: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

K-d traversal• Keep track of intervals on ray:

• Compute t of split plane

• If t > max: goto near child

• If t < min: goto far child

• Otherwise: goto both children with updated intervals

50

Min: 1.0Max: ∞

Split: 1.5Stack: 10

New min: 1.0New max: 1.5

9 10

Page 51: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

K-d traversal• Keep track of intervals on ray:

• Compute t of split plane

• If t > max: goto near child

• If t < min: goto far child

• Otherwise: goto both children with updated intervals

51

Min: 1.0Max: 1.5

Split: 1.4Stack: 10 11

New min: 1.0New max: 1.4

1112

Page 52: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

K-d tree build• Optimal solution is NP-hard

• Spatial median split (a little like octree)

• Object median split (makes balanced trees)

• Cost model based (better)

• Cost of traversal

• Cost of intersection

• Probability of hit

52

Page 53: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

K-d tree build• Havran ‘01:

• Start with bounding box of scene

• Select split plane along each axis

• Start with spatial median

• Move toward bounding box of child nodes

• Recurse

• Stop if node contains 2-3 objects

• Stop if depth > max (20-30)

• Backtrack

• Combine children with identical content53

Page 54: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

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

Page 55: Bounding Volume Hierarchies - my.eng.utah.edu

Fall 2011CS 6965

Update SimHWRT• svn up

• make

• ./update.sh

• This one will take a while

55