Contents Stochastic • Monte Carlo Integration • Application to direct lighting by area light sources • Solving the radiance equation with the Kadi Bouatouch Monte Carlo Method IRISA – Distributed Ray Tracing Email: [email protected] – Path Traycing

1 2

Classical Ray Tracing With Area Light Sources

• One shadow ray by intersection point

• Only point light sources

• Hard shadows: umbra • Soft shadows • Area light sources != point light sources 3 4 More Sample Points on the Light Solution to the Source

y ψ y • Analytical solution: too ψθy V(x,y)=? θy much difficult V(x,y)=?rxy rxy θx • Use the Monte Carlo θ x x Θx method Θ

cosθxcosθy e L(x→Θ)= fr (x,Θ↔Ψ)L (y→Ψ)V(x,y) 2 dy ∫ rxy • Approximate the source by a set of points AL • Aliasing along the shadows’ borders 5 6

Direct Lighting Direct Lighting

y ψ y • Random point sampling of ψθy ψ θy the area light sources V(x,y)=?rxy y rxyω’ θx θ x x • Use these points to evaluate Θx x Θ the integral Θ ω cosθxcosθy e 1 shadow ray 9 shadow rays L(x→Θ)= fr (x,Θ↔Ψ)L (y→Ψ)V(x,y) 2 dy 1 ∫ rxy p(y)= AL AS N N θ 1 D(yi) 1 cos xcosθyi L(x→Θ)= D(y)dy L(x→Θ) = L(x→Θ) = As f(x,Θ↔Ψ)Le(yi→Ψi ) Vis(x,yi) ∫ ∑ i ∑ 2 N p(y) N rxyi AL i=1 i=1 7 8 Direct Lighting Stratified Sampling

36 shadow rays 100 shadow rays

N θ cos xcosθyi 1 s e i i L(x→Θ) = ∑A f(x,Θ↔Ψ)L (y→Ψi ) 2 Vis(x,y) 9 shadow rays 9 shadow rays N rxyi i=1 without stratification with stratification

9 10

Stratified Sampling Stratified Sampling

36 shadow rays no 36 shadow rays with 100 shadow rays no 100 shadow rays with stratification stratification stratification stratification

11 12 Multiple Light Sources Multiple Light Sources

• The integral does not change : rather than integrating over one light source, integrate over all the surfaces of the light sources

cosθxcosθy r e L(x→Θ)= f (x,Θ↔Ψ)L (y→Ψ)V(x,y) 2 dy ∫ rxy AL • The pdf for selecting points is modified : first select a light source S using pdf p(S), then a point y on S with p(y|S) 36 shadow rays per pixel in the 2 images but with different p(S)=ΦS p(yS)= 1 p(y)=p(S)p(yS) ΦT As pdf 13 14

Application to pixels: oversampling Application to pixels: oversampling

• Compute radiance at the centre • Any sampling method of a pixel Æ aliasing • Oversample a pixel and compute radiance for sub-pixels • Use a filter

L= ∫ f(x)L(x)dx Pixel … evaluated by the Monte Carlo method. N f(xi)L(xi) L≈ 1 ∑ N i=1 p(xi)

15 16 Application to pixels: oversampling Implementation

• Comparison :

1 centered ray per pixel 100 centered rays per pixel 1 ray / pixel 10 rays / pixel 100 rays / pixel 100 random shadow rays 1 random shadow rays per per intersection intersection 17 18

The Rendering Equation The Rendering Equation

• Evaluation of the rendering equation L(x→Θ)=Le(x→Θ)+ ∫ fr(x,Ψ↔Θ)⋅L(x←Ψ)⋅cos(Ψ,nx)⋅dωΨ • How to write the rendering equation and Ωx how to evaluate it using Monte Carlo Lx()←Ψ Lx() integration? →Θ

Lxe ()→Θ – Which pdf to use for the rendering equation ? x – Algorithms and results

19 20 The Rendering Equation Computing Radiance

• How to evaluate L ? L(x→Θ)=Le(x→Θ)+ ∫ fr(x,Ψ↔Θ)⋅L(x←Ψ)⋅cos(Ψ,nx)⋅dωΨ Ωx

–Find Le(x→Θ)

= Le + ⋅ fr ⋅cos – Add: ∫ L=? Ω x

∫ fr (x,Ψ ↔ Θ)⋅ L(x ← Ψ)⋅cos(Ψ,nx )⋅dωΨ Ωx

21 22

Computing Radiance Computing Radiance

