44
Assignment 2 Path Tracing and Light Probes

Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

Assignment 2Path Tracing and Light Probes

Page 2: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

2

Objectives

• Experiment with stochastic path tracing and image-based lighting

• Implement:– Image-based lighting– Path tracing– Importance sampling

Page 3: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

3

Image-based Lighting

• The images in assignment 1 were dull

• Only black background• Dark reflections

Page 4: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

4

Image-based Lighting

• Exercise 1: Add a light probe

• Realistic background• Nice reflections

Page 5: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

5

Light Probes

• Take photo of reflective sphere:

Page 6: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

6

Light Probes

• Remap into convenient format• We use probes in angular map format:

Page 7: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

7

OpenEXR

• The probes are stored as OpenEXR (.exr) files• High-dynamic range (HDR) format by ILM

– First used in Harry Potter and the Sorcerers Stone, Men in Black II, Signs, Matrix, …

– 16 bit floating point / channel– Pixel values: ~0.000015 - 65000.00

• Low-dynamic range formats (PNG, JPG, etc):– Usually 8 bits/channel: 0-255

Page 8: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

8

OpenEXR

• Benefits of HDR– Change exposure without loss of detail– Full range of the human vision– Open format used in the industry

• However– Need tone-mapping (HDR to LDR)– Not yet widely supported, but becoming more

popular (e.g. Photoshop CS)

Page 9: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

9

Usage

• Download probes from homepage• Class LightProbe handles the probes

• Get background color from the probe

LightProbe probe(”data/uffizi_probe.exr”);

Scene.setBackground(&probe);

out = mScene->getBackground(direction);

Ray’s direction vector

Page 10: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

10

However

• Exercise 1 is not really image-based lighting

• Probe is only used as environment map

• Point lights for lighting

Page 11: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

11

More Realism

• We need a Global Illumination (GI) algorithm

• Simple, but slow:Path Tracing

Page 12: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

12

Path Tracing

• Send many rays into the scene– Use stratified random sampling– Output is average of samples

• At every intersection, trace random direction– Call sampleUniform(is)

Page 13: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

13

Path Tracing

Page 14: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

14

Path Tracing

Page 15: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

15

Path Tracing

Page 16: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

16

Russian Roulette

• Stopping criteria:– Kill ray with certain (absorption) probability: α

– Otherwise, divide output by: (1 - α)

• Use random number in range [0,1] to decide

Page 17: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

17

Russian Roulette

• Example– Absorption probability 10% (α = 0.1)– Constant background = 1 (white)– Send N = 1000 rays

• Result– About 100 rays are terminated (set to 0)– About 900 survive and hit background (set to 1)

out =1N100 ⋅ 0 + 900 ⋅ 1

(1−α)⎛

⎝ ⎜

⎠ ⎟ =1

Page 18: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

18

Exercise 2

• Implement the following in PathTracer:– Stratified random sampling– Russian roulette– Random sampling over hemisphere

• Debugging– Use few samples (10-100)– Low resolution (128x128 or 256x256)

Page 19: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

19

Random Sampling

• Compute a random direction in the hemisphere around the normal (sampleUniform)

Page 20: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

20

Random Sampling

• Suggested solution: Rejection sampling• Other possibilities: Spherical coordinates

(see lecture notes)

• Pick random points in the cube from -1 to 1

Page 21: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

21

Random Sampling

• Suggested solution: Rejection sampling• Other possibilities: Spherical coordinates

(see lecture notes)

• Pick random points in the cube from -1 to 1

• Discard outsidethe unit sphere

Page 22: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

22

Random Sampling

• Normalize to get samples on the sphere

Page 23: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

23

Random Sampling

• Keep only points in the hemisphere around the normal:

Page 24: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

24

Exercise 2 Result

Resolution 256x256100 samples/pixel ~40 seconds

Page 25: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

25

Exercise 3

• Add specular reflections and refractions

• WhittedTracer traced all 3 rays• With PathTracing we only trace 1 ray,

but which one?

out = (1-t)*((1-r)*direct + r*reflected) + t*refracted;

