47
RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School of Computing National University of Singapore

NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

RI3004A

3D Graphics Rendering

Lecture 9

Ray Tracing

NUSRI Summer Programme 2016

School of Computing National University of Singapore

Page 2: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

2

The Idea of "Ray Casting"

In ancient time, it was used for the study of perspective

Woodcut by Albrecht Dürer, 16th century

Page 3: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

3

Ray Casting

For every pixel

Construct a ray from the eye

For every object in the scene

Find intersection with the ray

Keep if closest

Figure by

Frédo Durand, MIT

Used for hidden

surface removal

Page 4: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

4

Ray Casting and Shading

For every pixel

Construct a ray from the eye

For every object in the scene

Find intersection with the ray

Keep if closest

Shade depending on light and normal vector

Figure by

Frédo Durand, MIT

Shading can use

Phong reflection model

Page 5: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

5

Ray Tracing

From the closest intersection point, secondary rays are

shot out

Reflection ray

Refraction ray

Shadow rays

reflection

Figure by

Frédo Durand, MIT

Page 6: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

6

Whitted Ray Tracing

We get

Hidden surface removal

(from ray casting)

Reflection of light

Reflection / refraction of

other objects

Shadows

All the above are obtained

in one single framework

No ad-hoc add-on

However, it simulates only

partial global illumination

Also called

Recursive Ray Tracing

Page 7: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

7

Ray Tracing Details

I = Ilocal + krg Ireflected + ktg Itransmitted

where Ilocal = Ia ka + Isource[ kd(NL) + kr(RV)n + kt(TV)m ]

Page 8: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

8

Ray Tracing Details

I = Ilocal + krg Ireflected + ktg Itransmitted

where Ilocal = Ia ka + Isource[ kd(NL) + kr(RV)n + kt(TV)m ]

N

L R

V

N

1

2

L

T V

Page 9: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

9

Ray Tree

Ray Tree

Page 10: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

10

Shadow Rays

Also called

light rays or shadow feelers

At each surface intersection point,

a shadow ray is shot towards each

light source to determine any

occlusion between light source

and surface point

Need to find only one opaque

occluder to determine occlusion

Ilocal = Ia ka + kshadow Isource[ kd(NL) + kr(RV)n + kt(TV)m ]

Page 11: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

11

Shadow Rays

What if occluder is translucent?

Light is attenuated by the ktg of the occluder

Refraction of light ray from light source is ignored

Both are physically incorrect!

Why is this done this way?

Page 12: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

12

Scene Description

Camera view & image resolution

Camera position and orientation in world coordinate frame

Similar to gluLookAt()

Field of view

Similar to gluPerspective(), but no need near & far plane

Image resolution

Number of pixels in each dimension

Each point light source

Position

Brightness and color (Isource,red, Isource,green, Isource,blue)

A global ambient (Ia,red, Ia,green, Ia,blue)

Spotlight is also possible

Ilocal = Ia ka +

Isource[ kd(NL) + kr(RV)n + kt(TV)m ]

Page 13: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

13

Scene Description

Each object surface material

krg, ktg, ka, kd, kr, kt (each is a RGB vector)

n, m

Refractive index if ktg 0 or kt 0

Objects

Implicit representations (e.g. plane, sphere, quadrics)

Polygon

Parametric (e.g. bicubic Bezier patches)

Volumetric

I = Ilocal + krg Ireflected + ktg Itransmitted

where Ilocal = Ia ka + Isource[ kd(NL) + kr(RV)n + kt(TV)m ]

Can use different

for R, G & B.

Page 14: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

14

Recursive Ray Tracing

For each reflection/refraction ray spawned, we can trace

it just like tracing the original ray

Implemented using recursion

Page 15: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

15

Recursive Ray Tracing

0 recursion 1 recursions 2 recursions

Page 16: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

16

Recursive Ray Tracing

When to stop recursion?

When the surface is totally diffuse (and opaque)

When reflected/refracted ray hits nothing

When maximum recursion depth is reached

When the contribution of the reflected/refracted ray to the

color at the top level is too small

(krg1 | ktg1) ... (krg(n1) | ktg(n1)) < threshold

Page 17: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

17

Adventures of Seven Rays

Page 18: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

18

Ray Representations

Finding ray-object intersection and computing surface normal

is central to ray tracing

Ray representations

Two 3D vectors

Ray origin position

Ray direction vector

Parametric form

