Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date...

24
© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5 Ray Tracing: shading CS 4620 Lecture 5 1

Transcript of Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date...

Page 1: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Ray Tracing:shading

CS 4620 Lecture 5

1

Page 2: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Image so far

• With eye ray generation and scene intersectionfor 0 <= iy < ny for 0 <= ix < nx { ray = camera.getRay(ix, iy); c = scene.trace(ray, 0, +inf); image.set(ix, iy, c); }

Scene.trace(ray, tMin, tMax) { surface, t = surfs.intersect(ray, tMin, tMax); if (surface != null) return surface.color(); else return black;}

2

Page 3: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Shading

• Compute light reflected toward camera• Inputs:

– eye direction– light direction

(for each of many lights)– surface normal– surface parameters

(color, shininess, …)

3

vl n

Page 4: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

• Light is scattered uniformly in all directions– the surface color is the same for all viewing directions

• Lambert’s cosine law

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Diffuse reflection

Top face of cubereceives a certain amount of light

Top face of 60º rotated cube

intercepts half the light

In general, light per unitarea is proportional to

cos θ = l • n

l n

4

Page 5: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Light falloff

5

Page 6: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

I

1

intensityhere:

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Light falloff

5

Page 7: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

rintensityhere: I/r2

I

1

intensityhere:

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Light falloff

5

Page 8: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

• Shading independent of view direction

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Lambertian shading

diffusecoefficient

diffuselyreflected

light

illuminationfrom source

vl n

6

Ld = kd (I/r2)max(0,n · l)

Page 9: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Lambertian shading

• Produces matte appearance

[Fol

ey e

t al

.]

kd

7

Page 10: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Diffuse shading

8

Page 11: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Image so farScene.trace(Ray ray, tMin, tMax) { surface, t = hit(ray, tMin, tMax); if surface is not null { point = ray.evaluate(t); normal = surface.getNormal(point); return surface.shade(ray, point, normal, light); } else return backgroundColor;}

Surface.shade(ray, point, normal, light) { v = –normalize(ray.direction); l = normalize(light.pos – point); // compute shading}

9

Page 12: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Shadows

• Surface is only illuminated if nothing blocks its view of the light.

• With ray tracing it’s easy to check– just intersect a ray with the scene!

10

Page 13: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Image so far

Surface.shade(ray, point, normal, light) { shadRay = (point, light.pos – point); if (shadRay not blocked) { v = –normalize(ray.direction); l = normalize(light.pos – point); // compute shading } return black;}

11

Page 14: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Shadow rounding errors

• Don’t fall victim to one of the classic blunders:

• What’s going on?– hint: at what t does the shadow ray intersect the surface

you’re shading?12

Page 15: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Shadow rounding errors

• Solution: shadow rays start a tiny distance from the surface

• Do this by moving the start point, or by limiting the t range

13

Page 16: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Multiple lights

• Important to fill in black shadows• Just loop over lights, add contributions• Ambient shading

– black shadows are not really right– one solution: dim light at camera– alternative: add a constant “ambient” color to the shading…

14

Page 17: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Image so far

shade(ray, point, normal, lights) { result = ambient; for light in lights { if (shadow ray not blocked) { result += shading contribution; } } return result;}

15

Page 18: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Specular shading (Blinn-Phong)

• Intensity depends on view direction– bright near mirror configuration

vl n

16

Page 19: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Specular shading (Blinn-Phong)

• Close to mirror ⇔ half vector near normal– Measure “near” by dot product of unit vectors

specularcoefficient

specularlyreflected

light

nv

hl

17

h = bisector(v, l)

=v + l�v + l�

Ls = ks (I/r2)max(0, cos↵)p

= ks (I/r2)max(0,n · h)p

Page 20: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Phong model—plots

• Increasing n narrows the lobe

[Fol

ey e

t al

.]

18

Page 21: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Specular shading

[Fol

ey e

t al

.]

19

ks

p

Page 22: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Diffuse + Phong shading

20

Page 23: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Ambient shading

• Shading that does not depend on anything– add constant color to account for disregarded illumination

and fill in black shadows

ambientcoefficient

reflectedambient

light21

La = ka Ia

Page 24: Ray Tracing: shading - Cornell University...Ray Tracing Author Steve Marschner Created Date 9/10/2014 2:30:38 AM ...

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 5

Putting it together

• Usually include ambient, diffuse, Phong in one model

• The final result is the sum over many lights

22

L = La + Ld + Ls

= ka Ia + kd (I/r2)max(0,n · l) + ks (I/r

2)max(0,n · h)p

L = La +

NX

i=1

[(Ld)i + (Ls)i]

L = ka Ia +NX

i=1

⇥kd (Ii/r

2i )max(0,n · li) +

ks (Ii/r2i )max(0,n · hi)

p⇤