0% found this document useful (0 votes)
21 views54 pages

Ray Tracing

Uploaded by

ricky27278
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views54 pages

Ray Tracing

Uploaded by

ricky27278
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

CS 4204 Computer Graphics

Introduction to Ray Tracing

Adapted from notes by Yong Cao


Virginia Tech

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?

• The more the better.


• Infinite reflections at the limit.
Stages of raytracing
• Setting the camera and the image plane
• Computing a ray from the eye to every
pixel and trace it in the scene
• Object-ray intersections
• Shadow, reflected and refracted ray at
each intersection
Setting up the camera
Image parameters
• Width 2W,
2W, Height 2H
Number of pixels nCols x nRows
• Camera coordinate system (eye,
(eye, u,v,n)
u,v,n)
• Image plane at -N
Pixel coordinates in camera
coordinate system
• Pixel P(r,c) has coordinates in camera space:

22
Ray through pixel
• Pixel location

• Ray through pixel:

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)

• Plane is defined by [A, B, C, D]


• Ax + By + Cz + D = 0 for a point in the plane
• Normal Vector, N = [A, B, C] (unit vector)
• A2 + B2 + C2 = 1
What Can Happen?
N • Rd > 0
N • Rd = 0

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)

Note: The normal vector of the plane should usually


(except for backface removal) be for the surface facing
the ray. If it isn't then it should be reversed before
illumination is calculated.

If N • Rd > 0 then use N = -[A, B, C]


Ray/Plane Summary
Intersection point:
(xi, yi , zi ) = (xo + xd*ti, yo + yd*ti, zo + zd*ti)

1. Calculate N • Rd and compare it to zero.


2. Calculate ti and compare it to zero.
3. Compute intersection point.
4. Compare N • Rd to zero and reverse normal if
appropriate
Polygon Intersection
• Step 2 : Check to see if intersection point
is inside the polygon

A polygon is defined by a set of p points:


Gn = {(xn, yn, zn) | n = 0, 1, ..., (p-1)}

The polygon is in a plane


Ax + By + Cz + D = 0
Normal Vector, N = [A, B, C]
Ray/polygon concept
1. Find the intersection of the ray with the plane of the
polygon.

2. Throw away the coordinate of each vertex for which the


corresponding normal component is of the greatest
magnitude (dominant coordinate).
You now have a 2-D polygon defined by the set of vertices
{(un, vn) | n = 0, 1, .., p-1}

3. Translate the polygon so that the intersection point of the


ray with the plane of the polygon goes to the origin of the
u,v system. Call these translated vertices
{(u´n, v´n) | n = 0, 1, .., p-1}

4. Determine whether the origin is within the 2D polygon.


Ray/Polygon (cont.)
Y
V

X
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

V11 Find areas


nV1
V1

n c
nV0 a
p V00
b

nV2
V2 V22

n = anV0 + bnV1
V1 + cnV2
area(V0V1V2)
Ray/Sphere 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)

Sphere's surface is defined by the set of points ( xs, ys,


zs) satisfying the equation:

(xs - xc)2 + (ys - yc)2 + (zs - zc)2 - rs2 = 0

Center of sphere: (xc, yc, zc)


Radius of sphere: rs
Possible cases of ray/sphere
intersection

1. Ray intersects sphere


1 2 twice with t>0
2. Ray tangent to sphere
3. Ray intersects sphere
with t<0
4. Ray originates inside
3 sphere
4
5. Ray does not intersect
5 sphere
Solving for t

Substitute the basic ray equation x = xo + xd*t


y = yo + yd*t
z = zo + zd*t
into the equation of the sphere:

(x0 + xdt - xc)2 + (y0 + ydt - yc)2 + (z0 + zdt - zc)2 - rs2 = 0

This is a quadratic equation in t: At2 + Bt + C = 0 where

A = xd2 + yd2 + zd2


B = 2[xd(x0 - xc) + yd(y0 - yc) + zd(z0 - zc)]
C = (x0 - xc)2 + (y0 - yc)2 + (z0 - zc)2 - rs2

Note: A=1
Relation of t to intersection

We want the smallest positive t - call it ti

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 )

Unit vector normal to the surface at this point is


N = [(xi - xc) / rs, (yi - yc) / rs, (zi - zc) / rs]

If the ray originates inside the sphere, N should be


negated so that it points back toward the center.

N
N
Ray/Sphere Intersection Summary

1. Calculate A, B and C of the quadratic


2. Calculate discriminant (If < 0, then no intersection)
3. Calculate t0
4. If t0 < 0, then calculate t1 (If t1 < 0, no intersection point
on ray)
5. Calculate intersection point
6. Calculate normal vector at point

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

for each pixel (c,r) on screen


determine ray rc,r from eye through pixel
color(c,r) = raytrace(
raytrace( r c,r )
end for
end
function raytrace(r)
raytrace(r)
find closest intersection P of ray with objects
clocal = Sum(shadowRays
Sum(shadowRays(P,
(P,Lighti
Lighti))
))
cre = raytrace(
raytrace(r re)
cra = raytrace(
raytrace(r ra)
return c = clocal+
clocal+kre*cre+ k ra*cra
end
Performance of ray tracing
• 1280x1024 image with 10 rays/pixel
• 1000 objects (triangle, CSG, NURBS)
• 3 levels recursion

39321600000 intersection tests


100000 tests/second -> 109 days!
Must use an acceleration method!
Bounding volumes
• Use simple shape for quick test, keep
a hierarchy
Space Subdivision
• Break your space into pieces
• Search the structure linearly
Parallel Processing
• You can always throw more processors at it.
• Ray tracing is “embarrassingly parallel”
parallel”
Advanced concepts
• Participating media
• Transculency
• Sub-surface scattering (e.g. Human skin)
• Photon mapping
Radiosity
• An alternative to ray-casting for
photorealistic CG scenes
• “The rate at which energy leaves a
surface”
• The sum of the rates at which the surface
emits (light) energy and reflects or
transmits it from that surface or other
surfaces.
Radiosity concept
• Divide objects into patches.
• Compute simultaneous equations that describe how
one patch’
patch’s energy emissions affect other patches
• Precomputed
• View independent!
• Problems
• Missing specular reflection.
• Change in scene means expensive recomputation.
recomputation.
Radiosity example 1
Radiosity example 2

You might also like