44
第第第 Ray Tracing

第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Embed Size (px)

Citation preview

Page 1: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

第五课 Ray Tracing

Page 2: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Overview of the Section

Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Page 3: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Object-ordering rendering

Each polygon or triangle is processed in turn and its visibility is determined using algorithms such as Z-buffer

After determining its visibility, the polygon or triangle is projected onto the viewing plane

Polygon or triangle is shaded using Phong Model\

OpenGL, rasterization ( 光栅化 )

Page 4: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Object-ordering rendering

Advantage Fast rendering can be implemented on

hardware Disadvantage

Difficult to model lighting effects such as reflection or transparency

Difficult to model lighting effects are caused by the interaction of objects, i.e. shadows

Assumption Light travels from visible objects towards the

eye

Page 5: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Ray tracing

Question Do light proceed from the eye to the light

source or from light source to the eye? To answer the question, it’s important to

understand Light rays do travel in straight lines Light rays do not interfere if they cross Light rays travel from the light source to the

eye but the physics are invariant under path reversal

Page 6: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Ray tracing

Image order rendering Start from pixel, what do you see through

this pixel Allows to add more realism to the

rendered scene by allowing effects such as Shadows Transparency Reflections

Page 7: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

How ray-tracing works

Looks through each pixel (e.g. 640 x 480) Determines what eye sees through pixel

Trace light ray: eye -> pixel (image plane) -> scene

If a ray intersect any scene object? Yes, render pixel using object color No, it uses the background color

Automatically solves hidden surface removal problem

Page 8: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Ray misses all objects

Page 9: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Ray hits an object

Page 10: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Ray hits an object

Ray hits object: Check if hit point is in shadow, build secondary ray towards light sources

Page 11: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Ray hits an object

If shadow ray hits another object before light source: first intersection point is in shadow of the second object. Otherwise, collect light contributions

Page 12: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Reflected Ray

When a ray hits an reflective object, a reflection ray is generated which is tested against all of the object in the scene

Page 13: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Shadows and Reflections

Page 14: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Transparency

If intersected object is transparent, transmitted ray is generated

Page 15: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Transparency

Page 16: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Recursion

Reflected rays can generated other reflected rays

Page 17: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Recursion

Scene with one layer of reflection

Page 18: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Recursion

Scene with two layer of reflection

Page 19: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Ray Tree

Reflective/transmitted rays are continually generated until ray leaves the scene without hitting any object or a preset recursion level has been reached

Page 20: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Ray-Object Intersections

Express ray as equation (origin is eye, pixel determines direction) R0 = [x0, y0, z0] – origin of ray Rd = [xd, yd, zd] – direction of ray

Define parametric equation of ray R (t) = R0 + Rd * t, with t > 0.0

Express all objects mathematically Ray tracing idea:

Put ray mathematical equation into object equation Determine if real solution exists Object with smallest hit time is object seen

Page 21: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Ray-Object Intersection

Dependent on parametric equation of object

Ray-Sphere Intersections Ray-Plane Intersections Ray-Polygon Intersections Ray-Box Intersections Ray-Quadric Intersections (cylinders, c

ones, ellipsoids, paraboloids)

Page 22: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Accelerating Ray Tracing

Ray tracing is very time-consuming because of intersection calculations

Each intersection requires many floating float point operations

Solutions Use faster machines Use specialized hardware, especially parallel

processors Speed up computations by using more

efficient algorithms Reduce the number of ray-object

computations

Page 23: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Reducing Ray-Object Intersections

We will look at spatial ( 空间的 ) data structures Hierarchical ( 层次 ) bounding volumes ( 包围

盒 ) Uniform Grids ( 均匀格 ) Quadtree/Octree ( 四叉树 / 八叉树 ) K-d Tree/BSP tree ( 空间二分树 )

Good spatial data structures could speed up ray tracing by 10-100 times

Page 24: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Ray intersection

Page 25: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Ray Intersection

Page 26: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Bounding Volume

Wrap (包住 ) things with bound volumes that hard to test for intersection in things that are easy to test. Ray can’t hit the real object unless it hits the bounding volume

Bounding Box and Bounding Sphere

Page 27: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Bounding Volume

Page 28: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Hierarchical Bounding Volume

HBV ( 层次包围盒 ) Limitation of bounding volume

Still need to test ray against every object, O(n) time complexity, where n is the number of objects

A natural extension to bounding volumes is a HBV

Page 29: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

HBV

Given the bounding volumes of the objects in the scene, a tree data structure of bounding volumes is created with the bounding volume of the objects at the leaves

Each interior node v of HBV corresponds to the bounding volumes that completely encloses the bounding volumes of all the children node of v

Page 30: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

HBV

An example Several triangles (right) HBV (below)

Page 31: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

HBV

Page 32: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

HBV

Page 33: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

HBV

Works well if you use good bounding volume and hierarchy

Should give O(logn) time complexity

Can have mulitple classes (box, sphere) of bounding volumes

Page 34: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Uniform Grids ( 均匀网格 )

Data structure: a 3D array of cells that tile space Each cell lists all primitives which

intersect with that cell

Page 35: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Uniform Grids

Intersection testing Start tracing at cell where ray begins Step from cell to cell, searching for the

first intersection cell At each cell, test for intersection with

all primitives intersected with that cell If there is an intersection, return the

closest one

Page 36: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Uniform Grids

Advantage Easy to construct and easy to traverse

Disadvantage Uniform grids are a poor choice if the world i

s non-homogeneous ( 不均匀 ). Many polygons will cluster in a small space

How many cells to use? Too few -> many objects in a cell -> slow Too many -> many empty cells to traverse -> slow

and large storage Non-uniform spatial division is better

Page 37: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

QuadTree/Octree

QuadTree The 2D generalization of binary tree Node is a square Recursively split square into four equal sub-squa

re Stop when leaves get “simple enough”

Octree 2D generalization of QuadTree Node is cube, recursively split into 8 equal sub-c

ubes

Page 38: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Construction of Octree

First, enclose the entire scene in a minimal axis-aligned box

Built recursively in a top-down fashion, and stops recursion when criterion is fulfilled

These criteria include: a maximum number of recursion level has been reached, or that there is fewer than a threshold number of primitives in a node. ( 递归终止条件:已达到最大层次、或包围盒内物体已很少 )

Page 39: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

BSP tree

Page 40: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Construction of BSP

Similar to octree, BSP is also constructed hierarchically in top-down fashion A splitting plane (perpendicular to axis

x or y or z) is selected to subdivide a current leaf node into two equal size sub-nodes

Page 41: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

BSP tree Examples

Page 42: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

BSP tree Examples

Page 43: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

KD-tree

Difference between BSP and KD tree Distinguished by the position of the

splitting plane: in BSP tree, the plane always lies at the mid-point of the current node, but the KD tree has arbitrarily position of the plane

Thus, any BSP tree is a KD tree, but not vice versa

Page 44: 第五课 Ray Tracing. Overview of the Section Why Ray Tracing? What is Ray Tracing? How to tracing rays? How to accelerating ray-tracing?

Thanks