Path Tracing
CS 319 Advanced Topics in Computer Graphics John C. Hart
Ray Tracing
• Whitted, CACM 80 • LDS*E • Rays cast from eye into scene Why? Because most rays cast from light wouldn’t reach eye • Shadow rays cast at each intersection Why? Because most rays wouldn’t reach the light source • Workload badly distributed Why? Because # of rays grows exponentially, and their result becomes less influential
objects
lights
Tough Cases
• Caustics – Light focuses through a specular surface onto a diffuse surface – LSDE – Which direction should secondary rays be cast to detect caustic? • Bleeding – Color of diffuse surface reflected in another diffuse surface – LDDE – Which direction should secondary rays be cast to detect bleeding?
Path Tracing
• • • • • Kajiya, SIGGRAPH 86 Diffuse reflection spawns infinite rays Pick one ray at random Cuts a path through the dense ray tree Still cast an extra shadow ray toward light source at each step in path • Trace 40 paths per pixel
objects
lights
Approximate Integration
• Can’t solve integral symbolically • Replace continuous integral with discrete summation f(x)dx Sf(xi)Dxi • Discretization requires sampling • Potential Sampling Problems • Aliasing – Jitter samples • Biasing – Ensure samples properly distributed • Inaccuracy – Ensure samples representative
Importance Sampling
• Areas of constant illumination need few samples – walls, floors, table tops • Areas of interesting illumination need many samples – edges, shadow boundaries, caustic boundaries – cast rays in high variance directions of reflectivity • Important that bright areas receive at least one sample – cast a ray directly to light source – cast a ray in highlight direction of reflectivity
Stratified Sampling
• Add sample until variance falls below threshold • Sequential Uniform Sampling – Lee, Redner and Uselton SIGGRAPH 85 – Subdivide largest area first – Breadth first subdivision • Hierarchical Integration – Weight samples by area represented – Variance becomes O(1/n2) instead of O(1/n)
Examples
Jensen, Stanford
Probability Distributions
• Continuous real random variable z • Probability density function (pdf) p(z) – Prob. of choosing a (P[z=a]) is p(a) – Prob. of a z b is ab p(z)dz – Pdf is always positive: p(z) 0 z
p(z) 1/(b-a) 0
Uniform pdf from a to b
z
a
p(z) = 2z
b
– Pdf sums to 100%: p(z)dz = 1
• Expected value: E[f(z)] = f(z)p(z)dz – Linear, regardless of corr. of f and g E[f(z)+g(z)] = E[f(z)] + E[g(z)] • Variance: V[z] = E[(z – E[z])2] – Easier to compute as: E[z2] – E[z]2
2
1
0
0 1
Prob. z < 50% is 25%
z
Monte Carlo Integration
• Expected value of f – Given random variable z – With pdf p E[f(z)] = f(z)p(z)dz • Implemented as 1 N f (z ) p(z )dz N f (z i ) i 1 • So to integrate some function g – Pick samples z with pdf p, then 1 N g (z i ) g (z )dz N p(z i ) i 1
L(w)
w
Uniform samples waste too much time measuring low light
L(w)
w
Non-uniform samples skewed toward brighter directions, but bias result to appear too bright
Dividing by pdf gives multiple samplesin brighter areas less weight. Ideal pdf is a normalized version of the integrand.
Monte Carlo Path Tracing
• Reflectance equation Lr(wr) = Le(wr) + Wfr(wi,wr)Li(wi) cosqi dwi • Path tracing – Repeated recursive evaluation of
f r (wi , wr ) Li (wi ) cos q i Dwi Lr (wr ) Le (wr ) p(wi ) • Assume diffuse surface fr(wi,wr) = r/p • Setting p(wi) = (1/p) cos qi yields Lr(wr) = Le(wr) + rLi(wi) Dwi • How can we pick wi with prob. (1/p) cos qi ? – Use Nusselt analog of random disk points
Sampling Strategies
• Sampling incident radiance
L(x, x) f r (x, x, x)G(x, x) L(x, x)dx
– Bias samples toward direction of maximal incident radiance • Sampling the BRDF
L(wr ) f r (wi , wr ) L(wi ) cos q i dwi
– Bias samples toward directions of maximal reflectance