50
Ray Tracing Algorithm Copyright © 2010 by Yong Cao

Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Page 2: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Page 3: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Page 4: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Page 5: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Page 6: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Page 7: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Page 8: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Simulate light rays from light source to eye

Reflected ray Incident ray

Eye Light

Surface

Page 9: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Trace rays from light  Lots of work for little return

Eye

Light Image Plane

Object

Light Rays

Page 10: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Light

Eye

SC

SA

SD

SB

SA shiny, transparent

SB,SD diffuse, opaque

SC shiny, opaque

Image Plane

Page 11: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

The light that point PA emits to the eye comes from:

light sources other objects (reflection) other objects (refraction)

SA shiny, transparent

SB,SD diffuse,opaque

SC shiny, opaque

Light

Eye

SC

SA

SD

PA

SB

Page 12: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Local illumination model: I = Ia+Idiff+Ispec

SA shiny, transparent

SB,SD diffuse,opaque

SC shiny, opaque

Light

Eye

SC

SA

SD

PA

SB

Page 13: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

What is the color that is reflected to PA ? The color of PC.

What is the color of PC ?

Pc

n

SA shiny, transparent

SB,SD diffuse,opaque

SC shiny, opaque

Light

Eye

SC

SA

SD

PA

SB

Page 14: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

What is the light that is reflected to PA ? The color of PC . as viewed by PA

What is the color of PC reflected towards PA? Just like PA : raytrace PC i.e compute the three contributions from

1.  Light sources 2.  Reflection 3.  refraction

SA shiny, transparent

SB,SD diffuse,opaque

SC shiny, opaque

Light

Eye

SC

SA

SD

PA

SB

Page 15: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Transparent materials

How do you compute the refracted contribution?

You raytrace the refracted ray. 1.  Lights 2.  Reflection 3.  Refraction

SA shiny, transparent

SB,SD diffuse,opaque

SC shiny, opaque

Light

Eye

SC

SA

SD

PA

SB

Page 16: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Diffuse objects do not receive light from other objects.

Page 17: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

The color that the pixel is assigned comes from: light sources other objects (reflection) other objects (refraction)

SA shiny, transparent

SB,SD diffuse,opaque

SC shiny, opaque

Light

Eye

SC

SA

SD

PA

SB

Page 18: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

  For each pixel construct a ray: eye pixel raytrace( ray ) P = closest intersection color_local = ShadowRay(light1, P)+… + ShadowRay(lightN, P) color_reflect = raytrace(reflected_ray ) color_refract = raytrace(refracted_ray ) color = color_local + kre*color_reflect + kra*color_refract

return( color )

A recursive function!

Page 19: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Page 20: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

SA

SB SC

SD

T R

R

SA shiny, transparent

SB,SD diffuse,opaque

SC shiny, opaque

Light

Eye

SC

SA

SD

PA

SB

Page 21: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 The more the better.  Infinite reflections at the limit.

Page 22: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 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

Page 23: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Page 24: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Width 2W, Height 2H Number of pixels nCols x nRows

 Camera coordinate system (eye, u,v,n)  Image plane at -N

Page 25: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Pixel P(r,c) has coordinates in camera space:

Page 26: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Pixel location

 Ray through pixel:

Page 27: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Want to know: at what point (p) does ray intersect triangle?

 Compute lighting, reflected rays, shadowing from that point

ro

rd

<?, ?, ?> (t = ???)

p

Page 28: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Step 1 : Intersect with plane  ( Ax + By + Cz + D = 0 ) Plane normal

n = <A, B, C>

p

p = -(n. ro + D) / (n. rd )

rd

ro

Page 29: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Step 2 : Check against triangle edges

p

V1

V2

V0

n

Plug p into (p. Ei + di ) for each edge

if signs are all positive or negative, point is inside triangle!

V0V1

E0 Ei = ViVi+1 x n (plane A, B, C) di = -A

.N (plane D)

Page 30: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Could use plane normals (flat shading)  Better to interpolate from vertices

p n

nV1

nV2

nV0

b

a c

V1

V2

V0

n = anV0 + bnV1 + cnV2

Find areas

area(V0V1V2)

Page 31: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Unit sphere at origin - ray intersection:

 That’s a quadratic equation

Page 32: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Page 33: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Ray(t)

t=0

t= ∞ Intersections

Page 34: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

  t1 < t2

Ray(t)

t=0

t= ∞ Intersections

t1

t2

Page 35: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Where does S+ct hit the transformed sphere G ?

Page 36: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Page 37: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Page 38: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Inverse transformed ray

 Drop 1 and 0 to get S’+c’t  For each object

 Inverse transform ray getting S’+c’t  Find intersection thit

 Use thit in the untransformed ray S+ct to find the intersection

Page 39: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 For each light intersect shadow ray with all objects.

 If no intersection is found apply local illumination at intersection

 If in shadow no contribution

Lights

Page 40: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Raytrace the reflected ray

Rayrf(t)

Ray(t)

N

a

a

P

Page 41: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Raytrace the refracted ray Snell’s law

N

Page 42: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 color(r,c) = color_shadow_ray + Kf*colorrf + Kr*colorrfa

Page 43: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

for each pixel on screen 1. determine ray from eye through pixel 2. find closest intersection of ray with an object 3. cast off reflected and refracted ray, recursively 4. calculate pixel color, draw pixel end

Page 44: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

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!

Page 45: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Use simple shape for quick test, keep a hierarchy

Page 46: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Break your space into pieces  Search the structure linearly

Page 47: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 You can always throw more processors at it.

Page 48: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

Recursive algorithm Function main()

for each pixel (c,r) on screen determine ray rc,r from eye through pixel color(c,r) = raytrace(rc,r )

end for End Function raytrace(r)

find closest intersection P of ray with objects clocal = Sum(shadowRays(P,Lighti)) cre = raytrace(rre)

cra = raytrace(rra) return c = clocal+kre*cre+kra*cra

end

Page 49: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 Participating media  Transculency  Sub-surface scattering (e.g. Human skin)  Photon mapping

Page 50: Ray Tracing Algorithm - Peopleyongcao/teaching/csx984/fall... · 2010. 9. 10. · You raytrace the refracted ray. 1. Lights 2. Reflection 3. Refraction S A shiny, transparent S B,S

Ray Tracing Algorithm

Copyright © 2010 by Yong Cao

 View dependent  Computationally expensive  Good for refraction and reflection effects