computer graphics 2 lecture 13: ray-tracing techniques benjamin mora 1 university of wales swansea...
TRANSCRIPT
Computer Graphics 2Lecture 13:
Ray-Tracing Techniques
Benjamin Mora 1University of Wales
Swansea
Dr. Benjamin Mora
Content
2Benjamin MoraUniversity of Wales
Swansea
• What is Ray-Tracing?
• Mathematics for ray-tracing intersections.
• Common Ray-Tracing effects.
• Other Ray-Tracing Algorithms.
• Current Ray-Tracing Technology.
What Is Ray-Tracing ?
3Benjamin MoraUniversity of Wales
Swansea
What is Ray Tracing ?
4Benjamin MoraUniversity of Wales
Swansea
• The alternative to rasterization-based methods (like OpenGL or DirectX).– Image-Order algorithm.
– Every pixel is processed separately.
• Slower than rasterization methods in the case of few primitives to render.
• Rarely used in games, widely used for realistic computer graphics.
• www.povray.org.
What is Ray Tracing ?
5Benjamin MoraUniversity of Wales
Swansea
Jaime Vives Piqueres (2004), http://www.povray.org/i/hof/office-13.jpg
What is Ray Tracing ?
6Benjamin MoraUniversity of Wales
Swansea
Jaime Vives Piqueres (2001), http://www.povray.org/i/hof/office-13.jpg
What is Ray Tracing ?
7Benjamin MoraUniversity of Wales
Swansea
viewlocation
image plane
primaryray
secondaryrays
What is Ray Tracing ?
8Benjamin MoraUniversity of Wales
Swansea
• Basic (or Naïve )Algorithm (Whitted 1980):FOR every pixel DO
FOR every primitive in the scene DO
IF there is a ray/primitive intersection
test if this is the closest intersection computed so far.
If this is the case,
keep this primitive as the closest primitive for the next tests.
• Basic Complexity: – O(number of rays*number of primitives). – Optimized algorithms can do much better (See the next lectures)!
Mathematics for Ray-Tracing intersections
9Benjamin MoraUniversity of Wales
Swansea
Parametric Ray/Plane Formulation
10Benjamin MoraUniversity of Wales
Swansea
• A ray can be described with a parametric formulation from a (starting) point and a direction
• A parametric plane can be described from one point and two vectors:
• A plane can also be defined from a point and a normal vector (here P0 and Vz).
directionVtPtP 0
yx VyVxPyxP 0,
P0
Vx
VyVz
Ray/Plane Intersection
11Benjamin MoraUniversity of Wales
Swansea
• We are looking for the point p(t) such that p(t) belong to the plane h.
• h is defined with a point o and a normal n.
• If vdir perpendicular to n, then either 0 solution, or an infinite number of solutions if p0 belongs to h (the vector p0o perpendicular to n).
p0
h
n
op(t)
vdir
Ray/Plane Intersection
12Benjamin MoraUniversity of Wales
Swansea
• Otherwise, there is only one point that is solution. We have to find out t.
• The solution comes from the fact that the vector p(t)o is perpendicular to n:
(6)
(5)
(4)
(3)
(2) 0.
(1) 0)(
0
00
0
nv
nopt
nopnpnonvt
nonvtp
nontp
ntpo
notp
dir
dir
dir
p0
h
n
op(t)
vdir
13Benjamin Mora
University of Wales Swansea
The ray intersects the cell only if max(txmin, tymin)<min(txmax, tymax).
Finding out the exit face consists of finding out the minimum of (txmax, tymax).
txmin txmax
tymin
tymax
t
tymin tymax
txmin txmax
p(t) = p0 +v•t
p0 v
Ray/Cube Intersection
Ray/Cube Intersection
14Benjamin MoraUniversity of Wales
Swansea
• A cube can be defined with 6 planes (faces).• 3 pairs of opposite faces (let’s reference them as x,y,z) .• A parametric ray p(t) intersects 2 opposite faces at 2
locations, with 2 respective parameters tmin and tmax. (let’s suppose tmin<=tmax).
• The 6 faces are intersected with 6 different parameters (txmin,txmax, tymin,tymax, tzmin,tzmax).
• The ray intersects the cube only if:Max (txmin,tymin,tzmin)< Min(txmax,tymax,tzmax).
Ray/Sphere Intersection
15Benjamin MoraUniversity of Wales
Swansea
• A sphere can be defined by a center o and a radius r.
• Looking for the points p(t) such as: norm(p(t)o)=r.
p0r
o
vdir p(t)
02 (3)
(2)
)( (1)
2200
22
220
20
22
ropvoptvt
rvtopvtpo
rotp
dirdir
dirdir
Ray/Triangle Intersections
16Benjamin MoraUniversity of Wales
Swansea
• Several ways to achieve this goal.
– Badouel’s Algorithm.– Moller’s Algorithm (not shown).– Other Algorithm (close to Segura et al. 98).
p0 vdir
p(t)
Badouel’s Algorithm
17Benjamin MoraUniversity of Wales
Swansea
• First, find the intersection p(t) between the ray and the plane containing the triangle.
• Then use the barycentric coordinates of the point to sort the intersection out (2D algorithm).
p1
p(t)
p0
p2 20100 .. pppptpp 1,0,0
)(
)(
)(
02010
02010
02010
zzzzzz
yyyyyy
xxxxxx
t
t
t
Other Ray/Triangle Algorithms
18Benjamin MoraUniversity of Wales
Swansea
• The edges of the triangles can be considered as oriented.
• The Ray should always be “on the same side” of the edges
• => Three triple products with the same sign.
Segura, R.J., Feito, F.R. Segura, R.J., Feito, F.R.: An algorithm for determining intersection segment-polygon in 3D. Computer & Graphics, Vol.22, No.5, pp.587-592, 1998.
p vdir
p(t)
p0p1
p2
Other Ray/Triangle Algorithms
19Benjamin MoraUniversity of Wales
Swansea
• There is an intersection Ray/Triangle only if:
• Fast algorithm.– Can take advantage of SIMD instructions on processor.
• But:– Only in order to detect an intersection.– Extra work must be done in order to get the tri-linear interpolating
weights (including a div) and the real intersection location if needed (but not always required).
p vdir
p(t)
p0p1
p2
vppppSignvppppSignvppppSign 022110
Triangle/Box Intersection
20Benjamin MoraUniversity of Wales
Swansea
• Why?
– Bounding boxes are used much to speed-up intersections.
• Standalone boxes.
• Spatial subdivision techniques.
– Therefore needed to construct such acceleration structures.
Triangle/Box Intersection
21Benjamin MoraUniversity of Wales
Swansea
• Solution 1: Working in the triangle plane.
• For every face of the cube:
– Find the face/triangle plane intersection.• A line!
– Clip the triangle/polygon with this line. (Sutherland ).
• Intersection occurs if result not NULL!
• Complex and Costly.
Triangle/Box Intersection
22Benjamin MoraUniversity of Wales
Swansea
• Solution 2: Use the Separating Axis Theorem. (Akenine-Moller)
• No intersection occurs between 2 convex objects if there exists a separating plane.
Triangle/Box Intersection (2D)
23Benjamin MoraUniversity of Wales
Swansea
Triangle/Box Intersection (2D)
24Benjamin MoraUniversity of Wales
Swansea
Triangle/Box Intersection (3D)
25Benjamin MoraUniversity of Wales
Swansea
• 13 planes to be tested in 3D.
• For each plane:– Compute distances to the triangle vertices
• Using the plane equation.
– Compute distances to the box vertices.– No intersection if MIN of one distance set greater
than MAX of the other distance set.
• If condition is false for all 13 cases, an intersection exists.
Triangle/Box Intersection (3D)
26Benjamin MoraUniversity of Wales
Swansea
• Plane directions are determined from:–
Where Ei are the directions of the edges of the box, and Tj the directions of the edges of the triangle. (9 cases)
– The faces of the box. (3 cases)– The triangle plane normal. (1 case)
Ei X Tj
Simple Ray-Tracing Effects
27Benjamin MoraUniversity of Wales
Swansea
Simple Ray-Tracing Effects
28Benjamin MoraUniversity of Wales
Swansea
• The main operation is to find an intersection from a given ray.
• This operation can be used (by casting secondary rays) to compute simple effects that are more
difficult to implement with OpenGL like (but fog):– Reflection.
– Refraction.
– Shadows.
– Fog. Source: Min Chen
Simple Ray-Tracing Effects
29Benjamin MoraUniversity of Wales
Swansea
• Basic Lighting model:
objectcolour
result oftracing
reflective ray
result oftracing
refractive ray++
Rin
Rfl
Rfr
refractiont
reflectionm
object
Ik
Ik
II
kmkt
Simple Ray-Tracing Effects
30Benjamin MoraUniversity of Wales
Swansea
• Shading can be done in the same way as to OpenGL.– But better methods exist.
Source: Min Chen
NV (-Rin)
L
nsdpaa RVkNLkIdfkII
R
Simple Ray-Tracing Effects
31Benjamin MoraUniversity of Wales
Swansea
• Reflection:
Simple Ray-Tracing Effects
32Benjamin MoraUniversity of Wales
Swansea
• Reflection:
N
Vincident Vreflected
ii
1,2 NVNNVV incidentincidentreflected
L
Simple Ray-Tracing Effects
33Benjamin MoraUniversity of Wales
Swansea
• Refraction:
NV (-Rin) Rfl
Lii
VNR riririfr )coscos(
R
Rfr
Snell’s Law
rirr
i
i
r
sinsin
Simple Ray-Tracing Effects
34Benjamin MoraUniversity of Wales
Swansea
• Total Internal Reflection:
NV (-Rin)
Rfr
criticalanglecritical
Simple Ray-Tracing Effects
35Benjamin MoraUniversity of Wales
Swansea
• Taking into account reflection and refraction several times (recursively) requires an exponential number of rays!
• The algorithm should stop after reaching a maximum number of consecutive reflections/refractions.
• Only reduced to some specific lighting effects.
Simple Ray-Tracing Effects
36Benjamin MoraUniversity of Wales
Swansea
Simple Ray-Tracing Effects
37Benjamin MoraUniversity of Wales
Swansea
• Fog effect (also possible with OpenGL).– Depth Cue:
– Fog:
max
maxminminmax
min
min
1
0
Dd
DdDDD
DdDd
f
D
df
e
11
fIfII foginout 1
or
Simple Ray-Tracing Effects
38Benjamin MoraUniversity of Wales
Swansea
• Super Sampling can be used in order to improve Anti-Aliasing.
non-adaptivesupersampling
stochasticsampling
adaptivesupersampling
Simple Ray-Tracing Effects
39Benjamin MoraUniversity of Wales
Swansea
Simple Ray-Tracing Effects
40Benjamin MoraUniversity of Wales
Swansea
• The final color is the average of all the sub-sampled rays.
• Uniform Super-Sampling:– Nice but not always needed.
• Adaptive Super-Sampling: – Useful for the regions with large color transitions (high frequencies),
like object boundaries.
– Trade-off between quality and speed.
• Stochastic Super-Sampling:– Take advantage of the visual system limitations.
Simple Ray-Tracing Effects
41Benjamin MoraUniversity of Wales
Swansea
• Stochastic Super-Sampling:
– Proposed by Robert L. Cook.
• Stochastic Sampling in Computer Graphics, ACM transaction on Graphics No. 5(1), January 1986, pp. 51-72.
– The ray location(s) are randomly chosen inside a pixel.
– Our visual system is less sensitive to noise than to contours.
Other Ray-Tracing Algorithms.
42Benjamin MoraUniversity of Wales
Swansea
Other Ray-Tracing Algorithms.
43Benjamin MoraUniversity of Wales
Swansea
• Pyramidal Ray-Tracing.
• Cone Tracing.
• Beam Tracing.
Pyramidal Ray-Tracing.
44Benjamin MoraUniversity of Wales
Swansea
• Proposed by:
• And improved by:
• Main Idea: Replacing Rays by Pyramids to improve aliasing.
Whitted, Turner. An Improved Illumination Model for Shaded Display. C.A.C.M. vol. 23, no. 6, June 1980, pp. 343-3~19.
J. Genetti, D. Gordon and G. Williams. Adaptive supersampling in object space using pyramidal rays. Computer Graphics Forum 17 (1998) 29-54.
viewlocation
image plane
primaryray
Pyramidal Ray-Tracing.
45Benjamin MoraUniversity of Wales
Swansea
J. Genetti, D. Gordon and G. Williams. Adaptive supersampling in object space using pyramidal rays. Computer Graphics Forum 17 (1998) 29-54.
The triangle will contribute here to 7/64 of the final pixel colour.
Pyramidal Ray-Tracing.
46Benjamin MoraUniversity of Wales
Swansea
• If a pyramid only partially intersects a graphics primitive (e.g., triangle), then the pyramid is subdivided until a given depth is reached.– The final pixel value is the (weighted) average of all the
samples inside the triangle.
– Non-intersecting pyramids keep on computing intersections with the rest of the scene.
Cone Tracing.
47Benjamin MoraUniversity of Wales
Swansea
• Proposed by:John Amanatides. Ray Tracing with Cones. Siggraph 1984, pp. 129-135.
viewlocation
image plane
primaryray
Partial intersection
Cone Tracing.
48Benjamin MoraUniversity of Wales
Swansea
• Similar to pyramidal ray-tracing, but the pyramid is now replaced by a cone.– Allows easier intersection computations.
– 1 cone per pixel, no subdivision.
• For a ray-triangle intersection:– The cone is projected onto the triangle plane.
• Circular projection.
– If there is a partial intersection detected, the colour of the triangle is weighted by the corresponding covering percentage, and computing intersections will carry on.
Cone Tracing: Result
49Benjamin MoraUniversity of Wales
Swansea
Beam Tracing.
50Benjamin MoraUniversity of Wales
Swansea
• Proposed by:
• Takes advantages of spatial coherency between rays to group them and save computations.
• Rays are distributed after Reflections and
Refractions according to the
reflection/refraction coefficients.
• Constructs a beam tree.
• Outdated.
Paul S. Heckbert and Pat Hanrahan. BEAM TRACING POLYGONAL OBJECTS. Siggraph 1984, pp. 119-127.
Current Ray-Tracing Technology
51Benjamin MoraUniversity of Wales
Swansea
Current Ray-Tracing Technology
52Benjamin MoraUniversity of Wales
Swansea
• Ray-Tracing is now used a lot for realistic rendering of scenes. – Not just Reflections/Refractions.
• Has a better complexity than OpenGL based renderings (when optimized).– Scalable.
• A simple ray representation (no cone or pyramid) is often used.
• Therefore, researchers have recently tried to hardware accelerate it.
Current Ray-Tracing Technology
53Benjamin MoraUniversity of Wales
Swansea
• 3 different approaches:– Cluster based CPU approach.
• Use SIMD instructions with packed rays.• A lot of threads in parallel.
– GPU Based.• Not very efficient because not really designed for RT.
– Hardware (FPGA) accelerated ray-tracing.• Seems to be efficient.• Need to compute a spatial subdivision tree first, which is not
really appropriate to dynamic scenes.
Tim Foley, Jeremy Sugerman. KD-Tree Acceleration Structures for a GPU Raytracer. In Proceedings of Graphics Hardware 2005.
Ingo Wald, Timothy J. Purcell, Jörg Schmittler, Carsten Benthin and Philipp Slusallek. Realtime Ray Tracing and its use for Interactive Global Illumination. Eurographics 2003 State of the Art Report.
Current Ray-Tracing Technology
54Benjamin MoraUniversity of Wales
Swansea
• The RPU unit. See Video !!!
Sven Woop, Jorg Schmittler, and Philipp Slusallek. RPU: A Programmable Ray Processing Unit for Realtime Ray Tracing. Siggraph 2005.