Ray Tracing
Ray Tracing
1
Raytracing (Picture from Povray.org)
Raytracing (Picture from Povray.org)
Raytracing (Picture from Povray.org)
Raytracing (Picture from Povray.org)
Raytracing (Picture from Povray.org)
Raytracing (Picture from Povray.org)
The Basic Idea
• Simulate light rays from light source to eye
Eye Light
d ray Incid
ent r
ef l ecte ay
R
Surface
“Forward” Ray-Tracing
• Trace rays from light
• Lots of work for little return
Light
Image Light Rays
Plane
Eye
Object
Scene
SA shiny,
transparent
SB,SD diffuse,
opaque
Light
SC shiny,
opaque SC
SD
Eye
SA
Image Plane SB
Three sources of light
The
The light
light that
that point
point P
PAA emits
emits to
to the
the eye
eye comes
comes from:
from:
Light
light
light sources
sources
other
other objects
objects (reflection)
(reflection) SC
other SD
other objects
objects (refraction)
(refraction)
Eye
PA
SA shiny, transparent
SB,SD diffuse,opaque SA
SC shiny, opaque
SB
Directly from light source
Local
Local illumination
illumination model:
model: SA shiny, transparent
SB,SD diffuse,opaque
II == IIaa++IIdiff
diff++IIspec
spec SC shiny, opaque
Light
SC
SD
Eye
PA
SA
SB
Reflection
What
What is
is the
the color
color that
that is
is reflected
reflected to
to P
PAA ?? SA shiny, transparent
SB,SD diffuse,opaque
The
The color
color of
of P
PCC..
SC shiny, opaque
What
What is
is the
the color
color of
of P
PCC ?? Light
SC
SD
Eye Pc
n
PA
SA
SB
Reflection
What
What is
is the
the light
light that
that is
is reflected
reflected to
to P
PAA ?? SA shiny, transparent
SB,SD diffuse,opaque
The
The color
color of
of P
PCC .. as
as viewed
viewed by
by P
PAA
SC shiny, opaque
What
What is
is the
the color
color of
of P
PCC reflected
reflected towards
towards P
PAA?? Light
Just
Just like
like P
PAA ::
SC
raytrace
raytrace P
PCC i.e
i.e compute
compute the
the SD
Eye
three
three contributions
contributions from
from
1. PA
1. Light
Light sources
sources
2.
2. Reflection
Reflection SA
3.
3. refraction
refraction
SB
Refraction
Transparent
Transparent materials
materials SA shiny, transparent
SB,SD diffuse,opaque
SC shiny, opaque
How
How do
do you
you compute
compute the
the refracted
refracted Light
contribution?
contribution?
SC
You
You raytrace
raytrace the
the refracted
refracted ray.
ray. SD
1.
1. Lights
Lights Eye
2.
2. Reflection
Reflection PA
3.
3. Refraction
Refraction
SA
SB
Three sources of light together
The
The color
color that
that the
the pixel
pixel is
is assigned
assigned comes
comes SA shiny, transparent
from:
from: SB,SD diffuse,opaque
light
light sources
sources SC shiny, opaque
other
other objects
objects (reflection)
(reflection) Light
other
other objects
objects (refraction)
(refraction)
SC
ItIt is
is more
more convenient
convenient to to trace
trace the
the rays
rays SD
from
from the
the eye
eye to
to the
the scene
scene (backwards)
(backwards)
Eye PA
SA
SB
Backwards Raytracing Algoritm
•• For
For each
each pixel
pixel construct
construct aa ray:
ray:
eye
eye pixel
pixel
raytrace(
raytrace( ray
raytrace( ray ))
PP == closest
closest intersection
intersection
color_local
color_local ==
ShadowRay(light1,
ShadowRay(light1, P)+… P)+…
P)+…
++ ShadowRay(
ShadowRay ( lightN,
lightN,
ShadowRay(lightN,
P)
P)
color_reflect
color_reflect ==
raytrace(
raytrace (reflected_ray ))
raytrace(reflected_ray
color_refract
color_refract ==
raytrace(
raytrace (refracted_ray ))
raytrace(refracted_ray
color
color == color_local
color_local
++ kkre * color_reflect
re*color_reflect
++ kkra * color_refract
ra*color_refract
return(
return( color
color ))
How many levels of recursion do we
use?
22
Ray through pixel
• Pixel location
23
Polygon Intersection
• Want to know: at what point (p
(p) does ray intersect
polygon?
• Compute lighting, reflected rays, shadowing from
that point
rd
ro <?, ?, ?>
(t = ???)
Polygon Intersection
• Step 1 : Intersect with plane
( Ax + By + Cz + D = 0 )
Plane normal
n = <A, B, C>
p
rd
ro
Ray/Plane Intersection
• Ray is defined by R(t) = Ro + Rd*t where t > 0
• Ro = Origin of ray at (xo, yo, zo)
• Rd = Direction of ray [xd, yd, zd] (unit vector)
Ro
Ro
t<0
t>0
Ray/Plane (cont.)
Substitute the ray equation into the plane equation:
A(xo + xdt) + B(yo + ydt) + C(zo +zdt) + D = 0
Solve for t:
t = -(Axo + Byo + Czo + D) / (Axd + Byd + Czd)
t = -(N • Ro + D) / (N • Rd)
X
U
V´
U´
Ray/Polygon Algorithm
num_crossings = 0; To determine whether the
sign_holder = sign(v´0); origin is within a 2D polygon,
we only need to count the
number of times the polygon’s
for(a=0; a < p; a++){ edges cross the positive u´-axis
b = (a+1)% p; as we walk around the polygon.
next_sign_holder = sign(v´b) Odd number crossings: origin is
if (sign_holder != next_sign_holder) within.
if (u´a > 0 and u´b > 0)
num_crossings = num_crossings + 1;
else if (u´a > 0 or u´b > 0){
int = u´a - v´a (u´b - u´a) / (v´b - v´a )
if(int > 0)
num_crossings = num_crossings + 1;
}
sign_holder = next_sign_holder;
}
if(num_crossings % 2 == 1) ray intersects polygon
Polygon Normals
• Could use plane normals (flat shading)
• Better to interpolate from vertices
n c
nV0 a
p V00
b
nV2
V2 V22
n = anV0 + bnV1
V1 + cnV2
area(V0V1V2)
Ray/Sphere Intersection
(x0 + xdt - xc)2 + (y0 + ydt - yc)2 + (z0 + zdt - zc)2 - rs2 = 0
Note: A=1
Relation of t to intersection
t0
t1 Discriminant = 0
t0
t0
" !B ! B2 ! 4C %
t1 t 0 = $$ '
'
# 2 &
t1
" !B + B2 ! 4C %
t1 = $$ '
'
Discriminant < 0 # 2 &
Actual Intersection
Intersection point, (xi, yi , zi ) =
(xo + xd*ti, yo + yd*ti, zo + zd*ti )
N
N
Ray/Sphere Intersection Summary
Helpful pointers:
• Precompute rs2
• Precompute 1/ rs
• If computed t is very small then, due to rounding error,
you may not have a valid intersection
Shadow ray
• For each light intersect shadow ray with
all objects.
• If no intersection is found Lights
apply local illumination
at intersection
• If in shadow no contribution
Reflected ray
• Raytrace the reflected ray
Rayrf(t)
N a
a
Ray(t) P
Refracted ray
• Raytrace the refracted ray
Snell’
Snell’s law
N
Add all together
• color(r,c) = color_shadow_ray + Kf*
Kf*color_rf +
Kr*color_rfa
Kr*color_rfa
Summary: Raytracing
• Recursive algorithm
Function Main