Title: MonteCarlo Ray Tracing
1Monte-Carlo Ray Tracing
Image by Henrik
2Antialiasing integration
- So far, Antialiasing as signal processing
- Now, Antialiasing as integration
- Complementary yet not always the same
- in particular for jittered sampling
3Why integration?
- Simple version compute pixel coverage
- More advanced Filtering (convolution)is an
integralpixel s filter color - And integration is useful in tons of places in
graphics
4Global illumination
- So far, we've seen only direct lighting (red
here) - We also want indirect lighting
- Full integral on hemisphere (multiplied by BRDF)
- In practice, send tons of random rays
5What else can we integrate?
- Pixel antialiasing
- Light sources Soft shadows
- Lens Depth of field
- Time Motion blur
- BRDF glossy reflection
- Hemisphere indirect lighting
6Domains of integration
- Pixel, lens (Euclidean 2D domain)
- Time (1D)
- Hemisphere
- Work needed to ensure uniform probability
- Light source
- Same thing make sure that the probabilities and
the measures are right.
7Example Light source
- Integrate over surface or over angle
- Be careful to get probabilities and integration
measure right! - More in 6.839
Sampling the source uniformly
Sampling the hemisphere uniformly
source
hemisphere
8A little bit of eye candy for motivation
- Glossy material rendering
- Random reflection rays around mirror direction
- 1 sample per pixel
9A little bit of eye candy for motivation
- Glossy material rendering
- Random reflection rays around mirror direction
- 256 sample per pixel
10Error/noise expressed as variance
- We use random rays
- Run the algorithm again, ? get different image
- What is the noise/variance/standard deviation?
11Goal today
- Prove that Monte-Carlo integration works
- Notion of expected value
- Discuss its convergence speed
- Error notion of variance
- For all this, we need to study what happens to
expected value and variance when we had tons of
random samples - Apply to ray tracing
12Questions?
13Integration
- compute integral of weird arbitrary function
- Continuous problem ? we need to discretize
14Integration
15Integration
- Now come Monte Carlo use random samples and
compute average - We dont keep track of spacing between samples
- But we kind of hope it will be on average 1/n
16Monte-Carlo computation of p
- Take a square
- Take a random point (x,y) in the square
- Test if it is inside the ¼ disc (x2y2 lt 1)
- The probability is p /4
Integral of the function that is one inside the
circle, zero outside
y
x
17Monte-Carlo computation of p
- The probability is p /4
- Count the inside ratio n inside / total
trials - p ? n 4
- The error depends on the number or trials
Demo
long N_SAMPLES(long)1e9 long nSuccess0
long nTrial0 int pow1 for (long
i0 iltN_SAMPLES i) double
xMath.random() double yMath.random() if
(xxyylt1) nSuccess / if
(nTrialpow0) double myPi4.0(double)nSuc
cess/(double)i System.out.println("n"nTrial
" pi"myPi) pow2 / doubl
e myPi4.0(double)nSuccess/(double)N_SAMPLES S
ystem.out.println(myPi)
18Why not use Simpson integration?
- Yeah, to compute p, Monte Carlo is not very
efficient - But convergence is independent of dimension
- Better to integrate high-dimensional functions
- For d dimensions, Simpson requires Nd domains
19Questions?
- Image from the ARNOLD Renderer by Marcos Fajardo
20Continuous random variables
- Real-valued random variable x
- Probability density function (PDF) p(x)
- Probability of a value between x and xdx is p(x)
dx
21Expected value
- Expected value is linear
- Ef1(x) a f2(x) Ef1(x) a Ef2(x)
22Monte Carlo integration
- Function f(x) of x 2 a b
- We want to compute
- Consider a random variable x
- If x has uniform distribution, IEf(x)
- By definition of the expected value
23Sum of Random Variables
- Use N independent identically-distributed (IID)
variables xi - Share same probability (uniform here)
- Define
- By linearity of the expectationEFN Ef(x)
Monte Carlo estimator
24Variance
- Measure of deviation from expected value
- Expected value of square difference (MSE)
- Standard deviation s square root of variance
(notion of error, RMS)
25Dumbest Monte-Carlo integration
- Compute 0.5 by flipping a coin
- 1 flip 0 or 1gt average error 0.5
- 2 flips 0, 0.5, 0.5 or 1 gtaverage error0. 25
- 4 flips 0 (1),0.25 (4), 0.5 (6), 0.75(4),
1(1) gt average error 0.1875 - Does not converge very fast
- Doubling the number of samples does not double
accuracy
26Study of variance
- Recall s2xy s2x s2y 2 Covx,y
- We have independent variables Covxi, xj0 if i
? j - s2ax a2 s2x
- i.e. stddev s (error) decreases by
27Example
- We know it should be 1.0
- In practicewith uniform samples
error
s2
- s2
N
28Monte-Carlo Recap
- Expected value is the integrand
- Accurate on average
- Variance decrease in 1/N
- Error decreases in
- Good news
- Math are mostly over for today
- OK, its bad news if you like math (and you
should)
29Advantages of MC Integration
- Few restrictions on the integrand
- Doesnt need to be continuous, smooth, ...
- Only need to be able to evaluate at a point
- Extends to high-dimensional problems
- Same convergence
- Conceptually straightforward
- Efficient for solving at just a few points
30Disadvantages of MC
- Noisy
- Slow convergence
- Good implementation is hard
- Debugging code
- Debugging maths
- Choosing appropriate techniques
31Questions?
- Images by Veach and Guibas
Naïve sampling strategy
Optimal sampling strategy
32Smarter sampling
- Sample a non-uniform probability
- Called importance sampling
- how to get probabilities right?
33Example Glossy rendering
- Integrate over hemisphere
- BRDF times cosine times incoming light
Slide courtesy of Jason Lawrence
34Sampling a BRDF
Slide courtesy of Jason Lawrence
35Sampling a BRDF
25 Samples/Pixel
Slide courtesy of Jason Lawrence
36Sampling a BRDF
75 Samples/Pixel
Slide courtesy of Jason Lawrence
37Questions?
1200 Samples/Pixel
Traditional importance function
Better importance by Lawrence et al.
38Stratified sampling
- With uniform sampling, we can get unlucky
- E.g. all samples in a corner
- To prevent it, subdivide domain W into
non-overlapping regions Wi - Each region is called a stratum
- Take one random samples per Wi
39Example
- Borrowed from Henrik Wann Jensen
Unstratified
Stratified
40Stratified sampling - bottomline
- Cheap and effective
- Mostly for low-dimensional domains
- Typical example jittering for antialiasing
- Signal processing perspective better than
uniform because less aliasing (spatial patterns) - Monte-Carlo perspective better than random
because lower variance (error for a given pixel)
41Questions?
- Image from the ARNOLD Renderer by Marcos Fajardo
42Global illumination
- e.g. indirect lighting bouncing off the walls
43The Rendering Equation
x
?'
Kajiya 1986
?
x'
L(x',?') E(x',?') ??x'(?,?')L(x,?)G(x,x')V(x,x
') dA
Incominglight
Geometricterm
visibility
emission
BRDF
44Ray Casting
- Cast a ray from the eye through each pixel
45Ray Tracing
- Cast a ray from the eye through each pixel
- Trace secondary rays (light, reflection,
refraction)
46Monte-Carlo Ray Tracing
- Cast a ray from the eye through each pixel
- Cast random rays from the visible point
- Accumulate radiance contribution
47Monte-Carlo Ray Tracing
- Cast a ray from the eye through each pixel
- Cast random rays from the visible point
- Recurse
48Monte-Carlo
- Cast a ray from the eye through each pixel
- Cast random rays from the visible point
- Recurse
49Monte-Carlo
- Systematically sample primary light
50Results
51Monte Carlo Path Tracing
- Trace only one secondary ray per recursion
- But send many primary rays per pixel
- (performs antialiasing as well)
52Results
Think about it we compute an infinite-dimension
al integral with 10 samples!!!
53Results glossy
54Results glossy
55Importance of sampling the light
Without explicit light sampling
With explicit light sampling
1 path per pixel
4 path per pixel
56Why use random numbers?
- Fixed random sequence
- We see the structure in the error
57Questions?
- Vintage path tracing by Kajyia
58Questions?
59Path Tracing is costly
- Needs tons of rays per pixel
60Direct illumination
61Global Illumination
62Indirect illumination smooth
63Irradiance cache
- The indirect illumination is smooth
64Irradiance cache
- The indirect illumination is smooth
65Irradiance cache
- The indirect illumination is smooth
- Interpolate nearby values
66Irradiance cache
- Store the indirect illumination
- Interpolate existing cached values
- But do full calculation for direct lighting
67Irradiance caching
- Yellow dots computation of indirect diffuse
contribution
68Radiance software by Greg Ward
- The inventor of irradiance caching
- http//radsite.lbl.gov/radiance/
69Photon mapping
- Preprocess cast rays from light sources
- Store photons
70Photon mapping
- Preprocess cast rays from light sources
- Store photons (position light power incoming
direction)
71Photon map
- Efficiently store photons for fast access
- Use hierarchical spatial structure (kd-tree)
72Photon mapping - rendering
- Cast primary rays
- For secondary rays
- reconstruct irradiance using adjacent stored
photon - Take the k closest photons
- Combine with irradiance caching and a number of
other techniques
73Photon map results
74Photon mapping - caustics
- Special photon map for specular reflection and
refraction
Glass sphere
75 76 77Photon mapping
- Animation by Henrik Wann Jensen
78Questions?
79References
- 6.839!
- Eric Veachs PhD dissertationhttp//graphics.stan
ford.edu/papers/veach_thesis/ - Physically Based Rendering by Matt Pharr, Greg
Humphreys
80References
81Questions?