CSE 574 Parallel Processing

Ray Tracing
Ray Tracing

Highly realistic

Considerable computation time
Ray Tracing
prp
Ray Tracing
R3
T2
T3
R2
R4
R1
T1
T4
R1
R2
prp
R3
T1
T2
T3
R4
T4
Ray Tracing Algorithm
main()
{
Select prp
Shade ( object, ray, point, normal, depth)
{
color = ambient term
for (each light)
Sray = ray to light from point
if (N.L>0)
compute how much light is blocked by opaque and
transparent surfaces,
use it to scale diffuse and specular terms and
add term to color
for (each scan line)
for (each pixel in scan line)
determine ray from prp through pixel
pixel = Trace (ray, 1)
}
Trace (ray, depth)
{
determine closest intersection of ray
with an object
if (depth < maxdepth)
if (object is reflective)
Rray = ray in reflection direction from point
Rcolor = Trace (Rray, depth+1)
scale Rcolor by specular coeff. and add to color
if (object hit)
compute normal at intersection
return Shade( closest object hit, ray,
intersection, normal, depth)
else
return background_value
if (object is transparent)
Tray = ray in refraction direction from point
if (total internal reflection does not occur)
Tcolor = Trace (Tray, depth+1)
scale Tcolor by transmission coeff. and
add to color
}
return color
}
Ray Tracing
ambient light:
ka Ia
diffuse light:
kd (N.L)
specular light: ks (H.N)ns
reflected light
R
L
shadow ray
T
qr
hr
N
qi
specular reflection direction:
R = u-(2u.N)N
H
hi
T = [(hi/hr) cos qi - cos qr] N - (hi/hr)L
u
incoming ray
(viewing direction V = -u)
cos qr = √1 - (hi/hr)2 (1-cos2 qi )
Ray Tracing
Ray equation
P = P0 + s.u
y
ray path
u
P0
Ppix
x
prp
z
P: any point along the ray
P0: initial position vector
s: distance of P to P0
u: unit direction vector
Ppix – Pprp
u = -----------------| Ppix – Pprp |
Initially P0 is Ppix or prp.
Update P0 and u at each intersection point
on the ray with a surface.
Ray Tracing
P: intersection point of the sphere along the
ray
Pc: center of sphere
r: radius of sphere
y
P
u
|P - Pc |2 – r2 = 0
r
P0
|P0 + s.u - Pc |2 – r2 = 0
Pc
x

z
s = u.P  (u.P)2 - |P|2+r2
where P = Pc – P0
If discriminant < 0
ray does not intersect sphere
Otherwise
calculate P using P = P0 + s.u
Ray Tracing
If the ray does not intersect the sphere,
eliminate the polyhedra
Otherwise
do the following:
y
u
P0
Identify front faces using: u.N<0
Pc
x
z
For each face that satisfies u.N<0:
- Solve plane equation:
N.P = -D
N.(P0 + s.u) = -D
Infinite plane
u
 s = - (D+N.P)
N.u
- Perform inside-outside test to check
if the intersection is inside the polygon
Ray Tracing
Ray Tracing
Ray Tracing
Ray Tracing