cs 6958 lecture 7 triangles, bvhcs6958/slides/lec8_2.pdf · 2014-02-04 · /// sphere primitive...

35
CS 6958 LECTURE 8 TRIANGLES, BVH February 3, 2014

Upload: others

Post on 20-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

CS 6958

LECTURE 8

TRIANGLES, BVH

February 3, 2014

Page 2: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Last Time

derived ray-triangle intersection

clarification:

ray tracing inherently abstract in terms of object

specification

we can use any object once we define an

algorithm for intersecting it with a ray (and

computing localized normal direction)

2

Page 3: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Ray Tracing Algorithm

foreach frame

foreach pixel

foreach sample

generate ray

intersect ray with objects

shade intersection point

3

Page 4: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Ray Tracing Algorithm

foreach frame

foreach pixel

foreach sample

generate ray

intersect ray with objects

shade intersection point

4

foreach object

t_new = object.intersect(ray)

t_closest = min(t_closest, t_new)

Page 5: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Ray Tracing Algorithm

/// Abstract Primitive class defining properties which are required for our ray tracer.

/// For now, it specifies just ray-object intersection routine, but can be extended to

/// support shadow rays, bounding volumes, etc

class Primitive {

public:

virtual bool Intersect(const Ray &ray) const = 0;

}

/// Sphere primitive

class Sphere : public Primitive {

bool Intersect(const Ray &ray) const;

}

// Triangle primitive

class Triangle : public Primitive {

bool Intersect(const Ray &ray) const;

}

5

Page 6: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Ray Tracing Algorithm

/// Abstract Primitive class defining properties which are required for our ray tracer.

/// For now, it specifies just ray-object intersection routine, but can be extended to

/// support shadow rays, bounding volumes, etc

class Primitive {

public:

virtual bool Intersect(const Ray &ray) const = 0;

}

/// Sphere primitive

class Sphere : public Primitive {

bool Intersect(const Ray &ray) const;

}

// Triangle primitive

class Triangle : public Primitive {

bool Intersect(const Ray &ray) const;

}

6

Others:

• Torus

• Cone / Cylinder

• Box / Rectangle

• Extrusions

• Surfaces of revolution

• Metaballs

• Iso-surface

• Spline surfaces

• Subdivision surfaces

Page 7: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Others:

• Torus

• Cone / Cylinder

• Box / Rectangle

• Extrusions

• Surfaces of revolution

• Metaballs

• Iso-surface

• Spline surfaces

• Subdivision surfaces

Ray Tracing Algorithm

/// Abstract Primitive class defining properties which are required for our ray tracer.

/// For now, it specifies just ray-object intersection routine, but can be extended to

/// support shadow rays, bounding volumes, etc

class Primitive {

public:

virtual bool Intersect(const Ray &ray) const = 0;

}

/// Sphere primitive

class Sphere : public Primitive {

bool Intersect(const Ray &ray) const;

}

// Triangle primitive

class Triangle : public Primitive {

bool Intersect(const Ray &ray) const;

}

7

Note! We can’t use inheritance, hence we

are restricted to a single primitive

Page 8: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Making Ray Tracing Faster

faster rays

packets (less overhead per ray, cache coherence)

CPU optimizations

fewer rays

adaptive super-sampling (less samples)

faster ray-primitive intersection tests

fewer ray-primitive intersection tests

acceleration structures

8

Page 9: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Which Operation Most Costly?

foreach frame

foreach pixel

foreach sample

generate ray

intersect ray with objects

shade intersection point

9

Page 10: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Acceleration Structures

foreach frame

foreach pixel

foreach sample

generate ray

traverse ray through acceleration structure

shade intersection point

change O(n) to O(log n), n – objects in scene

intersecting ray with structure primitive must be cheap

10

Page 11: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Acceleration Structures

11

Page 12: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Acceleration Structures

Grid

12

Page 13: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Acceleration Structures

Grid

Octree

13

Page 14: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Acceleration Structures

Grid

Octree

14

Page 15: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Acceleration Structures

Grid

Octree

KD tree (K-dimensional)

15

Page 16: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Acceleration Structures

Grid

Octree

KD tree (K-dimensional)

16

Page 17: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Acceleration Structures

Grid

Octree

KD tree (K-dimensional)

BSP tree (Binary Space Partitioning)

17

Page 18: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Acceleration Structures

Grid

Octree

KD tree (K-dimensional)

BSP tree (Binary Space Partitioning)

BVH (Boundary Volume Hierarchy)

18

Page 19: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Acceleration Structures

Grid

Octree

KD tree (K-dimensional)

BSP tree (Binary Space Partitioning)

BVH (Boundary Volume Hierarchy)

19

Page 20: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Acceleration Structures

Grid

Octree

KD tree (K-dimensional)

BSP tree (Binary Space Partitioning)

BVH (Boundary Volume Hierarchy)

20

Page 21: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Acceleration Structures

Grid

Octree

KD tree (K-dimensional)

BSP tree (Binary Space Partitioning)

BVH (Boundary Volume Hierarchy)

21

Page 22: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

BVH Traversal - Idea 22

Page 23: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

BVH Traversal - Idea 23

Page 24: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

BVH Traversal - Idea 24

Page 25: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

BVH Traversal - Idea 25

Page 26: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

BVH Traversal - Idea 26

Page 27: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

BVH Traversal - Idea 27

Page 28: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

BVH Traversal - Idea 28

Page 29: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

BVH Traversal - Idea 29

Page 30: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

BVH Traversal - Idea 30

Page 31: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

BVH Traversal - Pseudocode

description is recursive, but

TPs have small stack memory, so manage it

ourselves

code will run faster

int stack[32]; // holds node IDs to traverse

int sp = 0; // stack pointer into the above

31

Page 32: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

BVH Traversal - Pseudocode

current_node = root

while(true) {

if( ray intersects current_node ) {

if( current_node._is_interior() ) {

stack._push( current_node._right_child_id() )

current_node = current_node._left_child_id()

continue

}

else

intersect all triangles in leaf

}

if( stack._is_empty() )

break

current_node = stack._pop()

}

32

Page 33: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

BVH Traversal - Optimizations

traverse closer child first

don’t traverse subtree if closer hit found

33

Page 34: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

Axis Aligned Bounding Box

Let’s try to derive an intersection test

Box representation?

34

Page 35: CS 6958 Lecture 7 Triangles, BVHcs6958/slides/Lec8_2.pdf · 2014-02-04 · /// Sphere primitive class Sphere : public Primitive { bool Intersect(const Ray &ray) const; } // Triangle

End

35