P(t) = origin + t direction

origin

direction

P(t)

Page 19: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

19

Computing Reflection / Refraction Rays

N

1

2

L R

T

Reflection

LNLN

LNR

2

cos 2

Refraction

NLNLNL

NcoscosLT

11

11

22

22

21

sin sinθ 21

Snell’s law

Page 20: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

20

Ray-Plane Intersection

Plane is often represented in implicit form

Ax + By + Cz + D = 0

Equivalent to NP + D = 0

where N = [A B C]T and P = [x y z]T

To find ray-plane intersection, substitute ray equation P(t) into plane

equation

We get NP(t) + D = 0

Solve for t to get t0

If t0 is infinity, no intersection (ray is parallel to plane)

Intersection point is P(t0)

Verify that intersection is not behind ray origin, i.e. t0 0

The normal at the intersection is N (or N)

Page 21: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

21

Ray-Sphere Intersection

Sphere (centered at origin) is often represented in implicit form

x2 + y2 + z2 r2 = 0

Equivalent to PP r2 = 0

where P = [x y z]T

To find ray-sphere intersection, substitute ray equation P(t)

into sphere equation

We get P(t)P(t) r2 = 0

P(t)P(t) r2 = 0

(Ro + tRd)(Ro + tRd) r2 = 0

RdRd t2 + 2 RdRo t + RoRo r2 = 0

Ro is ray origin

Rd is ray direction

Page 22: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

22

Ray-Sphere Intersection

It is a quadratic equation in the form at2 + bt + c = 0

a = RdRd = 1 (since |Rd| = 1)

b = 2 RdRo

c = RoRo r2

Discriminant, d = b2 4ac

Solutions, t = (b d ) / (2a)

Three cases to consider depending on value of d

What are the 3 cases? What do they correspond to?

Choose t0 as the closest positive t value (t+ or t)

The normal at the intersection point is P(t0) / |P(t0)|

Page 23: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

23

Ray-Sphere Intersection

Very easy to compute, that is why most ray tracing

images have spheres

What if sphere is not centered at origin?

Transform the ray to the sphere's local coordinate frame

How to transform? Need to consider rotation?

Page 24: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

24

Ray-Box Intersection

A 3D box is defined by 3 pairs of parallel planes, where

each pair is orthogonal to the other two pairs

If 3D box is axis-aligned, only need to specify the

coordinates of the two diagonally opposite corners

The 3 pairs of planes can be deduced easily

Page 25: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

25

Ray-Box Intersection

To find ray-box intersection

For each pair of parallel plane, find the distance to the first plane

(tnear) and to the second plane (tfar)

Keep the largest tnear so far, and smallest tfar so far

If largest tnear > smallest tfar, no intersection

Otherwise, the intersection is at P(largest tnear) How to find

normal vector?

Page 26: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

26

Ray-Triangle Intersection

Finding intersection between a ray and a general polygon

is difficult

1) Compute ray-plane intersection

2) Determine whether intersection is within polygon

Tedious for non-convex polygon

Interpolation of attributes at the vertices are not well-

defined

Much easier to find ray-triangle intersection

Can use the barycentric coordinates method

Interpolation of attributes at the vertices are well-defined

using the barycentric coordinates

Page 27: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

27

Barycentric Coordinates

The barycentric coordinates of a point P on a triangle

ABC is (, , ) such that

P = A + B + C where + + = 1 and 0 , , 1

We can rewrite it as

P = (1)A + B + C

P = A + (BA) + (CA)

B

Ro Rd

C

A

P

Page 28: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

28

Barycentric Coordinates

To find ray-triangle intersection, we let

P(t) = A + (BA) + (CA)

Ro + tRd = A + (BA) + (CA)

Solve for t, and

Intersection if + < 1 & , > 0 & t > 0

B

Ro Rd

C

A

P

Page 29: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

29

Barycentric Coordinates

Expand Ro + tRd = A + (BA) + (CA)

Rox + tRdx = Ax + (BxAx) + (CxAx)

Roy + tRdy = Ay + (ByAy) + (CyAy)

Roz + tRdz = Az + (BzAz) + (CzAz)

Regroup and write in matrix form

oz z

oy y

ox x

dz z z z z

dy y y y y

dx x x x x

R A

R A

R A

R C A B A

R C A B A

R C A B A

t

3 equations,

3 unknowns

Page 30: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

30

Barycentric Coordinates

Use Cramer's Rule to solve for t, and

