Ray Tracing - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

Ray Tracing

Description:

1.,5.,0.,0.,0.,.5,1.5,};yx;double u,b,tmin,sqrt(),tan();double vdot(A,B)vec A , ... tmin;return best;}vec trace(level,P,D)vec P,D;{double d,eta,e;vec N,color; ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 45
Provided by: cs6
Category:
Tags: double | ray | tracing

less

Transcript and Presenter's Notes

Title: Ray Tracing


1
Ray Tracing
2
Ray Tracing
  • What is ray tracing?
  • Follow (trace) the path of a ray of light and
    model how it interacts with the scene
  • When a ray intersects an object, send off
    secondary rays (reflection, shadow, transmission)
    and determine how they interact with the scene
  • Basic algorithm allows for
  • Hidden surface removal - Reflections
  • Multiple light sources - Transparent refractions
  • Hard shadows
  • Extensions can achieve
  • Soft shadows - Motion blur
  • Blurred reflections (glossiness) - Depth of
    field (finite apertures)
  • Translucent refractions - and more

3
Ray Tracing
  • Produces Highly realistic scenes
  • Strengths
  • Specular reflections
  • Transparency
  • Weaknesses
  • Color bleeding (diffuse reflections)
  • Time consuming
  • References
  • An Improved Illumination Model for Shaded
    Display, Turner Whitted, CACM, June 1980.
  • Distributed Ray Tracing, Cook, Porter, and
    Carpenter, Computer Graphics, July 1984, pp.
    137-145.

4
Ray traced images
5
(No Transcript)
6
(No Transcript)
7
Ray Tracing
  • Backward ray tracing
  • Traces the ray forward (in time) from the light
    source through potentially many scene
    interactions
  • Physically based
  • Global illumination model
  • Color bleeding
  • Caustics
  • Etc.
  • Problem most rays will never
  • even get close to the eye
  • Very inefficient since it computes
  • many rays that are never seen

Light
Eye
Image plane
8
Ray Tracing
  • Forward ray tracing
  • Traces the ray backward (in time) from the eye,
    through a point on the screen
  • Not physically based
  • Doesnt properly model
  • Color bleeding
  • Caustics
  • Other changes in light intensity and
  • color due to refractions and
  • non-specular reflections
  • More efficient computes only
  • visible rays (since we start at eye)
  • Generally, ray tracing refers to forward ray
    tracing

Light
Eye
Image plane
9
Ray Tracing
  • Ray tracing is an image-precision algorithm
    Visibility determined on a per-pixel basis
  • Trace one (or more) rays per pixel
  • Compute closest object (triangle,
  • sphere, etc.) for each ray
  • Produces realistic results
  • Computationally expensive

10241024, 16 rays/pixel 10 hours on a 99 MHz
HP workstation
10
Minimal Ray Tracer
  • A basic (minimal) ray tracer is simple to
    implement
  • The code can even fit on a 35 card (code
    courtesy of Paul Heckbert with a small change to
    output as a PPM file)

typedef structdouble x,y,zvecvec
U,black,amb.02,.02,.02struct sphere vec
cen,colordouble rad,kd,ks,kt,kl,irs,best,sph
0.,6.,.5,1.,1.,1.,.9, .05,.2,.85,0.,1.7,-1.,8.,-
.5,1.,.5,.2,1.,.7,.3,0.,.05,1.2,1.,8.,-.5,.1,.8,.8
, 1.,.3,.7,0.,0.,1.2,3.,-6.,15.,1.,.8,1.,7.,0.,0.,
0.,.6,1.5,-3.,-3.,12.,.8,1., 1.,5.,0.,0.,0.,.5,1.5
,yxdouble u,b,tmin,sqrt(),tan()double
vdot(A,B)vec A ,Breturn A.xB.xA.yB.yA.zB.z
vec vcomb(a,A,B)double avec A,BB.xa A.xB.y
aA.yB.zaA.zreturn Bvec vunit(A)vec
Areturn vcomb(1./sqrt( vdot(A,A)),A,black)stru
ct sphereintersect(P,D)vec P,Dbest0tmin1e30
s sph5while(s--gtsph)bvdot(D,Uvcomb(-1.,P,s-gtc
en)),ubb-vdot(U,U)s-gtrads -gtrad,uugt0?sqrt(u)
1e31,ub-ugt1e-7?b-ubu,tminugt1e-7ulttmin?best
s,u tminreturn bestvec trace(level,P,D)vec
P,Ddouble d,eta,evec N,color struct
spheres,lif(!level--)return blackif(sintersec
t(P,D))else return ambcolorambetas-gtird
-vdot(D,Nvunit(vcomb(-1.,Pvcomb(tmin,D,P),s-gtcen
)))if(dlt0)Nvcomb(-1.,N,black),eta1/eta,d
-dlsph5while(l--gtsph)if((el -gtklvdot(N,Uvun
it(vcomb(-1.,P,l-gtcen))))gt0intersect(P,U)l)col
orvcomb(e ,l-gtcolor,color)Us-gtcolorcolor.xU.
xcolor.yU.ycolor.zU.ze1-eta eta(1-dd)r
eturn vcomb(s-gtkt,egt0?trace(level,P,vcomb(eta,D,vc
omb(etad-sqrt (e),N,black)))black,vcomb(s-gtks,tr
ace(level,P,vcomb(2d,N,D)),vcomb(s-gtkd, color,vco
mb(s-gtkl,U,black))))main()puts(P3\n32
32\n255)while(yxlt3232) U.xyx32-32/2,U.z32/2-
yx/32,U.y32/2/tan(25/114.5915590261),Uvcomb(25
5., trace(3,black,vunit(U)),black),printf(".0f
.0f .0f\n",U)/minray!/
11
Minimal Ray Tracer
  • This code implements
  • Multiple spheres (with
  • different properties)
  • Multiple levels of
  • recursion
  • Reflections
  • Transparency
  • Refraction
  • One point light source
  • Hard shadows
  • Hidden surface removal
  • Phong illumination model
  • It even has a comment

typedef structdouble x,y,zvecvec
U,black,amb.02,.02,.02struct sphere vec
cen,colordouble rad,kd,ks,kt,kl,irs,best,sph
0.,6.,.5,1.,1.,1.,.9, .05,.2,.85,0.,1.7,-1.,8.,-
.5,1.,.5,.2,1.,.7,.3,0.,.05,1.2,1.,8.,-.5,.1,.8,.8
, 1.,.3,.7,0.,0.,1.2,3.,-6.,15.,1.,.8,1.,7.,0.,0.,
0.,.6,1.5,-3.,-3.,12.,.8,1., 1.,5.,0.,0.,0.,.5,1.5
,yxdouble u,b,tmin,sqrt(),tan()double
vdot(A,B)vec A ,Breturn A.xB.xA.yB.yA.zB.z
vec vcomb(a,A,B)double avec A,BB.xa A.xB.y
aA.yB.zaA.zreturn Bvec vunit(A)vec
Areturn vcomb(1./sqrt( vdot(A,A)),A,black)stru
ct sphereintersect(P,D)vec P,Dbest0tmin1e30
s sph5while(s--gtsph)bvdot(D,Uvcomb(-1.,P,s-gtc
en)),ubb-vdot(U,U)s-gtrads -gtrad,uugt0?sqrt(u)
1e31,ub-ugt1e-7?b-ubu,tminugt1e-7ulttmin?best
s,u tminreturn bestvec trace(level,P,D)vec
P,Ddouble d,eta,evec N,color struct
spheres,lif(!level--)return blackif(sintersec
t(P,D))else return ambcolorambetas-gtird
-vdot(D,Nvunit(vcomb(-1.,Pvcomb(tmin,D,P),s-gtcen
)))if(dlt0)Nvcomb(-1.,N,black),eta1/eta,d
-dlsph5while(l--gtsph)if((el -gtklvdot(N,Uvun
it(vcomb(-1.,P,l-gtcen))))gt0intersect(P,U)l)col
orvcomb(e ,l-gtcolor,color)Us-gtcolorcolor.xU.
xcolor.yU.ycolor.zU.ze1-eta eta(1-dd)r
eturn vcomb(s-gtkt,egt0?trace(level,P,vcomb(eta,D,vc
omb(etad-sqrt (e),N,black)))black,vcomb(s-gtks,tr
ace(level,P,vcomb(2d,N,D)),vcomb(s-gtkd, color,vco
mb(s-gtkl,U,black))))main()puts(P3\n32
32\n255)while(yxlt3232) U.xyx32-32/2,U.z32/2-
yx/32,U.y32/2/tan(25/114.5915590261),Uvcomb(25
5., trace(3,black,vunit(U)),black),printf(".0f
.0f .0f\n",U)/minray!/
12
Ray Tracing Types of Rays
  • Primary rays
  • Sent from the eye, through the image plane, and
    into the scene
  • May or may not intersect an object in the scene
  • No intersection ? set pixel color to
  • background color (P2)
  • Intersects object ? send out
  • secondary rays and compute
  • lighting model (P1)

Light
Opaque object
Transparent object
P1
P2
Eye
13
Ray Tracing Types of Rays
  • Secondary Rays
  • Sent from the point at which the
  • ray intersects an object
  • Multiple types

Transmission (T) sent in the direction of
refraction
Reflection (R) sent in the direction of
reflection, and used in the Phong illumination
model
Shadow (S) sent toward a light source to
determine if point is in shadow or not.
14
Ray Tracing Types of Rays
Light
P ? Primary rays R ? Reflected rays T ?
Transmitted rays S ? Shadow rays
S3
S2
R2
T2
S1
T1
R1
R3
Opaque object
Transparent object
P
Eye
15
Ray Tracing Ray Tree
  • Each intersection may spawn secondary rays
  • Rays form a ray tree
  • Nodes ? Intersection points
  • Edges ? Reflected/transmitted ray
  • Rays are recursively spawned until
  • Ray does not intersect any object
  • Tree reaches a maximum depth
  • Light reaches some minimum value
  • Shadow rays are sent from every intersection
    point (to determine if point is in shadow), but
    they do not recursively spawn secondary rays

16
Ray Tracing Ray Tree Example
Eye
Light
P
S2
S3
O1
T2
R2
R1
T1
S1
T1
R3
O1
O2
R1
Opaque object
Transparent object
T2
R3
R2
O1
Eye
  • Ray tree is evaluated from bottom up
  • Depth-first traversal
  • Each nodes color is calculated as a function of
    its childrens colors

17
Basic Ray Tracing Algorithm
  • Generate one ray for each pixel
  • For each ray
  • Determine the nearest object intersected by the
    ray
  • Compute intensity information for the
    intersection point using the illumination model
  • Calculate and trace reflection ray (if surface is
    reflective)
  • Calculate and trace transmission ray (if surface
    is transparent)
  • Calculate and trace shadow ray
  • Combine results of the intensity computation,
    reflection ray intensity, transmission ray
    intensity, and shadow ray information
  • If the ray misses all objects, set the pixel
    color to the background color

18
Ray-Object Intersections
  • Basic (non-recursive) ray tracing algorithm
  • 1. Send a ray from the eye through the screen
  • 2. Determine which object that ray first
    intersects
  • 3. Compute pixel color
  • Most (approx. 75) of the time in step 2
  • Simple method
  • Compare every ray against every object and
    remember the closest object hit by each ray
  • Very time consuming
  • Several optimizations possible

19
Ray Representation
  • A ray can be represented explicitly (in
    parametric form) as an origin (point) and a
    direction (vector)
  • Origin
  • Direction
  • The ray consists of all points
  • r(t) ro rdt

r(3)
r(2)
ro rd r(1)
ro r(0)
r(1)
20
Viewing Ray
  • The primary ray (or viewing ray) for a point s on
    the view plane (i.e., screen) is computed as
  • Origin ro eye
  • Direction rd s eye
  • Which coordinate space?
  • Want to define rays in terms world-space
    coordinates (x, y, z)
  • Eye is already in specified in terms of (x, y, z)
    position
  • Screen point s is easiest to define in terms of
    where it is on the window in viewing-space
    coordinates (u, v, n)

s
rd s eye
ro eye
Window
21
Viewing Ray Screen Point
  • Given
  • Our scene in world-coordinates
  • A camera (eye) position in world-coordinates (x,
    y, z)
  • A pixel (i, j) in the viewport
  • We need to
  • Compute the point on the view plane window that
    corresponds to the (i, j) point in the viewport
  • Transform that point into world-coordinates

22
View-reference coordinates
v
y
LookAt point
LookFrom point
n
u
x
z
View reference coordinates
World coordinates
23
View-reference Window
v
Window
LookAt point
u
LookFrom point
n
View reference coordinates
24
Viewport
(imax, jmax)
(i, j)
(imin, jmin)
25
Computing Window Point
  • Step 1 Reverse the Window-to-Viewport
    transformation

v
(i, j)
(u, v, 0)
u
n
Viewport
View Reference coordinates
26
Viewport-Window transform
  • Window-viewport
  • Inverse transform (viewport-window)

27
View-reference to World transform
  • Given the screen point in terms of viewing-space
    coordinates (u, v, n), transform to world-space
    (x, y, z)
  • The viewing transform takes a point from world
    space to view space
  • We want to reverse this
  • or
  • s LookAt usu vsv nsn

v
s
n
Window
u
28
Ray-Object Intersections
  • Many objects can be represented as implicit
    surfaces
  • Sphere (with center at c and radius R)
    fsphere(p) p c2 - R2 0
  • Plane (with normal n and distance to origin d)
    fplane(p) p n D 0
  • To determine where a ray intersects an object
  • Need to find the intersection point p of the ray
    and the object
  • The ray is represented explicitly in parametric
    form
  • r(t) ro rdt
  • Plug the ray equation into the surface equation
    and solve for t
  • f(r(t)) 0
  • Substitute t back into ray equation to find
    intersection point p
  • p r(t) ro rdt

29
Ray-Sphere Intersections
  • To find the intersection points of a ray with a
    sphere
  • Substitute the ray equation into the sphere
    equation
  • fsphere(p) fsphere(r(t)) ro rdt c2
    - R2 0
  • We can split the ray into its component
    equations
  • x x0 xdt
  • y y0 ydt
  • z z0 zdt
  • Giving
  • (x0 xdt - xc)2 (y0 ydt - yc)2 (z0
    zdt - zc)2 R2

30
Ray-Sphere Intersections
  • Simplifying gives
  • (xd2 yd2 zd2)t 2
  • 2(xd x0 - xd xc yd y0 - yd yc zd z0 - zd zc)t
  • (xo2 - 2xo xc xc2 yo2 - 2yo yc yc2 zo2 -
    2zo zc zc2)
  • R2

31
Ray-Sphere Intersections
  • Let
  • A (xd2 yd2 zd2)
  • 1 since the ray is normalized
  • B 2(xd x0 - xd xc yd y0 - yd yc zd z0 - zd
    zc)
  • C (xo2 - 2xo xc xc2 yo2 - 2yo yc yc2
    zo2-2zo zczc2)-R2
  • Then solve using the quadratic equation

32
Ray-Sphere Intersections
  • If the discriminant is negative, the ray misses
    the sphere
  • The smaller positive root (if one exists) is the
    closer intersection point
  • We can save some computation time by computing
    the smller root first, then only computing the
    second root if necessary

33
Ray-Sphere Intersections Algorithm
  • Algorithm for ray-sphere intersection
  • 1. Calculate B and C of the quadratic
  • 2. Calculate the discriminant D B2 4C
  • 3. If D lt 0 return false (no intersection point)
  • 4. Calculate smaller intersection parameter t0
  • 5. If t0 ? 0 then calculate larger t-value t1
  • 6. If t1 ? 0 return false (intersection point
    behind ray)
  • 7. else set t t1
  • 8. else set t t0
  • 9. Return intersection point p r(t) ro rdt

34
Ray-Sphere Intersections Normal
  • The normal n at an intersection point p on a
    sphere is

n
p
R
c
35
Ray-Plane Intersections
  • To find the intersection points of a ray with an
    infinite plane
  • Substitute the ray equation into the plane
    equation
  • fplane(p) fplane(r(t)) (ro rdt) n d
    0
  • Solving for t we get
  • If t ? 0 then intersection point is behind the
    ray (return false)
  • Compute intersection point p ro rdt

36
Ray Tracing Basic Algorithm
  • The basic ray tracing algorithm is
  • for each pixel do
  • compute viewing ray r ro t rd
  • if (ray hits an object with 0 ? t lt ?)
  • compute n
  • evaluate illumination model and set pixel
    color
  • else
  • set pixel color to background color
  • Intersect each ray with every object (spheres,
    triangles, etc.)

37
Ray Tracing Ray Intersection
  • The test if (ray hits object can be
    implemented as
  • hit false
  • for each object obj do
  • if (object is hit at ray parameter t and t0
    ? t ? t1 then
  • hit true
  • hitObj obj
  • t1 t
  • return hit

38
Shadows
  • Send a shadow ray from intersection point to the
    light
  • Compute the following shadow ray properties
  • Shadow ray direction sd (l p) / l p
  • Distance to the light from the intersection
    point tl l p
  • Test if shadow ray intersects an object before
    reaching the light
  • In theory, the possible range of t-values is t ?
    0, tl
  • Due to numerical (floating-point) error, test in
    the range t ? e, t1 where is some small value
    (such as 216)

39
Recursive Ray Tracing
  • Basic ray tracing results in basic Phong
    illumination plus hidden surfaces
  • Shadows require only one extra ray per light
    source
  • Shadow rays do not reflect or refract
  • No need to find the closest object, only need to
    hit once before reaching the light
  • Reflection and refraction can spawn many new rays
    since light can keep bouncing!

40
Specular Reflection
  • Reflection in same angle as light came in
  • Light continues to bounce
  • Typically, some energy is lost on each bounce

n
r
d
r d 2n(d n)
41
Specular Reflection
  • Implement specular reflection with a recursive
    call
  • color ambient diffuse specular cs
    reflectedColor
  • where cs represents how perfect the mirror is
    and reflected color is the recursive call
  • Limit recursion max depth or when the
    contribution of a ray is negligible
  • For efficiency, generate a reflection ray ONLY if
    this point is not in shadow
  • c c cs rayColor(psr, epsilon, infinity)

42
Refraction (transparency)
  • When an object is transparent, it transmits light
  • Light bends when moving from one medium to
    another according to Snells law
  • ni sin q nt sin ?

d
n
air
glass
43
Refraction Indices
  • Index of refraction for various materials
  • Material Index
  • Vacuum 1.0
  • Air 1.0003
  • Water 1.33
  • Alcohol 1.36
  • Fused quartz 1.46
  • Crown glass 1.52
  • Flint glass 1.65
  • Sapphire 1.77
  • Heavy flint glass 1.89
  • Diamond 2.42

44
Refraction
  • Total internal reflection
  • When going from a dense to less dense medium, the
    angle of refraction becomes more shallow
  • If the angle of incidence is too shallow, it can
    get trapped in the dense material
  • Optical cable
  • Diamonds
  • Calculating the transmitted vector requires
  • Incident ray direction
  • Normal
  • Indices of refraction
  • Some trigonometry

air
d
glass
n
Write a Comment
User Comments (0)
About PowerShow.com