Page 26: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

26

Ray Probabilities

• Rewrite to get the individual weights

• Choose type of ray randomly based on their weights. Example:

wd = (1-t)*(1-r)wr = (1-t)*r wd + wr + wt = 1wt = t

wd = 0.50wr = 0.35wt = 0.15

50% probability of random ray35% probability of reflected ray15% probability of refracted ray

float uniform(); // random number in [0,1)

Page 27: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

27

Exercise 3 Result

Same setup as before:

Resolution 256x256100 samples/pixel

Page 28: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

28

Noise

• Random sampling gives very noisy output:

• Reduce noise:– More samples -> Slow– Better sampling strategy -> Importance Sampling

Page 29: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

29

Random Sampling

• The rendering equation:

• Our estimation: N uniform random samples Ψi€

L x →Θ( ) = L(x∫ ← Ψ) fr x,Ψ↔Θ( ) cos Nx ,Ψ( )dωΨ

L x→Θ( ) ≈ 1N

L x←Ψi( ) f r x,Ψi ↔Θ( )cos(Nx,Ψi)i∑

trace(…) getBRDF(…)

Page 30: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

30

Importance Sampling

• Choose sampling directions Ψi smarter

• Higher probability for samples near normal

L x →Θ( ) = L(x∫ ← Ψ) fr x,Ψ↔Θ( ) cos Nx ,Ψ( )dωΨ

P Ψi( )∝cos Nx,Ψi( )

Page 31: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

31

Importance Sampling

• We must divide by probability of each sample:

• Implement cosine sampling in sampleCosine€

L x→Θ( ) ≈ 1N

L x←Ψi( ) f r x,Ψi ↔Θ( )cos(Nx,Ψi)p Ψi( )i

Page 32: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

32

Cosine Sampling

• Describe directions on hemisphere by (θ,φ):• Set z = normal

• Want samples with:

• How?

normal

p(θ,φ) =cosθπ

Page 33: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

33

Use the CDF

• Compute the cumulative distribution function by integrating over the hemisphere:(details p. 65-66 in Advanced Global Illumination)

• Separable into:€

F θ,φ( ) =1π

p θ,φ( )dθdφ =φ2π

1− cos2θ( )0

θ

∫0

φ

Fφ =φ2π

Fθ =1− cos2θ

Page 34: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

34

CDF Sampling

• Set the CDF:s to random numbers u1,u2 in [0,1)

• Spherical coordinates (θ,φ) to Cartesian (x,y,z):

Fφ = u1Fθ = u2

x = cosφ sinθy = sinφ sinθz = cosθ

Page 35: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

35

Coordinate Transform

• The sampling direction (x,y,z) is in local coords,transform to world coordinates with z = normal

• Setup orthogonal base vectors:• Transform dir to world space

Page 36: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

36

Sampling Probability

• The sampling direction should be computed in:

• Return the probability of the sample in pdf• Change trace() to divide by the probability:

sampleCosine(Intersection is, float& pdf);

out = trace(r) * brdf / (pdf * 2.0f*M_PI);

correction factor

Page 37: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

37

Why an extra 2π?

• With random sampling we assumed pdf = 1 (no division), but:

• To get matching output, we must multiply our pdf by 2π

1dθdφ = 2πθ = 0

π / 2

∫φ= 0

Page 38: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

38

Exercise 4 Result

Resolution 256x256100 samples/pixel

Page 39: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

39

Comparison

Random sampling

Page 40: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

40

Comparison

Cosine sampling

Page 41: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

41

Conclusions

• Cosine-weighted importance sampling:– (Slightly) Less noise than random sampling– Simple and fast

• More advanced importance sampling:– Environment map importance sampling– BRDF importance sampling– Product sampling (Rejection, Resampling, Wavelets)

Page 42: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

42

Examples

Page 43: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

43

Examples

Page 44: Assignment 2 - fileadmin.cs.lth.sefileadmin.cs.lth.se/.../assignment2/S2-pathTracing.pdf · 2 Objectives • Experiment with stochastic path tracing and image-based lighting • Implement:

44