A

R R A B A

R R A B A

R R A B A

dz oz z z z

dy oy y y y

dx ox x x x

A

R A C A B A

R A C A B A

R A C A B A

t oz z z z z z

oy y y y y y

ox x x x x x

A

R C A R A

R C A R A

R C A R A

dz z z oz z

dy y y oy y

dx x x ox x

| | denotes the

determinant

Page 31: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

31

Advantages of Barycentric Intersection

Efficient

No need to store plane equation

Barycentric coordinates are useful for linear interpolation

of normal vectors, texture coordinates, and other

attributes at the vertices

For example, the interpolated normal at P is

NP = (1)NA + NB + NC (should do a normalization)

Page 32: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

32

The "Epsilon" Problem

Should not accept intersection for very small positive t

May falsely intersect the surface at the ray origin

Method 1: Use an epsilon value > 0, and accept an

intersection only if its t >

Method 2: When a new ray is spawned, advanced the ray

origin by an epsilon distance in the ray direction

with without

Page 33: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

33

The "Epsilon" Problem

with without

Page 34: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

34

Ray Tracing Acceleration

Most ray tracing research have been in

Acceleration techniques for ray-scene intersection

Extension to simulate more complete global illumination (in a later lecture)

Real-time ray tracing!

Some common acceleration techniques

Adaptive recursion depth control

First-hit speed-up using z-buffer method

Can use item buffer to identify first-hit object at each pixel

Bounding volumes

Bounding volume hierarchies

Spatial subdivision

Page 35: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

35

Bounding Volumes

Use a simple shape to enclose each more complex object

If ray does not intersect bounding volume, no need to test

complex object (quick reject)

Simple shapes are efficient for testing ray intersection

Common bounding volumes are spheres, AABBs (axis-aligned

bounding boxes), and OBBs (oriented bounding boxes)

However, there is trade-off between intersection efficiency and

tightness

Page 36: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

36

Bounding Volume Hierarchy

Can organized bounding volumes into hierarchy

However, good hierarchies are usually constructed

manually

Page 37: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

37

Spatial Subdivision

Subdivide 3D space into regions, and associate each region with a list of objects that occupy (fully or partially) the region

When a ray is traced into a region, query the object list and perform intersection tests with the objects

Since we are looking for the nearest intersection, the ray should be traced in a front-to-back order through the regions

Common spatial subdivisions for ray tracing

Uniform grid

Octree

BSP

Page 38: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

38

Octree

Each cubic region is conditionally and

recursively subdivided into 8 equal sub-regions

Different possible conditions for subdivision

Scheme 1: Subdivide a cell if it is occupied by

more than one object

Scheme 2: Subdivide a cell if it is occupied by

any object until the maximum allowable depth

Ray-cell intersection can be easily tested in

front-to-back order

Page 39: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

39

Octree Cell Subdivision Schemes

Scheme 1: Subdivide a

cell if it is occupied by

more than one object

Scheme 2: Subdivide a cell if it is

occupied by any object until the

maximum allowable depth

Page 40: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

40

Limitations Of Whitted Ray Tracing

Hard shadows

Inconsistency between highlights and reflections

Sharp reflections but blurred highlights

Aliasing (jaggies)

Page 41: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

41

Limitations Of Whitted Ray Tracing

Compute only a subset of light transports

For example, cannot simulate caustics, and color bleeding

Color bleeding caused by

diffuse-to-diffuse interactions

Caustics caused by

focusing of light

Page 42: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

42

Distributed Ray Tracing

For each pixel, shoot multiple random rays

At each intersection, the reflection, refraction & shadow

rays are randomly perturbed (according to some

distributions)

Stratified or

jittered sampling

Page 43: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

43

Distributed Ray Tracing

Able to simulate the followings

Area lights and soft shadows

Blurred reflections and refractions

Anti-aliasing

Depth of field

Motion blur

However, it does not increase the subset of light

transports simulated by Whitted ray tracing

Page 44: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

44

Area Lights & Soft Shadows

Page 45: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

45

Glossy Reflections

Page 46: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

46

Depth Of Field Effect & Motion Blur

Page 47: NUSRI Summer Programme 2016 RI3004A 3D Graphics …hucoco.com/file/lec09_ray_tracing.pdf · RI3004A 3D Graphics Rendering Lecture 9 Ray Tracing NUSRI Summer Programme 2016 School

47

End of Lecture 9