Ray Tracing
Ray Tracing
Problem?
Backward Tracing
For every pixel
Construct a ray from the eye
For every object in the scene
Find intersection with the ray
Keep if closest
The Viewing Model
• Based on a simple Pinhole Camera model
Simplest lens model Perfect image if hole
Inverted image infinitely small
Similar triangles Pure geometric optics
No blurry
xres
• Screen
• Pixel resolution N
Construct Eye Coordinate System
• We can calculate the pixel positions much
more easily if we construct an eye
coordinate system (eye space) first
Known: eye position, center of interest, view-up
vector
To find out: new origin and three basis vectors
v
u
Center of interest (COI)
eye
n
U = V_up x n
u = U / |U|
Eye Coordinate System
What about v?
Knowing n and u, getting v is
easy
v
V_up u
COI
eye
n
Eye Coordinate System
What about v?
Knowing n and u, getting v is
easy
v
V_up u
COI
eye v = n xu
n
v is already normalized
Eye Coordinate System
Put it all together
L
• C’s position = e - n * d
L + u * i * W/X + v * j * H/Y
Put it all together
• We can represent the ray as a 3D parametric line
p(t) = e + t (s-e)
(now you know how to get s and e)
s
s-e e
• Typically we offset the ray by half
of the pixel width and height, i.e, cast the ray from the pixel
center
incrementing
(i,j)
(0,0)
Put it all together
• We can represent the ray as a 3D parametric line
p(t) = e + t (s-e)
(now you know how to get s and e)
s
s-e e
• Typically we offset the ray by half
of the pixel width and height, i.e, cast the ray from the pixel
center
incrementing
(i,j)
(0,0)
Ray-Sphere Intersection
• Problem: Intersect a line with a sphere
✓ A sphere with center c = (xc,yc,zc) and radius R can be
represented as:
2 2 2 2
(x-x + (y-yc) + (z-zc) - R = 0
c)
C
t
Q
Calculate Normal
• Needed for computing lighting
Q = P(t) – C … and remember Q/||Q||
C
t
Q
normal
Choose the closet sphere
• Minimum search problem
For each pixel {
form ray from eye through the pixel center
tmin = ∞
For each object {
if (t = intersect(ray, object)) {
if (t < tmin) {
closestObject = object
tmin = t
}
}
}
}
Final Pixel Color
if (tmin == ∞)
pixelColor = background color
else
pixelColor = color of object at d along ray
Y = y2
Z = z2
X = x1 X = x2
Z = z1
Y = y1
Ray-Box Intersection Test
Y = y2
Z = z2
X = x1 X = x2
Z = z1
Y = y1
Ray-Box Intersection Test
• Intersect ray with each plane
– Box is the union of 6 planes
x = x1, x = x2 Y = y2
y = y1, y = y2
z = z1, z = z2 Z = z2
X = x1 X = x2
Z = z1
Y = y1
Ray-Box Intersection Test
• Intersect ray with each plane
– Box is the union of 6 planes
x = x1, x = x2 Y = y2
y = y1, y = y2
z = z1, z = z2 Z = z2
X = x1 X = x2
Z = z1
• Ray/axis-aligned plane
is easy: Y = y1
Ray-Box Intersection Test
• Intersect ray with each plane
– Box is the union of 6 planes
x = x1, x = x2 Y = y2
y = y1, y = y2
z = z1, z = z2 Z = z2
X = x1 X = x2
Z = z1
• Ray/axis-aligned plane
is easy: Y = y1
Ray-Box Intersection Test
• Intersect ray with each plane
– Box is the union of 6 planes
x = x1, x = x2 Y = y2
y = y1, y = y2
z = z1, z = z2 Z = z2
X = x1 X = x2
Z = z1
• Ray/axis-aligned plane
is easy: Y = y1
Y = y2
Z = z2
X = x1 X = x2
Z = z1
Y = y1
Ray-Box Intersection Test
Y = y2
Z = z2
X = x1 X = x2
Z = z1
Y = y1
Ray-Box Intersection Test
1. Intersect the ray with each plane
2. Sort the intersections
Y = y2
Z = z2
X = x1 X = x2
Z = z1
Y = y1
Ray-Box Intersection Test
1. Intersect the ray with each plane
2. Sort the intersections
3. Choose intersection Y = y2
Z = z2
X = x1 X = x2
Z = z1
Y = y1
Ray-Box Intersection Test
1. Intersect the ray with each plane
2. Sort the intersections
3. Choose intersection Y = y2
X = x1 X = x2
Z = z1
Y = y1
Ray-Box Intersection Test
1. Intersect the ray with each plane
2. Sort the intersections
3. Choose intersection Y = y2
X = x1 X = x2
Z = z1
Y = y1
Ray-Box Intersection Test
1. Intersect the ray with each plane
2. Sort the intersections
3. Choose intersection Y = y2
X = x1 X = x2
of the box Z = z1
Y = y1
Ray-Box Intersection Test
1. Intersect the ray with each plane
2. Sort the intersections
3. Choose intersection Y = y2
X = x1 X = x2
of the box Z = z1
Y = y1
Ray-Box Intersection Test
1. Intersect the ray with each plane
2. Sort the intersections
3. Choose intersection Y = y2
X = x1 X = x2
of the box Z = z1
• We can do more Y = y1
Ray-Box Intersection Test
1. Intersect the ray with each plane
2. Sort the intersections
3. Choose intersection Y = y2
X = x1 X = x2
of the box Z = z1
• We can do more Y = y1
efficiently
Only Consider 2D for Now
• if a point (x,y) is in the box, then (x,y) in
[x1 , x2] x [y1, y2]
y = y1
y = y1
x = x1 x = x2
The Principle
• Assuming the ray hits the box boundary lines at
intervals [txmin,txmax], [tymin,tymax], the ray
hits the box if and only if the intersection of the
two intervals is not empty
txmax
txmin
tymin tymax
tymin
28
Pseudo Code
txmin =(x1 - ex )/Dx
//assume Dx >0
txmax =(x2 - ex )/Dx
tymin = (y1 - ey )/Dy
tymax = (y2 - ey )/Dy //assume Dy >0
if (txmin > tymax) or (tymin > txmax)
return false
else
return true
29
Pseudo Code
30
6
Now Consider All Axis
• We will calculate t1 and t2 for each axis (x,
y, and z)
• Update the intersection interval as we
compute t1 and t2 for each axis t2
• remember: t1
t1=(x1- px)/Dx
t2=(x2- px)/Dx
D x = x1 x = x2
p
Update [tnear, tfar]
• Set tnear = -∞ and tfar = +∞
• For each axis, compute t1 and t2
– make sure t1 < t2
– if t1 > tnear, tnear =t1
t2
– if t2 < tfar, tfar = t2
t1
D x = x1 x = x2
p
Algorithm
Set tnear = - ∞, tfar = ∞
R(t) = p + t * D
For each pair of planes P associated with X, Y, and Z do: (example uses X
planes)
if direction Dx = 0 then
if (px < x1 or px > x2)
return FALSE
else
begin
t1 = (xl - px) / Dx
t2 = (xh - px) / Dx
if t1 > t2 then swap (t1, t2)
if t1 > tnear then tnear = t1
if t2 < tfar then tfar = t2
if tnear > tfar return FALSE
if tfar < 0 return FALSE
end
Return tnear
Special Case
• Ray is parallel to an axis
– If Dx = 0 or Dy = 0 or Dz = 0
y=Y1
D x=X1 x=X2
p
Special Case
• Box is behind the eye
– If tfar < 0, box is behind
D
p
x = x1 x = x2