mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Last Time
Participating Media
Assignment 2
A solution program now exists, so you can preview what yoursolution should look like
It uses OpenGL with smooth shading, so you would have toimplement Gourand shading in your ray-tracer to see the same thing
It is OK to use OpenGL to generate the image in this project (butyou’ll get better results if you “gather to a pixel” in a ray-tracer)
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Today
Monte-Carlo introduction
Probability overview
Sampling methods
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Monte Carlo Rendering
Central theme is sampling:
Determine what happens at a set of discrete points, or for a set oflight paths, and extrapolate from there
Central algorithm is ray tracing
Rays from the eye, the lights or other surfaces
Theoretical basis is probability theory
The study of random events
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Distributed Ray Tracing(Cook, Porter, Carpenter 1984)
Arguably the simplest Monte-Carlo method
Addresses the inability of ray tracing to capture:
non-ideal reflection/transmission
soft shadows
motion blur
depth of field
Basic idea: Use multiple sample rays to build the image foreach pixel
Rays are distributed, not the algorithm (should probably becalled distribution ray tracing)
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Specific Cases
Cast multiple rays per pixel, through different lens paths, toget depth-of-field
Sample several directions around the reflected direction toget non-ideal reflection
Send multiple rays to area light sources to get soft shadows
Cast multiple rays per pixel, spread in time, to get motionblur
Must be careful not to get correlated samples, which wouldbe particularly apparent in an image
What should you do for lights?
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Depth-of-Field
Regardless of where they pass through the lens, all the rays from a pointon the focal plane end up on the image
Rays from a non-focal plane point converge somewhere else, resultingin a circle on the image plane
Note that the aperture also allows a sampling of directions through aworld point
Focal plane
Lens
Aperture
Image plane
Out of Focal plane givescircle of confusion
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Distributed Depth-of-Field
Sample multiple rays through the aperture from the image point
If there are objects outside the focal plane, different rays will hitdifferent points, and contribute different colors
Also cast rays from within area of pixel
Average all rays to get resulting pixel color
Lens
Aperture
Image plane
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Distributed Directional Reflectance
Cast multiple rays to determine what is reflected in a surface
Rays should be sampled according to the BRDF
Average contribution to get pixel color
Why is this a poor strategy for very diffuse surfaces?
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Better Approaches
Distributed ray tracing would need to cast extremely denseray-trees to get good diffuse effects
Other approaches improve upon this:
Don’t cast complete trees, just single paths
Observe that the same point may be hit many times, but distributedray-tracing does not ever re-use partial ray-trees
But first, some math…
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Random Variables
A random variable, x
Takes values from some domain, 
Has an associated probability density function, p(x)
A probability density function, p(x)
Is positive over the domain, 
“Integrates” to 1 over  :
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Expected Value
The expected value of a random variable, x, is defined as:
The expected value of a function, f(x), is defined as:
The sample mean, for samples xi is defined as:
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Variance and Standard Deviation
The variance of a random variable is defined as:
The standard deviation of a random variable is defined asthe square root of its variance:
The sample variance is:
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Sampling
A process samples according to the distribution p(x) if it randomlychooses a value for x such that:
Weak Law of Large Numbers: If xi are independent samples from p(x),then in the limit of infinite samples, the sample mean is equal to theexpected value:
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Algorithms for Sampling
Psuedo-random number generators give independentsamples from the uniform distribution on [0,1), p(x)=1
Transformation method: take random samples from theuniform distribution and convert them to samples fromanother
Rejection sampling: sample from a different distribution,but reject some samples
Importance sampling (to compute an expectation): Samplefrom an easy distribution, but weight the samples
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Distribution Functions
Assume a density function f(y) defined on [a,b]
Define the probability distribution function, or cumulativedistribution function, as:
Monotonically increasing function with F(a)=0 and F(b)=1
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Transformation Method for 1D
Generate zi uniform on [0,1)
Compute
Then the xi are distributed according to f(x)
To apply, must be able to compute and invert distributionfunction F
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Multi-Dimensional Transformation
Assume density function f(x,y) defined on [a,b][c,d]
Distribution function:
Sample xi according to:
Sample yi according to:
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Rejection Sampling
Say we wish to sample xi according to f(x)
Find a function g(x) such that g(x)>f(x) for all in thedomain
Geometric interpretation: generate sample uniform in thearea under g, and accept if also under f
Transform a weighted uniform sample according to xi=G-1(z), and generate yi uniform on [0,g(xi)]
Keep the sample if yi<f(x), otherwise reject
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Important Example
Consider uniformly sampling a point on a sphere
Uniformly means that the probability that the point is in a regiondepends only on the area of the region, not its location on the sphere
Generate points inside a cube [-1,1]x[-1,1]x[-1,1]
Reject if the point lies outside the sphere
Push accepted point onto surface
Fraction of pts accepted: /6
Bad strategy in higher dimensions
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Estimating Integrals
Say we wish to estimate
Write h=gf, where f is something you choose
If we sample xi according to f, then:
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Standard Deviation of the Estimate
Expected error in the estimate after n samples is measured by the standarddeviation of the estimate:
Note that error goes down with
This technique is called importance sampling
f should be as close as possible to g
Same principle for higher dimensional integrals
 
mediumWiscHandSketched
02/10/03
© 2003 University of Wisconsin
Example: Form Factor Integrals
We wish to estimate
Sample from f by sampling xi uniformly on Pi and yiuniformly on Pj
Define
Estimate is