• How to evaluate L ? • Hemisphere Sampling θin – Monte Carlo Integration π / 2 dω Θin – Generate random directions on Ω , using the x θin probability density function p(Ψ) θ ϕ 0 π in 0 θ 2π ϕin L(x→Θ)= fr(x,Ψ↔Θ)⋅L(x←Ψ)⋅cos(Ψ,nx)⋅dωΨ ξ ∫ ϕ Ωx p(Θ)=cos ⇒ =acos( )et =2πξ π θ 1 2 θ N 1 fr(x,Ψi↔Θ)⋅L(x←Ψi)⋅cos(Ψi,nx) Θ=(θ,ϕ)⇒dωΘ=sinθ⋅dθ⋅dϕξ L(x→Θ) = ∑ 1 ϕ N i=1 p(Ψi) p(Θ)=n+1cosn ⇒ =acos( n+1)et =2πξ 2 1 2

23 24 Computing Radiance Computing Radiance

• Generate a random

direction Ψi z Evaluation of L(x←Ψi) ? 1 N f ( )⋅ L(x ← Ψ )⋅cos( ) L = ∑ r K i K N i=1 p(Ψi ) z Radiance is constant along the propagation direction. Evaluate the BRDF

z rc(x, Ψ ) = first visible point. Evaluate the cos(…) i

Evaluate L(x←Ψi) z L(x←Ψi) = L(rc(x, Ψi) → Ψi)

25 26

Computing Radiance Stopping Recursion

• When recursion is stopped? • Recursive Evaluation

• Each bounce adds a level of indirect lighting.

The contributions of higher order reflections are negligible. If we ignore them, the estimates are biased !

27 28 Terminating the Recursion Russian Roulette • When/how do we stop the recursion? •Integral f (x) – When the ray doesn’t hit any object 1 1 α • Can be very hard/impossible for dense scenes I = ∫ f (x)dx = ∫ f (x /α)dx 0 α 0 • Estimator α – When a maximum depth is reached α • This is highly scene dependent 1 – Primarily specular scenes requires far more bounces than < Iroulette >= f ( x / ) if x < α diffuse scenes • Having a fixed path length results in a biased estimate 0 if x > α – When the contribution of the ray falls below a certain • Use Russian roulette to decide α threshold – ray is absorbed with probability 1-α • More efficient than a fixed max depth, but still gives a biased – results in unbiased estimator result

29 30

Russian Roulette Russian Roulette • A simple and unbiased termination criteria is Russian roulette: •Example – Given a uniform random number ξ, terminate the ray –p = 0.9 , then α = 1- p = 0.1 if ξ ≥ α, otherwise scale the contribution of the ray by 1/α – One chance in 10 that ray is reflected. • Here αє [0,1] is the absorption probability – The radiance due to one reflected ray – Recursion stops with a probability of p = 1- α – By scaling the contribution of rays that continue by is multiplied by 10. 1/ α, the result remains unbiased – instead of shooting 10 rays, we shoot • Russian roulette is not practically useful until we only 1, but count the contribution of add direct light to our ray tracer!! this one 10 times

31 32 Russian Roulette Russian Roulette

• With Russian roulette the pseudo code now looks like this: • Case of n incident rays RGB radiance(Ray r) if(r hits at x) N if(ξ < α) 1 fr (x,Ψi ↔ Θ)⋅ L(x ← Ψi )⋅cos(Ψi ,nx ) L(x → Θ) = ∑ Generate new direction, Di, from p ( Ψ ) and i=1 i N p(Ψi ) the surface normal at x Ray ray(x, Di) •Case of one incident ray return Le(x) + R(x,Di)*radiance(ray) / α else f (x, Di ↔ Θ)⋅ L(x ← Di)⋅cos(Di,n ) return Le(x) L(x → Θ) = r x p(Di) else return background = R(x, Di).L(x ← Di)

33 34

Russian Roulette Distribution Ray Tracing

• Non biased Estimate • Algorithm – Trace N rays per pixel • The expected value is correct – At each intersection point, trace 1 ray (or more) randomly chosen on the hemisphere to evaluate the rendering equation • Bigger variance – End recursion using the Russian roulette

• But more efficient

