Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS...

48
CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference: Ed Angle, Interactive Computer Graphics, University of New Mexico, class notes

Transcript of Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS...

Page 1: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

CS 4204 Computer Graphics

Introduction to Ray Tracing

Yong CaoVirginia Tech

Reference: Ed Angle, Interactive Computer Graphics, University of New Mexico, class notes

Page 2: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Raytracing (Picture from Povray.org)

Page 3: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Raytracing (Picture from Povray.org)

Page 4: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Raytracing (Picture from Povray.org)

Page 5: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Raytracing (Picture from Povray.org)

Page 6: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Raytracing (Picture from Povray.org)

Page 7: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Raytracing (Picture from Povray.org)

Page 8: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

The Basic Idea

• Simulate light rays from light source to eye

Eye Light

Surface

Page 9: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

“Forward” Ray-Tracing• Trace rays from light

• Lots of work for little return

Eye

LightImagePlane

Object

Light Rays

Page 10: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Scene

Light

Eye

SC

SA

SD

SB

SA shiny, transparent

SB,SD diffuse,opaque

SC shiny, opaque

Image Plane

Page 11: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Three sources of light

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

light sourcesother objects (reflection)other objects (refraction)

SA shiny, transparent

SB,SD diffuse,opaque

SC shiny, opaque

Light

Eye

SC

SA

SD

PA

SB

Page 12: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Directly from light source

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: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

ReflectionWhat 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: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

ReflectionWhat 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: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

RefractionTransparent 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: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

What are we missing?

• Diffuse objects do not receive light from other objects.

Page 17: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Three sources of light togetherThe color that the pixel is assigned comes

from:light sourcesother objects (reflection)other objects (refraction)

It is more convenient to trace the rays from the eye to the scene (backwards)

SA shiny, transparent

SB,SD diffuse,opaque

SC shiny, opaque

Light

Eye

SC

SA

SD

PA

SB

Page 18: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Backwards Raytracing Algoritm• For each pixel construct a ray: eye pixel

raytrace( ray )

P = closest intersectioncolor_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 )

Page 19: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

How many levels of recursion do we use?

• The more the better.• Infinite reflections at the limit.

Page 20: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

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

Page 21: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Setting up the camera

Page 22: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Image parameters• Width 2W, Height 2H

Number of pixels nCols x nRows

• Camera coordinate system (eye, u,v,n)

• Image plane at -N

Page 23: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Pixel coordinates in camera coordinate system

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

Page 24: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Ray through pixel

• Pixel location

• Ray through pixel:

Page 25: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Triangle Intersection• Want to know: at what point (p) does ray intersect

triangle?

• Compute lighting, reflected rays, shadowing from that point

ro

rd

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

p

Page 26: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Triangle Intersection• Step 1 : Intersect with plane( Ax + By + Cz + D = 0 )

Plane normaln = <A, B, C>

p

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

rdro

Page 27: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Triangle Intersection• 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

E0Ei = ViVi+1 x n (plane A, B, C)di = -A.N (plane D)

Page 28: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Triangle Normals• Could use plane normals (flat shading)

• Better to interpolate from vertices

pn

nV1

nV2

nV0

b

ac

V1

V2

V0

n = anV0 + bnV1 + cnV2

Find areas

area(V0V1V2)

Page 29: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Ray-object intersections

• Unit sphere at origin - ray intersection:

• That’s a quadratic equation

Page 30: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Solving a quadratic equation

Page 31: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

First intersection?

Ray(t)

t=0

t= ∞Intersections

Page 32: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

First intersection?

t1 < t2

Ray(t)

t=0

t= ∞Intersections

t1

t2

Page 33: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Transformed primitives?

• Where does S+ct hit the transformed sphere G ?

Page 34: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Linear transformation

Page 35: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Linear transformation

Page 36: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Final Intersection• Inverse transformed ray

• Drop 1 and O to get S’+c’t

• For each object• Inverse transform ray getting S’+c’t

• Find intersection th• Use th in the untransformed ray S+ct to find the intersection

Page 37: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Shadow ray• 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 38: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Reflected ray

• Raytrace the reflected ray

Rayrf(t)

Ray(t)

N

a

a

P

Page 39: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Refracted ray• Raytrace the refracted raySnell’s law

N

Page 40: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Add all together• color(r,c) = color_shadow_ray + Kf*color_rf +

Kr*color_rfa

Page 41: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Raytracingfor each pixel on screen

determine ray from eye through pixel

find closest intersection of ray with an object

cast off reflected and refracted ray, recursively

calculate pixel colour, draw pixel

end

Page 42: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Acceleration• 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 43: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Bounding volumes• Use simple shape for quick test, keep

a hierarchy

Page 44: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Space Subdivision• Break your space into pieces

• Search the structure linearly

Page 45: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Parallel Processing• You can always throw more processors at

it.

Page 46: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

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(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 47: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Advanced concepts

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

Page 48: Introduction to Ray Tracing - Virginia Techpeople.cs.vt.edu/~yongcao/teaching/cs4204/fall2008/...CS 4204 Computer Graphics Introduction to Ray Tracing Yong Cao Virginia Tech Reference:

Raytracing summary

• View dependent• Computationally expensive• Good for refraction and reflection effects