Download presentation source

Download Report

Transcript Download presentation source

Advanced Computer Graphics
Lecture 2: Ray Tracing
David Luebke
[email protected]
http://www.cs.virginia.edu/~cs551dl
David Luebke
7/27/2016
Recursive Ray Tracing
Idea dates back to Descartes (1637)
 In graphics: Turner Whitted (1980)

– Used recursive ray tracing as general
framework for:
Hidden surface problem
 Shadow computation
 Reflection/refraction

– Much work in the 80’s, then fell into
academic disfavor (Turner’s old joke)
David Luebke
7/27/2016
Recursive Ray Tracing
David Luebke
7/27/2016
Recursive Ray Tracing
Pixel
Image
Plane
David Luebke
7/27/2016
Recursive Ray Tracing
“Direct” Ray To Eye
David Luebke
7/27/2016
Recursive Ray Tracing
“Indirect” Ray To Eye
David Luebke
7/27/2016
Recursive Ray Tracing
Physically, we’re interested in path
of light rays from light source to eye.
 In practice, we trace rays backwards
from eye to source (Why?)

David Luebke
7/27/2016
Recursive Ray Tracing
Physically, we’re interested in path
of light rays from light source to eye.
 In practice, we trace rays backwards
from eye to source (Why?)

– Computational efficiency: we want the
finite subset of rays that leave source,
bounce around, and pass through eye
– Can’t predict where a ray will go, so
start with rays we know reach eye
David Luebke
7/27/2016
Basic Algorithm

Function TraceRay()
Recursively trace ray R and return resulting color C
– If R intersects any objects then
Find nearest object O
 Find local color contribution
 Spawn reflected and transmitted rays, using
TraceRay() to find resulting colors
 Combine colors; return result

– else

return background color
David Luebke
7/27/2016
Basic Algorithm: Code
Object allObs[];
Color image[];
RayTraceScene()
allObs = initObjects();
for (Y  all rows in image)
for (X  all pixels in row)
Ray R = calcPrimaryRay(X,Y);
image[X,Y] = TraceRay(R);
display(image);
David Luebke
7/27/2016
Basic Algorithm: Code
Color TraceRay(Ray R)
if rayHitsObjects(R) then
Color localC, reflectC, refractC;
Object O = findNearestObject(R);
localC = shade(O,R);
Ray reflectedRay = calcReflect(O,R)
Ray refractedRay = calcRefract(O,R)
reflectC = TraceRay(reflectedRay);
refractC = TraceRay(refractedRay);
return localC  reflectC  refractC
else return backgroundColor
David Luebke
7/27/2016
Refining the
Basic Algorithm
Color TraceRay(Ray R)
if rayHitsObjects(R) then
Color localC, reflectC, refractC;
Object O = findNearestObject(R);
localC = shade(O,R);
Ray reflectedRay = calcReflect(O,R)
Ray refractedRay = calcRefract(O,R)
reflectC = TraceRay(reflectedRay);
refractC = TraceRay(refractedRay);
return localC  reflectC  refractC
else return backgroundColor
David Luebke
7/27/2016
Ray-Object Intersection
Given a ray and a list of objects, what
(if any) objects does the ray intersect?
 Query: Does ray R intersect object O?

– How to represent ray?
– What kind of object?
Sphere
 Polygon
 Box
 General quadric

David Luebke
7/27/2016
Representing Rays
How might we represent rays?
 We represent a ray parametrically:

– A starting point O
– A direction vector D
– A scalar t

Why?
t<0
R = O + tD
O
t=1
D
David Luebke
7/27/2016
t>1
Ray-Sphere Intersection

Ray R = O + tD
x = Ox + t * Dx
y = Oy + t * Dy
z = Oz + t * Dz

Sphere at (l, m, n) of radius r is:
(x - l)2 + (y - m)2 + (z - n)2 = r 2

Substitute for x,y,z and solve for t…
David Luebke
7/27/2016
Ray-Sphere Intersection

Works out as a quadratic equation:
at2 + bt + c = 0
where
a = Dx2 + Dy2 + Dz2
b = 2Dx (Ox - l) + 2Dy (Oy - m) + 2Dz (Oz - n)
c = l2 + m2 + n2 + Ox2 + Oy2 + Oz2
- 2(l Ox + m Oy + n Oz + r2)
David Luebke
7/27/2016
Ray-Sphere Intersection
If solving for t gives no real roots: ray
does not intersect sphere
 If solving gives 1 real root r, ray
grazes sphere where t = r
 If solving gives 2 real roots (r1, r2),
ray intersects sphere at t = r1 & t = r2

– Ignore negative values
– Smallest value is first intersection
David Luebke
7/27/2016
Ray-Sphere Intersection
Find intersection point Pi = (xi, yi, zi)
by plugging t back into ray equation
 Find normal at intersection point by
subtracting sphere center from Pi
and normalizing:

 xi  l yi  m zi  n 
N 
,
,

r
r 
 r
(When might we need the normal? When not?)
David Luebke
7/27/2016
Ray-Polygon Intersection
Polygons are the most common model
representation (Why?)
 Basic approach:

– Find plane equation of polygon
– Find intersection of ray and plane
– Does polygon contain intersection point?
David Luebke
7/27/2016
Ray-Polygon Intersection

Find plane equation of polygon:
ax + by + cz + d = 0

y
How?
N
N = [a, b, c]
d = N  P1
P2
P1
(How to find N ?)
David Luebke
d
7/27/2016
x
Ray-Polygon Intersection

Find intersection of ray and plane:
t = -(aOx + bOy + cOz + d) / (aDx + bDy + cDz)

Does poly contain intersection point Pi ?
– Book’s algorithm:
Draw line from Pi to each polygon vertex
 Measure angles between lines (how?)
 If sum of angles between lines is 360°,
polygon contains Pi

– Slow — better algorithms available
David Luebke
7/27/2016
Ray-Box Intersection
Often want to find whether a ray hits
an axis-aligned box (Why?)
 One way:

– Intersect ray with pairs of parallel
planes that form box
– If intervals of intersection overlap, the
ray intersects the volume.
David Luebke
7/27/2016