1
Speeding Up Ray Tracing
Images from Virtual Light Field Projectwww.cs.ucl.ac.uk/vlf
©Slides Anthony Steed 1999 & Mel Slater 2004
2
Optimisations
Limit the number of rays Make the ray test faster
• for shadow rays– the main drain on resources in there are
several lights
• for primary rays• for all rays
over 90% of the cost of raytracing is in ray-objectintersection tests
3
Primary Rays
Use a z-buffer! Instead of writing colour write and object
identifier• Easy to support in OpenGL - turn off lighting,
do flat shading and encode object id within 24bit colour
Difficult technique to use elsewhere because rays are no longer spatially coherent and evenly spaced
4
General Rays
Techniques to use• bounding volumes• hierarchical bounding volumes• space subdivision
– regular – adaptive
• ray coherence
5
Bounding Volume
Find a tight bounding volume and use it for a first reject test
If hit volume then test full object
Axis alignedBounding Box
Bounding Box
Bounding Sphere
6
Fast BV Tests
Box-Ray test• a box is three sets of parallel planes, each
set orthogonal to the other two– Calculate tnear for each of the three plane pairs
– find max of the 3 tnear
– Calculate tfar for each of the three plane pairs
– find min of the 3 tfar
• If max tnear is greater that min tfar, then the box is not intersected
7
Fast BV Tests (EG)
tnearx
tfary
tfarx
tnearx
tneary
tfarx
8
Choosing a Volume
Choice depends on the cost of the test and the fit of the shape• The “void” area can be very large
More efficient fitting shapes are possible (this is still a research area e.g. k-dops)
9
Hierarchical Bounding Volumes
A
B
C
D E
F
root
A+B
A B
C D+E F
D E
r1 r2
10
Choosing a HBV
Scene graph might not map to a decent space partitioning• group BVs based on proximity rather than
scene graph position
You would sort the HBV using a BSP tree anyway ...
11
Regular Spatial Subdivision
Regular 3D grid of “voxels”
A
B
C
12
Details on the RSS
Testing• Each voxel keeps a list of
objects that intersect this voxel
• Need for a ‘mailbox’ system where voxel also keeps track of what has already intersected it to avoid duplications.
Size of voxels• Obvious trade off on
granularity of voxels
13
Adaptive Spatial Subdivision
The octree idea (illustrated with a quadtree!)
14
Building the Octree
Recursively split the cells into eight, until each cell meets some criteria• e.g. previous diagram was “only one object
intersects each cell” Advantage is clear - cells are not
wasted on void areas
15
Tracing an Octree
Find octree cell containing start point If no intersection, find intersection (I) of
ray with cube that surround the octree Find (I) in the octree
• it is moved a little along the ray Repeat
16
kD-tree / BSP Tree
Similar to octree, but orthogonal splitting planes are chosen to provide a good search tree
Split based on balancing objectnumbers andminimising volume disparity
17
Ray Coherence Methods
Light Buffer Ray Classification Scheme
18
Light Buffer
Haines and Greenberg, 1986 Problem with shadow rays is that for
every intersection we trace and additional n rays for each light
r
l0
l1
19
Light Buffer
Enclose light in a box. Cells of box faces store objects
20
Construction - 2D Analogue
A
B
C1 - <NULL> C2 - A, B, C
C3 - A C4 - <NULL>
C1
C2
C3
C4
C
21
Ray Classification
Arvo and Kirk, 1987 Group together similar rays (rather than
similar objects) Similar rays should intersect the same
groups of objects Very elegant method
22
Ray Classification - Rays are Points
Every ray is represented as a point in 5D space (R5) (x,y,z,u,v) where (u,v) is the directional component, and (x,y,z) is the origin
(x,y,z)
(u,v)-Y
+X
This ray is (x,y,z) inside boxin direction (u,v)
on +X plane There are 6 planes-X,+X,-Y,+Y,-Z,+Z
23
Ray Classification – 5 Tasks
E is the subset of R5 containing every possible ray in the scene
E1, E2, …, En is a partition of E• formed as a 32-tree
– The 5D hypercube can be split along each of its axis to form 32 children, and so on…
For each Ei there will be a Ci - a set of objects intersected by at least one of the ray s in Ei
For any ray find the Ei to which it belongs Then intersect it with the objects in Ci (see details in the book – the important aspect to
undrestand is the idea of ray classification – similar rays will intersect similar groups of objects!).
24
Virtual Light Field Approach
Very recent development For a fixed ‘uniformly distributed’ set of rays,
find for each ray all of its intersections with objects in the environment
Each ray maintains a list of objects it intersects (and where along the ray)
Given any arbitrary ray that emerges in ray tracing, find the closest ray in the ray set, and use the rays that this intersects as a candidate set of objects.
Several complications – but the idea is clear.
25
Summary
Ray intersection is computationally expensive and we need to reduce it
Methods used fall into 3 classes• Bound the objects• Partition the object space• Partition the ray space
Ray tracing can be tremendously speeded up by appropriate use of modern hardware (see papers of Ward et al, and also UCL’s Virtual Light Field project www.cs.ucl.ac.uk/vlf ).