35 36 Distribution Ray Tracing Distribution Ray Tracing computeImage() { basicPT(x, theta) { for each pixel (i,j) { estimatedRadiance = Le(x, theta) estimatedRadiance[i,j] = 0 if(not absorbed) { // russian roulette for s = 1 to #samples-in-pixel { for s = 1 to #radianceSamples { // ray directions generate Q in pixel (i,j) theta = (Q – E)/|Q-E| // E is the Eye psi = generate random direction on hemisphere x = trace(E,theta) y = trace(x, psi) estimatedRadiance [i,j] += computeRadiance(x,-theta) estimatedRadiance += } basicPT(y,-psi) * BRDF(x,psi,theta)* estimatedRadiance [i,j] /= # samples-in-pixel cos(Nx,psi) / pdf(psi) } } } computeRadiance(x, theta) { estimatedRadiance /= #radianceSamples estimatedRadiance = basicPT(x, theta) } return estimatedRadiance return estimatedRadiance/(absorption) } } 37 38

Distribution Ray Tracing Distribution Ray Tracing

39 40 Distribution Ray Tracing Distribution Ray Tracing

1 ray/pixel 16 rays/pixel 256 rays/pixel

Very noisy : null contribution as long as the path does not reach a light source!! L = ??

41 42

Distribution Ray Tracing Distribution Ray Tracing

Lee = 00 Linin = ??

Lrr = ??

43 44 Distribution Ray Tracing Distribution Ray Tracing

L = ??

Linin = ??

45 46

Distribution Ray Tracing Distribution Ray Tracing

Linin = ?? Lee=00 Lrr=??

47 48 Distribution Ray Tracing Distribution Ray Tracing

Lee = 11..234234 Lrr = ??

L = ??

49 50

Distribution Ray Tracing Distribution Ray Tracing

• Improve the algorithm by dividing the integral into two parts: direct and indirect

Li(x→Θ)=∫ fr(x,Ψ↔Θ)⋅Le(x←Ψ)⋅cos(Ψ,nx)⋅d Ψ Ωx ω + ∫ fr(x,Ψ↔Θ)⋅Li(x←Ψ)⋅cos(Ψ,nx)⋅dωΨ Ωx

L(x→Θ)=Le(x→Θ)+Li(x→Θ)

51 52 Distribution Ray Tracing Direct Illumination

L (x → Θ) = f ( )⋅ L (x ← Ψ)⋅cos( )⋅d direct ∫ r K e K ω Ψ Ωx – Evaluate differently the direct and the indirect = f ( )⋅ L (y → x)⋅cos( )⋅dω components ∫ r K e K Ψ Ωx cos( )dA = f ( )⋅ L (y → x)⋅cos( )⋅( K y )⋅V (x, y) ∫ r K e K 2 Area rxy ⋅ f ⋅cos + ⋅ f ⋅cos source ∫ r ∫ r Source Ω x dAy

Θ dA yycosθ dω = dω Θ 2 x Θ r

53 54

Indirect Illumination Indirect Illumination

Li (x → Θ) = ∫ fr (x,Ψ ↔ Θ)⋅ Li (x ← Ψ)⋅cos(Ψ,nx )⋅dωΨ Li (x → Θ) = ∫ fr (x,Ψ ↔ Θ)⋅ Li (x ← Ψ)⋅cos(Ψ,nx )⋅dωΨ Ωx Ωx

55 56 Distribution Ray Tracing Distribution Ray Tracing

L(x→Θ)= ∫ fr(x,Ψ↔Θ)⋅L(x←Ψ)⋅cos(Ψ,nx)⋅dωΨ •Uniform distribution Ωx N 1 fr(x,Ψi↔Θ)⋅L(x←Ψi)⋅cos(Ψi,nx) p(Ψ) = L(x→Θ) = 1 ∑ 2π N i=1 p(Ψi) 2π N • Depends on how to sample the hemisphere L(x → Θ) = ∑ fr (x,Ψi ↔ Θ)⋅ L(x ← Ψi )⋅cos(Ψi ,nx ) N i=1 – Uniform distribution – Importance sampling : pick p to match integral • Cosine distribution • BRDF distribution • BRDF*cosine distribution

57 58

Distribution Ray Tracing Distribution Ray Tracing

•Cosine distribution •BRDF distribution

cosθΨ p(Ψ) = p(Ψ) = fr(...) π π N 1 N L(x → Θ) = ∑ fr (x,Ψi ↔ Θ)⋅ L(x ← Ψi ) N i=1 L(x → Θ) = ∑ L(x ← Ψi )⋅cos(Ψi ,nx ) N i=1

59 60 Distribution Ray Tracing: Distribution Ray Tracing pixel sampling •BRDF*cosine distribution computeImage() { for (each pixel (i,j)) { estimatedRadiance[i,j] = 0 p(Ψ) = fr(...).cosθΨ for (s = 1 to #samples-in-pixel) { 1 N generate Q in pixel (i,j) theta = (Q – E)/|Q-E| L(x → Θ) = ∑ L(x ← Ψi ) N i=1 x = trace(E,theta) estimatedRadiance [i,j] += computeRadiance(x,-theta) } estimatedRadiance [i,j] /= #samples-in-pixel } } 61 62

Distribution Ray Tracing: Distribution Ray Tracing: radiance estimation direct illumination directIllumination(x,theta) { estimatedRadiance = 0 computeRadiance(x,theta) { for (s = 1 to #shadowRays) { estimatedRadiance = Le(x,theta) k = pick random light estimatedRadiance += directIllumination(x, theta) y = generate random point on light k estimatedRadiance += indirectIllumination(x, theta) psi = (x-y) / |x-y| estimatedRadiance += Le_k(y,-psi) * return estimatedRadiance BRDF(x,psi,tetha) *G(x,y) * V(x,y) /(p(k)*p(y|k)) } } estimateRadiance /= #shadowRays return estimatedRadiance }

63 64 Distribution Ray Tracing: Distribution Ray Tracing: direct illumination indirect illumination directIllumination(x,theta) { indirectIllumination(x,theta) { estimatedRadiance = 0 estimatedRadiance = 0 for (k=1 to #lights) { if (not absorbed) { // russian roulette for (s = 1 to #shadowRays) { for (s = 1 to #indirectDirectionSamples) { y = generate random point on light k psi = generate random direction on hemisphere psi = (x-y) / |x-y| y = trace(x, psi) estimatedRadiance += Le_k(y,-psi) * estimatedRadiance += computeRadiance(y,-psi) * BRDF(x,psi,tetha) *G(x,y) * V(x,y) /p(y) BRDF(x,psi,theta) *cos(Nx,psi) / pdf(psi) } } } estimatedRadiance /= #indirectDirectionSamples estimateRadiance /= #shadowRays } return estimatedRadiance return estimatedRadiance /(absorption) } } 65 66

Distribution Ray Tracing Tracing Lambertian Materials

• To sum up – For primary rays : • We’ve already seen that for Lambertian • use many ray samples at the intersection point materials, R is just a constant between 0 and 1 • Use uniform or cosine pdf to sample the hemisphere • To generate a randomθ φray direction, we use 1 – For shadow rays : the cosine density p ( , ) = π cos θ and two uniform • Use uniform area-based pdf to sample the light sources random numbers ξ1 and ξ2 • Use many samples – Using the techniques presented before, we find – For secondary rays that θ • Use one or more samples cos = 1−ξ 1 φ • Use BRDF based pdf = 2πξ2

67 68 Imperfect Specular Reflections Transparent Materials

• Perfectly reflecting materials are rare • When a ray hits a transparent material, it is either – Usually, the reflection is slightly blurred reflected or transmitted • To achieve an imperfect specular reflection, we – When a ray is transmitted from a medium with can choose the reflection direction from a phong refractive index ni to a medium with refractive index nt, it is bend according to Snell’s law: n sin(θ)=n sin(φ) density: i t π – For an incident angle of θ, θ 1 m+1 m+1 m cos = (1−ξ 1) the fraction of incident rays p(k) = (r⋅k) ⇒ φ 2 = 2πξ that are reflected is R(θ). 2 1-R(θ) is the fraction of – Where r is the mirror reflection direction and θ is the transmitted rays angle between r and k

69 70

Path tracing Reflection or Transmission?

• At each intersection point one can make a • When striking a transparent surface, we choice need to make a choice: Should the new – Reflection or refraction? ray be a reflected or transmitted ray? – If reflection : diffuse or specular? • We can set a transmission probability P, and then pick a random number ξ. –If ξ

71 72 Beer’s Law Specular-Diffuse Surfaces

• When light travels through an ‘impure’ • Most surfaces reflects light is some medium, it’s radiance is attenuated combination of specular and diffuse according to Beer’s law: reflections I(s) = I(0)eln(a)s – When the angle between the view vector and –Here I(s) is the radiance of a ray at a distance the normal increases, the specular reflection s from the interface and a is the RGB increases and the diffuse decreases attenuation constant • We model such materials by linearly combining a specular and a diffuse material

73 74

Specular-Diffuse Surfaces Results

• We can choose the specular ray with probability P and the diffuse ray with probability 1-P if(ξ < P) return R(x,Di)*radiance(specular ray)/P else return R(x)*radiance(diffuse ray)/(1-P)

L=?? 75 76 Results Results

Lee = 00 ee L dd L = 00 L = ?? rr L ? ii L = ? L = ?? 77 78

Results Results

L = ?? Lee = 00 Ldd ≈ 22..345345 Lii = ?? 79 80 Results Comparison

Without computing With direct lighting direct lighting computation 16 rays/pixel

81 82

Comparison

1 ray/ 4 rays/ pixel pixel

16 rays/ 256 rays/ pixel pixel

83