23
Ray tracing secondary rays: refraction, reflection and supersampling COSC342 Lecture 15 26 Apr 2016

Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

Ray tracing secondary rays:refraction, reflection and supersampling

COSC342

Lecture 1526 Apr 2016

Page 2: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

Whitted’s ray tracer clearly supported refraction

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 2

Page 3: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

i

r

n

What is refraction?

sin i = n sin r

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 3

Page 4: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

Effect of refraction

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 4

Page 5: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

Refractive index n

I The refractive index n is actually a ratio.

I Velocity of light in a medium = cn

I n depends on the medium

I n1 sin i = n2 sin r

I n for air is almost 1.0

I (Also, note that this is the third unrelated type of ‘n’ that we havediscussed recently, alongside normal vectors (n) and the Phongspecular exponent.)

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 5

Page 6: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

n

iv

Using vectors

I Ray is u + tv

I n, v are unit vectors

I sin i = ‖v − (v · n)n‖

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 6

Page 7: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

Refracted ray

The refracted ray v′ will have:

I a ‘parallel’ part v′par in the direction of v − (v · n)n, i.e., the directionthat’s perpendicular to n . . .

I . . . but whose magnitude is affected by the refractive index, i.e., thechange of speed of the light: ‖v′par‖ = ‖v − (v · n)n‖n1

n2

I Ray v′ also has a ‘normal’ part v′nor , that is the right size for v′ to bea unit vector. So v′nor = k(v · n)n for some scalar k .

I We know that that ‖v′‖ = 1, and v′ = v′par + v′nor

I From Pythagoras’ Theorem,(v′par

)2+ (k(v · n)n)2 = 12

I So, we have:

k =

√1− (v′par )2

((v · n)n)2

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 7

Page 8: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

n

r

i

Total internal reflection

I n1 sin i = n2 sin r

I sin r = n1n2

sin i

I if n1 > n2, then we forsome i angles we needsin r > 1 (impossible!)

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 8

Page 9: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

The truth is more complicated

I As the incident ray approaches the critical angle the proportion oflight reflected goes up to 100%.

I The amount of reflected and transmitted light is given by Fresnel’sequations. . .

I . . . but those are not part of COSC342 (phew!)

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 9

Page 10: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

Ray propagation—from the eye . . .

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 10

Page 11: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

. . . to the reflected/refracted rays . . .

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 11

Page 12: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

. . . and considering shadow rays from each hit point

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 12

Page 13: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

Refraction recursion: inescapable

I Where we just have reflections, we can avoid recursion:I Do the rest of the illumination before sending the reflective rayI i.e., use an accumulator to collect the colour so far, and pass it forward

to the code that handles the reflected ray.

I But when we have refraction, we can spawn two, new, recursive raysat each and every intersection point.

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 13

Page 14: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

How deep should we go in terms of recursion?

I Real reflection and refraction is never perfect. We will always lose alittle light energy at each surface interaction.

I So our coefficients kd , ks , kr , kt , for diffuse, specular, reflection andrefraction are all less than 1.0

I For an object with kr = 0.8, after 30 reflections the contribution willbe 0.830 < 1

1000

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 14

Page 15: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

Do it in the right order...

void ray(fraction, depth, u, v ...)

{

if (depth > LIMIT) return;

if (fraction < TINY) return;

get hit... do local lighting...

shadows...

ray(fraction*kr, depth+1,

hit, r(v) ...)

ray(fraction*kt, depth+1,

hit, t(v) ...)

}

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 15

Page 16: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

Our progress compared to Whitted’s ray tracer

We can now achieve:

I Ambient,

I Lambert,

I Phong,

I shadows,

I reflection,

I refraction,

I point light sources.

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 16

Page 17: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

Actually, in some ways this is just the beginning . . .

Problems that we have yet to discuss in detail:

I Aliasing artefactsI No surface/surface illumination

I If you want to read up on this, you might enjoyhttp://escience.anu.edu.au/lecture/cg/

GlobalIllumination/printNotes.en.html

I No caustics

I Shadows are hard (i.e., have hard edges)

I Colour problems

I Very slow

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 17

Page 18: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

We would like to avoid aliasing effects

I The jaggies,

I versus antialiased output:

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 18

Page 19: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

Aliases

I A given set of samples can represent more than one picture.

I The alternatives are called aliases.

I Antialiasing is mostly about getting the samples to look like thepicture we want.

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 19

Page 20: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

Supersampling

I Fire multiple rays per pixel

I How expensive will this be?

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 20

Page 21: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

There are many potential patterns for supersampling rays

I What are the pros and cons of the different patterns shown here?

I Is there an answer that will suit all potential scenes?

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 21

Page 22: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

Adaptive Sampling

I Another approach is to determine what rays to fire depending on theresults of rays you have alread fired, i.e., adaptive sampling.

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 22

Page 23: Ray tracing secondary rays: refraction, reflection and ... · Refraction recursion: inescapable I Where we just have re ections, we can avoid recursion: I Do the rest of the illumination

Jittering

I Even when just using one sample per pixel this is an improvement onuniform sampling.

COSC342 Ray tracing secondary rays: refraction, reflection and supersampling 23