Fundamentals of Computer Graphics -...

40
“cg” 2002/8 page 1 Fundamentals of Computer Graphics Peter Shirley School of Computing University of Utah August 22, 2002

Transcript of Fundamentals of Computer Graphics -...

Page 1: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page1

i

i

i

i

i

i

i

i

Fundamentals of Computer Graphics

Peter ShirleySchool of Computing

University of Utah

August 22, 2002

Page 2: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page2

i

i

i

i

i

i

i

i

2

Page 3: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page3

i

i

i

i

i

i

i

i

Chapter 1

Measure and Sampling

Many applications in graphics require “fair” sampling of unusual spaces such asthe space of all possible lines. For example, we might need to generate randomedges within a pixel, or random sample points on a pixel that vary in densityaccording to some density function. This chapter provides the machinery for suchoperations: basic measure theory and probability theory. These two areas areclosely related, and some would argue are really one area, so the discussion willnot be tightly segregated. This will also prove useful for numerically evaluatingcomplicated integrals usingMonte Carlo integration, also covered in this chapter.

1.1 Integrals and Measure

Although the words “integral” and “measure” often seem to intimidate students,they relate to some of the most intuitive concepts found in mathematics, and theyshould not be feared. For our very non-rigorous purposes, ameasureis just afunction that maps subsets toR

+ in a manner consistent with our intuitive notionsof length, area, and volume. For example, on the 2D real planeR

2, we have thearea measureA which takes sets of points in the plane. Note thatA is just afunction that takes pieces of the plane and returns area. This means the domain ofA is all possible subsets ofR2, which we denote as thepower setP(R2). Thuswe can characterizeA in arrow notation:

A : P(R2) → R+.

An example of applying the area measure is that the area of the square with sidelength one is one:

A([a, a + 1]× [b, b + 1]) = 1,

where(a, b) is just the lower-lefthand corner of the square. Note that a single pointsuch as(3, 7) is a valid subset ofR2 and has zero area:A((3, 7)) = 0. The same is

3

Page 4: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page4

i

i

i

i

i

i

i

i

4 CHAPTER 1. MEASURE AND SAMPLING

true of the set of pointsS on the x-axis,S = (x, y) such that(x, y) ∈ R2 andy = 0,

i.e.,A(S) = 0. Such sets are calledzero measure sets.To be considered a measure, a function has to obey certain area-like properties.

For example, we have a functionµ : P(S) → R+. To be a measure we need:

1. The measure of the empty set is zero:µ(∅) = 0,

2. The measure of two distinct sets together is the sum of their measure alone.This rule with possible intersections is:µ(A∪B) = µ(A)+µ(B)−µ(A∩B), where∪ is the set union operator and∩ is the set intersection operator.

When we actually compute measures we usually useintegration. We can think ofintegration is really just notation:

A(S) ≡∫

x∈S

dA(x).

You can informally read the right hand side as “take all pointsx in the regionS,and sum their associated differential areas”. The integral is often written otherways including: ∫

S

dA,

∫x∈S

dx,

∫x∈S

dAx,

∫x

dx.

All of the above formulas can be read at a high level “the area of regionS”. Wewill stick with the first one we used because it is so verbose it avoids ambiguity. Toevaluate such integrals analytically, we usually need to lay down some coordinatesystem and use our bag of calculus tricks to solve the equations. But have no fearif those skills have faded as we usually have to numerically approximate integrals,and that requires only a few simple techniques as is covered later in this chapter.

Given a measure on a setS, we can always create a new measure by weightingwith a non-negative functionw : S → R

+. This is best expressed in the integralnotation. For example, we can start with the example of the simple area measureon [0, 1]2: ∫

x∈[0,1]2dA(x),

Page 5: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page5

i

i

i

i

i

i

i

i

1.1. INTEGRALS AND MEASURE 5

and we can use a “radially weighted” measure by inserting a weighting functionof radius squared: ∫

x∈[0,1]2‖x‖2dA(x).

To evaluate this analytically you could expand with a Cartesian coordinate systemwith dA ≡ dx dy:∫

x∈[0,1]2‖x‖2dA(x) =

∫ 1

x=0

∫ 1

y=0

(x2 + y2) dx dy.

The key thing here is that if you think of the‖x‖2 term as married to thedAterm, and these together form a new measure we could callν. This would allowus to writeν(S) instead of the whole integral. If this strikes you as just a bunchof notation and bookkeeping you are right. But it does allow us to write downequations that are either compact or expanded depending on whether you want tothink at a high or low level.

1.1.1 Measures and Averages

One area in which measures really start paying off is taking averages !of a func-tion. You can only take an average with respect to a particular measure. Youwould like to select a measure that is “natural” for the application or domain.Once a measure is chosen, the average of a functionf over a regionS with re-spect to measureµ is:

average(f) ≡∫

x∈Sf(x) dµ(x)∫

x∈Sdµ(x)

.

For example, the average of the functionf(x, y) = x2 over[0, 2]2 with respect tothe area measure is

average(f) ≡∫ 2

x=0

∫ 2

y=0x2 dx dy∫ 2

x=0

∫ 2

y=0dx dy

=43.

Where this machinery helps you solve seemingly hard problems is where choos-ing the measure is the tricky part. Such problems often arise inintegral geometry.That field studies measures on geometric entities such as lines and planes. Forexample, one might want to know what the average length of line through[0, 1]2

is. That is by definition

average(length)=

∫linesL through[0, 1]2

length(L)dµ(L)∫linesL through[0, 1]2

dµ(L).

All that is left once we know that is choosing the appropriateµ for the application.This is dealt with for lines in the next section.

Page 6: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page6

i

i

i

i

i

i

i

i

6 CHAPTER 1. MEASURE AND SAMPLING

1.1.2 Example: Measures on the Lines in the 2D Plane

The question is what measureµ is “natural”? If you parameterize your lines asy = mx+ b, you might think of a given line as a point(m, b) in “slope-intercept”space. An easy measure to use would bedmdb, but this would not be a “good”measure in that not all equal size “bundles” of lines would have the same measure.More precisely, the measure would not be invariant with respect to change ofcoordinate system. For example, if you took all lines through the square[0, 1]2,the measure of lines through it would not be the same as the measure througha unit square rotated forty five degrees. What we would really like is a “fair”measure that does not change with rotation or translation of a set of lines. Thisidea is illustrated in Figures 1.1 and 1.2.x

y

Figure 1.1: These twobundles of lines shouldhave the same measure,but have different intersec-tion lengths with the y axisso using db would be apoor choice for a differen-tial measure.

x

y

Figure 1.2: These twobundles of lines shouldhave the same measure,but have different amountsof change in slope. There-fore using dm would be apoor choice for a differen-tial measure.

To develop a natural measure on the lines, we should first start thinking ofthem as points in a dual space. This is a simple concept: the liney = mx + bcan be specified as the point(m, b) in a slope-intercept point. This concept isillustrated in Figure 1.3. It is more straightforward to develop a measure in(φ, b)space. In that spaceb is they-intercept, whileφ is the angle the line makes withthe x-axis, as shown in Figure 1.4. Here the differential measuredφ db almostworks, but would be unfair due the the effect shown in Figure 1.1. To account forthe larger span ab a constant width bundle of lines makes we must add a cosinefactor:

dµ = cos φ dφ db.

It can be shown that this measure, up to a constant, is the only one that is invariantwith respect to rotation and translation.

This measure can be converted into an appropriate measure for other param-eterizations of the line. For example, the appropriate measure for(y, b) spaceis:

dµ =dm db

(1 + m2)32.

For the space of lines parameterized in(u, v) space:

ux + vy + 1 = 0,

the appropriate measure is:

dµ =du dv

(u2 + v2)32.

For lines parameterized in terms of(a, b), the x-intercept andy-intercept, themeasure is:

dµ =ab da db

(a2 + b2)32.

Note that any of those spaces are equally valid ways to specify lines, and whichis best depends upon circumstances. However, one might wonder whether there

Page 7: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page7

i

i

i

i

i

i

i

i

1.1. INTEGRALS AND MEASURE 7

exists a coordinate system where the measure of a set of lines are just an areain the dual space. In fact there is such a coordinate system, and it is delightfullysimple; this is thenormal coordinateswhich specifies a line in terms of the normaldistance from the origin to the line, and the angle the normal of the line makeswith respect to thex-axis (Figure 1.5). The implicit equation for such lines is:

x cos θ + y sin θ − p = 0.

And indeed the measure in that space is:

dµ = dp dθ.

We shall use these measures to choose fair random lines in a later section.

x

y

m

b

y = 0.5x+1

(0.5,1)

Figure 1.3: The set ofpoints on the line y = mx+b in (x, y) space can alsobe represented by a sin-gle point in (m, b) space sothe top line and the bottompoint represent the samegeometric entity: a 2D line.

x

y

φ

Figure 1.4: In angle-intercept space we param-eterize the line by angleφ ∈ [−pi/2, pi/2) ratherthan slope.

x

y

θ

p

Figure 1.5: The normal co-ordinates of a line use thenormal distance to the ori-gin and an angle to specifya line.

1.1.3 Example: Measure of Lines in 3D

In 3D there are many ways to parameterize lines. Perhaps the simplest is to usetheir intersection with a particular plane along with some specification of theirorientation. For example, we could chart the intersection with thexy plane alongwith the spherical coordinates of its orientation. Thus each line would be specifiedas a(x, y, θ, φ) quadruple. This shows that lines in 3D are 4D entities, i.e., theycan be described as points in a 4D space.

The differential measure of a line should not vary with(x, y), but bundles oflines with equal cross-section should have equal measure. Thus a fair differentialmeasure is:

dµ = dx dy sin θ dθ dφ.

Another way to parameterize lines is to chart the intersection with two parallelplanes. For example, if the line intersects the planez = 0 at (x = u, y = v) andthe planez = 1 at (x = s, y = t) then the line can be described by the quadruple(u, v, s, t). Note that like the previous parameterization, this one is degenerate forlines parallel to thexy plane. The differential measure is more complicated forthis parameterization although it can be approximated as:

dµ ≈ du dv a ds dt,

For bundles of lines nearly parallel to thez axis. This is the measure often im-plicitly used in image-based rendering (Chapter??).

For sets of lines that intersect a sphere, we can use the parameterization ofthe two points where the line intersects the sphere. If these are in spherical coor-dinates then the point can be described by the quadruple(θ1, φ1, θ2, φ2) and themeasure is just the differential area associated with each point:

dµ = sin θ1 dθ1 dφ1 sin θ2 dθ2 dφ2.

This implies picking two uniform random endpoints on the sphere results in aline with uniform density. This observation was used to compute form-factors byMateu Sbert in his dissertation.

Page 8: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page8

i

i

i

i

i

i

i

i

8 CHAPTER 1. MEASURE AND SAMPLING

Note that for sometimes we want to parameterize directed lines, and some-times we want the order of the endpoints not to matter. This is a bookkeepingdetail that is especially important for rendering applications where the amount oflight flowing along a line is different in the two directions along the line.

1.2 Continuous Probability

Many graphics algorithms use probability to construct random samples to solveintegration and averaging problems. This is the domain of applied continuousprobability which has basic connections to measure theory.

1.2.1 One-dimensional Continuous Probability Density Func-tions

Loosely speaking, acontinuous random variablex is a scalar or vector quantitythat “randomly” takes on some value from the real lineR = (−∞,+∞). Thebehavior ofx is entirely described by the distribution of values it takes. Thisdistribution of values can be quantitatively described by theprobability densityfunction, p, associated withx (the relationship is denotedx ∼ p). The probabilitythatx will take on a value in some interval[a, b] is given by the integral:

Probability(x ∈ [a, b]) =∫ b

a

p(x)dx. (1.1)

Loosely speaking, the probability density functionp describes the relative likeli-hood of a random variable taking a certain value; ifp(x1) = 6.0 andp(x2) = 3.0,then a random variable with densityp is twice as likely to have a value “near”x1

than it it to have a value nearx2. The densityp has two characteristics:

p(x) ≥ 0 (Probability is nonnegative), (1.2)

∫ +∞

−∞p(x)dx = 1 (Probability(x ∈ R) = 1). (1.3)

As an example, thecanonicalrandom variableξ takes on values between zero(inclusive) and one (non-inclusive) with uniform probability (hereuniformsimplymeans each value forξ is equally likely). This implies that the probability densityfunctionq for ξ is:

q(ξ) =

1 if 0 ≤ ξ ≤ 10 otherwise

The space over whichξ is defined is simply the interval[0, 1). The probabilitythatξ takes on a value in a certain interval[a, b] ∈ [0, 1) is:

Probability(a ≤ ξ ≤ b) =∫ b

a

1 dx = b− a.

Page 9: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page9

i

i

i

i

i

i

i

i

1.2. CONTINUOUS PROBABILITY 9

1.2.2 One-dimensional Expected Value

The average value that a real functionf of a one dimensional random variablewith underlying pdfp will take on is called itsexpected value, E(f(x)) (some-times writtenEf(x)):

E(f(x)) =∫

f(x)p(x)dx.

The expected value of a one dimensional random variable can be calculated byletting f(x) = x. The expected value has a surprising and useful property: theexpected value of the sum of two random variables is the sum of the expectedvalues of those variables:

E(x + y) = E(x) + E(y),

for random variablesx andy. Because functions of random variables are them-selves random variables, this linearity of expectation applies to them as well:

E(f(x) + g(y)) = E(f(x)) + E(g(y)).

An obvious question is whether this property holds if the random variables beingsummed are correlated (variables that are not correlated are calledindependent).This linearity property in fact does holdwhether or notthe variables are indepen-dent! This summation property is vital for most Monte Carlo applications.

1.2.3 Multi-dimensional Random Variables

The discussion of random variables and their expected values extends naturallyto multidimensional spaces. Most graphics problems will be in such higher-dimensional spaces. For example, many lighting problems are phrased on thesurface of the hemisphere. Fortunately, if we define a measureµ on the space therandom variables occupy, everything is very similar to the one-dimensional case.Suppose the spaceS has associated measureµ, for exampleS is the surface of asphere andµ measures area. We can define a pdfp : S 7→ R, and ifx is a randomvariable withx ∼ p, then the probability thatx will take on a value in some regionSi ⊂ S is given by the integral:

Probability(x ∈ Si) =∫

Si

p(x)dµ

HereProbabilty(event)is the probability thateventis true, so the integral is theprobability thatx takes on a value in the regionSi.

In graphicsS is often an area (dµ = dA = dxdy), or a set of directions (pointson a unit sphere:dµ = dω = sin θ dθ dφ). As an example, a two dimensionalrandom variableα is a uniformly distributed random variable on a disk of radius

Page 10: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page1

i

i

i

i

i

i

i

i

10 CHAPTER 1. MEASURE AND SAMPLING

R. Hereuniformlymeans uniform with respect to area, e.g., the way a bad dartplayer’s hits would be distributed on a dart board. Since it is uniform, we knowthatp(α) is some constant. From the fact that the area of the disk isπr2 and thatthe total probability is one, we can deduce that:

p(α) =1

πR2.

This means that the probability thatα is in a certain subsetS1 of the disk is just:

Probability(α ∈ S1) =∫

S1

1πR2

dA.

This is all very abstract. To actually use this information we need the integral ina form we can evaluate. SupposeSi is the portion of the disk closer to the centerthan the perimeter. If we convert to polar coordinates, thenα is represented as a(r, φ) pair, andS1 is wherer < R/2. Note that just becauseα is uniform doesnot imply thatθ or r are necessarily uniform (in fact,θ is, andr is not uniform).The differential areadA becomesr dr dφ. This leads to:

Probability

(r <

R

2

)=∫ 2π

0

∫ R2

0

1πR2

r dr dφ = 0.25.

The formula for expected value of a real function applies to the multidimensionalcase:

E(f(x)) =∫

S

f(x)p(x)dµ,

Wherex ∈ S andf : S 7→ R, andp : S 7→ R For example, on the unit squareS = [0, 1] × [0, 1] andp(x, y) = 4xy, the expected value of thex coordinate for(x, y) ∼ p is:

E(x) =∫

S

f(x, y)p(x, y)dA

=∫ 1

0

∫ 1

0

4x2y dx dy

=23

Note that heref(x, y) = x.

1.2.4 Variance

The variance, V (x), of a one dimensional random variable is by definition theexpected value of the square of the difference betweenx andE(x):

V (x) ≡ E([x− E(x)]2).

Page 11: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page1

i

i

i

i

i

i

i

i

1.3. MONTE CARLO INTEGRATION 11

Some algebraic manipulation can give the non-obvious expression:

V (x) = E(x2)− [E(x)]2 .

The expressionE([x− E(x)]2) is more useful for thinking intuitively about vari-ance, while the algebraically equivalent expressionE(x2) − [E(x)]2 is usuallyconvenient for calculations. The variance of a sum of random variables is thesum of the variancesif the variables are independent. This summation propertyof variance is one of the reasons it is frequently used in analysis of probabilisticmodels. The square root of the variance is called thestandard deviation, σ, whichgives some indication of expected absolute deviation from the expected value.

1.2.5 Estimated Means

Many problems involve sums of independent random variablesxi, where the vari-ables share a common densityp. Such variables are said to beindependent identi-cally distributed(iid) random variables. When the sum is divided by the numberof variables, we get an estimate ofE(x):

E(x) ≈ 1N

N∑i=1

xi.

As N increases, the variance of this estimate decreases. We wantN to be largeenough that we have confidence that the estimate is “close enough”. However,there are no sure things in Monte Carlo; we just gain statistical confidence thatour estimate is good. To be sure, we would have to haven = ∞. This confidenceis expressed byLaw of Large Numbers:

Probability

[E(x) = lim

N→∞1N

N∑i=1

xi

]= 1.

1.3 Monte Carlo Integration

In this section the basic Monte Carlo solution methods for definite integrals areoutlined. These techniques are then straightforwardly applied to certain integralproblems. All of the basic material of this section is also covered in several of theclassic Monte Carlo texts.

As discussed earlier, given a functionf : S 7→ R and a random variablex ∼ p, we can approximate the expected value off(x) by a sum:

E(f(x)) =∫

x∈S

f(x)p(x)dµ ≈ 1N

N∑i=1

f(xi). (1.4)

Page 12: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page1

i

i

i

i

i

i

i

i

12 CHAPTER 1. MEASURE AND SAMPLING

Because the expected value can be expressed as an integral, the integral is alsoapproximated by the sum. The form of Equation 1.4 is a bit awkward; we wouldusually like to approximate an integral of a single functiong rather than a productfp. We can get around this by substitutingg = fp as the integrand:

∫x∈S

g(x)dµ ≈ 1N

N∑i=1

g(xi)p(xi)

. (1.5)

For this formula to be valid,p must be positive whereg is nonzero.So to get a good estimate, we want as many samples as possible, and we want

theg/p to have a low variance (g andp should have a similar shape). Choosingp intelligently is calledimportance sampling, because if p is large whereg islarge, there will be more samples in important regions. Equation 1.4 also showsthe fundamental problem with Monte Carlo integration:diminishing return. Be-cause the variance of the estimate is proportional to1/N , the standard deviationis proportional to1/

√N . Since the error in the estimate behaves similarly to the

standard deviation, we will need to quadrupleN to halve the error.Another way to reduce variance is to partitionS, the domain of the integral,

into several smaller domainsSi, and evaluate the integral as a sum of integralsover theSi. This is calledstratified sampling, the technique that jittering employsin pixel sampling (Chapter??). Normally only one sample is taken in eachSi

(with densitypi), and in this case the variance of the estimate is:

var

(N∑

i=1

g(xi)pi(xi)

)=

N∑i=1

var

(g(xi)pi(xi)

). (1.6)

It can be shown that the variance of stratified sampling is never higher than un-stratified if all strata have equal measure:∫

Si

p(x)dµ =1N

∫S

p(x)dµ.

The most common example of stratified sampling in graphics is jittering for pixelsampling as discussed in Section??.

As an example of the Monte Carlo solution of an integralI setg(x) to bexover the interval (0, 4):

I =∫ 4

0

x dx = 8. (1.7)

The great impact of the shape of the functionp on the variance of theN sampleestimates is shown in Table 1.1. Note that the variance is lessened when theshape ofp is similar to the shape ofg. The variance drops to zero ifp = g/I,but I is not usually known or we would not have to resort to Monte Carlo. Oneimportant principle illustrated in Table 1.1 is that stratified sampling is oftenfarsuperior to importance sampling. Although the variance for this stratification onI

Page 13: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page1

i

i

i

i

i

i

i

i

1.4. CHOOSING RANDOM POINTS 13

method sampling function variance samples needed forstandard error of 0.008

importance (6− x)/(16) 56.8N−1 887,500importance 1/4 21.3N−1 332,812importance (x + 2)/16 6.3N−1 98,437importance x/8 0 1stratified 1/4 21.3N−3 70

Table 1.1: Variance for Monte Carlo Estimate of∫ 4

0x dx

is inversely proportional to the cube of the number of samples, there is no generalresult for the behavior of variance under stratification. There are some functionswhere stratification does no good. An example is a white noise function, wherethe variance is constant for all regions. On the other hand, most functions willbenefit from stratified sampling because the variance in each subcell will usuallybe smaller than the variance of the entire domain.

1.3.1 Quasi-Monte Carlo Integration

A popular method for quadrature is to replace the random points in Monte Carlointegration withquasi-randompoints. Such points are deterministic, but are insome sense uniform. For example, on the unit square[0, 1]2, a set ofN quasi-random points should have the following property on a region of areaA withinthe square:

number of points in the region≈ AN.

A set of regular samples in a lattice has this property.Quasi-random points can improve performance in many integration applica-

tions. Sometimes care must be taken to make sure they do not introduce aliasing.What is especially nice is that in any application where calls are made to ran-dom or stratified points in[0, 1]d, one can substituted-dimensional quasi-randompoints with no other changes.

The key intuition behind why quasi-Monte Carlo integration works is thatwhen estimating the average value of an integrand, any set of sample points willdo provided they are “fair”.

1.4 Choosing Random Points

We often want to generate sets of random or pseudorandom points on the unitsquare for applications such as distribution ray tracing. There are several methodsfor doing this such as jittering (see Section??). These methods give us a set ofN reasonably equidistributed points on the unit square[0, 1]2 : (u1, v1) through(uN , vN ).

Page 14: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page1

i

i

i

i

i

i

i

i

14 CHAPTER 1. MEASURE AND SAMPLING

Sometimes, our sampling space may not be square (e.g., a circular lens), ormay not be uniform (e.g, a filter function centered on a pixel). It would be nice ifwe could write a mathematical transformation that would take our equidistributedpoints(ui, vi) as input, and output a set of points in our desired sampling spacewith our desired density. For example, to sample a camera lens, the transforma-tion would take(ui, vi) and output(ri, φi) such that the new points were approx-imately equidistributed on the disk of the lens. While we might be tempted to usethe transform:

φi = 2πui,

ri = viR,

it has a serious problem. While the points do cover the lens, they do so nonuni-formly (Figure 1.6). What we need in this case is a transformation that takesequal-area regions to equal-area regions. That will take uniform sampling distri-butions on the square to uniform distributions on the new domain.

Figure 1.6: The trans-form that takes the horizon-tal and vertical dimensionsuniformly to (r, φ) does notpreserve relative area; notall of the resulting areasare the same.

There are several ways to generate such non-uniform points, or uniform pointson non-rectangular domains, and this section reviews the three most often used:function inversion, rejection, and Metropolis.

1.4.1 Function Inversion

If the density is a one dimensionalf(x) defined over the intervalx ∈ [xmin, xmax],then we can generate random numbersαi that have densityf from a set of uni-form random numbersξi, whereξi ∈ [0, 1]. To do this we need the cumulativeprobability distribution functionP (x):

Probability(α < x) = P (x) =∫ x

xmin

f(x′)dµ

To getαi we simply transformξi:

αi = P−1(ξi)

whereP−1 is the inverse ofP . If P is not analytically invertible then numericalmethods will suffice because an inverse exists for all valid probability distributionfunctions.

Note that analytically inverting a function is more confusing than it should bedue to notation. For example, if we have the function

y = x2,

for x > 0, then the inverse function is just in terms ofy as a function ofx:

x =√

y.

Page 15: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page1

i

i

i

i

i

i

i

i

1.4. CHOOSING RANDOM POINTS 15

When the function is analytically invertible, it is almost always that simple. How-ever, things are a little more opaque with the standard notation:

f(x) = x2,

f−1(x) =√

x.

Herex is just a dummy variable. You may find it easier to use the less standardnotation:

y = x2,

x =√

y,

while keeping in mind these are inverse functions of each other.For example, to choose random pointsxi that have the density

p(x) =3x2

2

on [−1, 1], we see that

P (x) =x3 + 1

2,

andP−1(x) = 3

√2x− 1,

so we can “warp” a set of canonical random numbers(ξ1, · · · , ξN ) to the properlydistributed numbers

(x1, · · · , xN ) = ( 3√

2ξ1 − 1, · · · , 3√

2ξN − 1).

Of course, this same warping function can be used to transform “uniform” jitteredsamples into nicely distributed samples with the desired density.

If we have a random variableα = (αx, αy) with two dimensional density(x, y) defined on[xmin, xmax]× [ymin, ymax] then we need the two dimensionaldistribution function:

Probability(αx < x andαy < y) = F (x, y) =∫ y

ymin

∫ x

xmin

f(x′, y′)dµ(x′, y′)

We first choose anxi using the marginal distributionF (x, ymax), and then chooseyi according toF (xi, y)/F (xi, ymax). If f(x, y) is separable (expressible asg(x)h(y)), then the one dimensional techniques can be used on each dimension.

Returning to our earlier example, suppose we are sampling uniformly fromthe disk of radiusR, sop(r, φ) = 1/(πR2). The two dimensional distributionfunction is:

Probability(r < r0 and φ < φ0) = F (r0, φ0) =∫ φ0

0

∫ r0

0

rdrdφ

πR2=

φr2

2πR2

Page 16: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page1

i

i

i

i

i

i

i

i

16 CHAPTER 1. MEASURE AND SAMPLING

This means that a canonical pair(ξ1, ξ2) can be transformed to a uniform randompoint on the disk:

φ = 2πξ1,

r = R√

ξ2.

This mapping is shown in Figure 1.7.

Figure 1.7: A mapping thattakes equal area regionsin the unit square to equalarea regions in the disk.

To choose reflected ray directions for some realistic rendering applications,we choose points on the unit hemisphere according to the density:

p(θ, φ) =n + 12π

cosn θ

Wheren is a Phong-like exponent,θ is the angle from the surface normal andθ ∈[0, π/2] (is on the upper hemisphere) andφ is the azimuthal angle (φ ∈ [0, 2π]).The cumulative distribution function is:

P (θ, φ) =∫ φ

0

∫ θ

0

p(θ′, φ′) sin θ′dθ′dφ′ (1.8)

Thecos θ′ term arises because on the spheredω = cos θdθdφ. When the marginaldensities are found,p (as expected) is separable and we find that a(ξ1, ξ2) pair ofcanonical random numbers can be transformed to a direction by:

θ = arccos((1− r1)

1n+1

)φ = 2πr2

Again, a nice thing about this is that a set of jittered points on the unit square canbe easily transformed to a set of jittered points on the hemisphere with the desireddistribution. Note that ifn is set to1 then we have a diffuse distribution as is oftenneeded.

Often we must map the point on the sphere into an appropriate direction withrespect to auvwbasis. To do this we can first convert the angles to a unit vectora:

a = (cos φ sin θ, sin φ sin θ, cos θ)

As an efficiency improvement, you can avoid taking trigonometric functions ofinverse trigonometric functions (e.g.cos arccos θ). For example, whenn = 1 (adiffuse distribution), the vector~a simplifies to

a =(cos (2πξ1)

√ξ2, sin (2πξ1)

√ξ2,√

1− ξ2

)

1.4.2 Rejection

A rejectionmethod chooses points according to some simple distribution and re-jects some of them so that they are in a more complex distribution. There are

Page 17: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page1

i

i

i

i

i

i

i

i

1.4. CHOOSING RANDOM POINTS 17

several scenarios where rejection is used, and we show several of these by exam-ple.

Suppose we want uniform random points within the unit circle. We can firstchoose uniform random points(x, y) ∈ [−1, 1]2 and reject those outside the cir-cle. If the functionr() returns a canonical random number, then the procedure forthis is:

done = falsewhile (not done)do

x = −1 + 2r()y = −1 + 2r()if (x2 + y2 < 1) then

done = true

If we want a random numberx ∼ p and we know thatp : [a, b] 7→ R, andthat for all x, p(x) < m, then we can generate random points in the rectangle[a, b]× [0,m] and take those wherey < p(x):

done = falsewhile (not done)do

x = a + r()(b− a)y = r()mif (y < p(x)) then

done = true

A variant of the last methods is common because we can often deal more eas-ily with boxes than spheres. To pick a random unit vector with uniform di-rectional distribution, we first pick a random point in the unit sphere and thentreat that point as a direction vector by taking the unit vector in the same direc-tion:

done = falsewhile (not done)do

x = −1 + 2r()y = −1 + 2r()z = −1 + 2r()if ( (l =

√x2 + y2 + z2) < 1) then

done = truex = x/ly = y/lz = z/l

Although the rejection method is usually simple to code, it is rarely compatiblewith stratification. For this reason, it tends to converge more slowly and shouldthus be used only for debugging on in particularly difficult circumstances.

Page 18: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page1

i

i

i

i

i

i

i

i

18 CHAPTER 1. MEASURE AND SAMPLING

1.4.3 Metropolis

TheMetropolismethod uses randommutationsto produce a set of samples witha desired density. This concept is used extensively in theMetropolis Light Trans-port algorithm referenced in the chapter notes. Suppose we have a random pointx0 in a domainS. Further, suppose for any pointx we have a way to generaterandomy ∼ px. We use the marginal notationpx(y) ≡ p(x → y) to denote thisdensity function. Now suppose we letx1 be a random point inS selected withunderlying densityp(x0 → x1). We generatex2 with densityp(x1 → x0) and soin. In the limit where we generate an infinite number of samples, it can be proventhat the samples with have some underlying density determined byp regardlessof the initial pointx0.

Now suppose we want to chosep such that the underlying density of sam-ples we converge to is proportional to a functionf(x) wheref is a non-negativefunction with domainS. Further, suppose we can evaluatef but we have littleor no additional knowledge about its properties (such functions are common ingraphics). Also suppose we have the ability to make “transitions” fromxi to xi+1

with underlying density functiont(xi → xi+1). To add flexibility, further sup-pose we add the potentially non-zero probability thatxi transitions to itself, i.e.,xi+1 = xi. We phrase this as generating a potential candidatey ∼ t(xi → y) and“accepting” this candidate (i.e.,xi+1 = y) with probabilitya(xi → y) and reject-ing it (i.e., xi+1 = xi) with probability1 − a(xi → y). Note that the sequencex0, x1, x2, . . . will be a random set, but there will be some correlation among sam-ples. They will still be suitable for Monte Carlo integration or density estimation,but analyzing the variance of those estimates is much more challenging.

Now suppose that given a transition functiont(x → y) and a functionf(x)we want to mimic the distribution of, can we usea(y → x) such that the pointsare distributed in the shape off , or more precisely:

x0, x1, x2, . . . ∼ f∫sf

It turns out this can be forced by making sure thexi arestationaryin some strongsense. If you visualize a huge collection of sample pointsx, you want the “flow”between two points to be the same in each direction. If we assume the density ofpoints nearx andy are proportional tof(x) andf(y) respectively, the the flow inthe two directions as follows should be the same:

flow(x → y) = kf(x)t(x → y)a(x → y)flow(y → x) = kf(y)t(y → x)a(y → x)

wherek is some positive constant. Setting these two flows constant gives a con-straint ona:

a(y → x)a(x → y)

=f(x)t(x → y)f(y)t(y → x)

.

Page 19: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page1

i

i

i

i

i

i

i

i

1.4. CHOOSING RANDOM POINTS 19

Thus if eithera(y → x) or a(x → y) is known, so is the other. Making thembigger improves the chance of acceptance, so the usual technique is to set thelarger of the two to1.

An awkward part of using the Metropolis sample generation technique is thatit is hard to estimate how many points are needed before the set of points is“good”. Things are accelerated if the firstn points are discarded, although choos-ing n wisely is non-trivial.

1.4.4 Example: Choosing Random Lines in the Square

As an example of the full process of designing a sampling strategy, consider theproblem of finding random lines that intersect the unit square[0, 1]2. We want thisprocess to be fair, that is we would like the lines to be uniformly distributed withinthe square. Intuitively we can see that there is some subtlety to this problem; thereare “more” lines at an oblique angle than in horizontal or vertical directions. Thisis because the cross-section of the square is not uniform.

x

y

θr

Figure 1.8: The largestdistance r corresponds toa line hitting the square forθ ∈ [−π/2, 0]. Becausethe the square has side-length one, r = cos θ.

Our first goal is to find a function-inversion method if one exists, and thento fall back on rejection or Metropolis if that fails. This is because we wouldlike to have stratified samples in line space. Using normal coordinates is whatwe might try first because the problem of choosing random lines in the square isjust the problem of finding uniform random points in whatever part of(r, θ) spacecorresponds to lines in the square. Consider the region where−π/2 < θ < 0.What values ofr correspond to lines that hit the square? For those angles,r <cos θ are all lines that hit the square as shown in Figure 1.8. Similar reasoning inthe other four quadrants finds the region in(r, θ) space that must be sampled, asshown in Figure 1.9. The equation the boundary of that regionrmax(θ)is:

rmax(θ) =

0 if θ ∈ [−π,−π2 ]

cos θ if θ ∈ [−π2 , 0]

cos θ + sin θ if θ ∈ [0, π2 ]

sin θ if θ ∈ [π2 , π]

Because the region underrmax(θ) is a simple function bounded below byr = 0,we can sample it by first choosingθ according to the density function:

p(θ) =rmax(θ)∫ π

−πrmax(θ)dθ

The denominator there is4. Now we can compute the cumulative probabilitydistribution function to be:

P (θ) =

0 if θ ∈ [−π,−π2 ]

(1 + sin θ)/4 if θ ∈ [−π2 , 0]

(2 + sin θ − cosθ)/4 if θ ∈ [0, π2 ]

(3− cos θ)/4 if θ ∈ [π2 , π]

Page 20: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page2

i

i

i

i

i

i

i

i

20 CHAPTER 1. MEASURE AND SAMPLING

θ

r

-π/2 π/2 π

r=cosθ

r=cosθ+sinθ

r=sinθ

Figure 1.9: The maximum radius for lines hitting the unit square [0, 1]2 as a function of θ.

We can invert this by manipulatingξ1 = P (θ) into a formθ = g(ξ1). This yields:

θ =

arcsin(4ξ1 − 1) if ξ1 < 14(

arcsin((4ξ1 − 2)2

))/2 if ξ1 ∈ [14 , 3

4 ]arccos(3− 4ξ1) if ξ1 > 3

4

Once we haveθ, thenr is simply:

r = ξ2rmax(θ).

As discussed earlier, there are many parameterizations of the line, and each has anassociated “fair” measure. We can generate random lines in any of these spaces aswell. For example, in slope-intercept space the region that hits the square is shownin Figure 1.10. By similar reasoning to the normal space, the density function forslope is:

p(m) =1 + |m|

4with respect to the differential measure

dµ =dm

(1 + m2)32.

This gives rise to the cumulative distribution function:

P (m) =

12 − m−1

2√

1+m2 if m < 0

1 + m−12√

1+m2 if m ≥ 0

These can be inverted by solving two quadratic equations. Given anm generatedusingξ1, we then have

b =

−m + 2(m + 1)ξ2 if ξ < 1

2

(1−m)(2ξ2 − 1) otherwise

This is not a better way than using normal coordinates; it is just an alternativeway.

Page 21: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page2

i

i

i

i

i

i

i

i

1.4. CHOOSING RANDOM POINTS 21

m

b

Figure 1.10: The region of (m, b) space that contains lines that intersect the unit square

[0, 1]2.

Frequently-Asked Questions

This chapter discussed probability but not statistics. What is thedistinction?

Probability is the study of how likely an event is. Statistics infers characteristicsof large but finite populations of random variables. In that sense, statistics couldbe viewed as a specific type of applied probability.

Is Metropolis sampling the same as the Metropolis Light Trans-port Algorithm?

No. TheMetropolis Light Transport(Veach and Guibas, SIGGRAPH, 1996) al-gorithm uses Metropolis sampling as part of its procedure, but it is specifically forrendering, and has other steps as well.

Notes

Monte Carlo methods for graphics are discussed inMonte Carlo Rendering(Jensen,SIGGRAPH Course Notes, 2001). The classic reference for geometric probabil-ity is Geometric Probability(Solomon, SIAM Press, 1978). A method for pickingrandom edges in a square is given inRandom-edge Discrepancy of SupersamplingPatterns(Dobkin and Mitchell, Graphics Interface Conference, 1993). Some con-vergence properties of stratified sampling in graphics is given inConsequences of

Page 22: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page2

i

i

i

i

i

i

i

i

22 CHAPTER 1. MEASURE AND SAMPLING

Stratified Sampling in Graphics(Mitchell, SIGGRAPH, 1996). More informationon Quasi-Monte Carlo methods for graphics can be found inEfficient Multidimen-sional Sampling(Keller and Kollig, Computer Graphics Forum, 2002).

Exercises

1. What is the average value of the functionxyz in the unit cube(x, y, z) ∈[0, 1]3?

2. What is the average value ofr on the unit-radius disk:(r, φ) ∈ [0, 1] ×[0, 2π)?

3. Show that the uniform mapping of canonical random points(ξ1, ξ2) to athe barycentric coordinates of any triangle is:β = 1 − √

1− ξ1, andγ =(1− u)ξ2.

4. What is the average length of a line inside the unit square? Verify youranswer by generating ten million random lines in the unit square and aver-aging their lengths.

5. What is the average length of a line inside the unit cube? Verify your answerby generating ten million random lines in the unit cube and averaging theirlengths.

6. Show from the definition of variance thatV (x) = E(x2)− [E(x)]2 .

Page 23: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page2

i

i

i

i

i

i

i

i

Chapter 2

Global Illumination

Many surfaces in the real world receive most or all of their incident light fromother reflective surfaces. This is often calledindirect lighting or mutual illumi-nation. For example, the ceilings of most rooms receive little or no illuminationdirectly from luminaires (light emitting objects). The direct and indirect com-ponents of illumination are shown is Figure 2.1. Although accounting for theinterreflection of light between surfaces is straightforward, it is potentially costlybecause all surfaces may any given surface, resulting in as many asO(N2) in-teractions forN surfaces. Because the entire global database of objects mayilluminate any given objects, accounting for indirect illumination is often calledtheglobal illuminationproblem.

There is a rich and complex literature on solving the global illumination prob-lem as is discussed in the chapter notes. In this chapter we discuss two algorithmsas examples: particle tracing and path tracing. The first is useful for walkthroughapplications such as maze games, and as a component of batch rendering. Thesecond is useful for realistic batch rendering.

Figure 2.1: One the left and middle the indirect and direct lighting are seperated out. On

the right the sum of both components is shown. Global illumination algorithms are used to

account for both the direct and the indirect lighting.

23

Page 24: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page2

i

i

i

i

i

i

i

i

24 CHAPTER 2. GLOBAL ILLUMINATION

2.1 Particle Tracing for Lambertian Scenes

Recall from Section?? the transport equation:

Ls(ko) =∫

all ki

ρ(ki,ko)Lf (ki) cos θidσi.

The geometry for this equation is shown Figure 2.2. When the point being illu-minated is Lambertian this reduces to:

Ls =R

π

∫all ki

Lf (ki) cos θidσi,

whereR is the diffuse reflectance. One way to approximate the solution to thisEquation is to use finite element methods. First we would break the scene intoN surfaces each with unkown surface radianceLi, reflectanceRi, and emittedradianceEi. This results in the set of simultaneous linear equations:

Li = Ei +Ri

π

N∑j=1

kijLj

wherekij is a constant related to the integral in the full case. We could then solvethis set of linear equations and then we would haveN constant colored polygonswe could render. This finite element approach is often calledradiosity.

An alternative to radiosity is to use a statistical simulation approach by ran-domly following light “particles” from the luminaire though the environment.This is a form ofparticle tracing. There are many algorithms that use someform of particle tracing. We will discuss a form of particle tracing that depositslight in the textures on triangles. First we review some basic radiometric relations.For Lambertian surfaces, the radianceL of a Lambertian surface with areaA isdirectly proportional to the incident power per unit area:

L =ΦπA

, (2.1)

WhereΦ is the outgoing power from the surface. Note that in this discussion, allradiometric quantities are either spectral or RGB depending on the implementa-tion. If the surface has emitted powerΦe, incident powerΦi, and reflectanceR,then this becomes:

L =Φe + RΦi

πA,

If we are given a model withΦe andR specified for each triangle, then we canproceed luminaire by luminaire firing power in the form of particles from eachluminaire. We associate a texture map with each triangle to store accumulatedradiance, with all texels initialized to:

L =Φe

πA,

Page 25: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page2

i

i

i

i

i

i

i

i

2.1. PARTICLE TRACING FOR LAMBERTIAN SCENES 25

θi

n dσ

ko

−ki

Figure 2.2: The geometry for the transport equation in its directional form.

R=(0.75, 0.75, 0.75)

R=(0.5,0.5,0.5)

R=(0.2, 0.1, 0.1)

φ=(10, 10, 20)

φ=(6, 3, 6)

φ=(1

5, 1

5, 3

0)

Figure 2.3: The path of a particle that survives with probility 0.5 and is absorbed at the last

intersection. The RGB power is shown for each path segment.

If a given triangle has areaA andnt texels, and is hit by a particle carrying powerφ, then the radiance of that texel is incremented by:

∆L =ntφ

πA,

Once a particle hits a surface, we increment the radiance of the texel it hits, prob-abilistically decide whether to reflect the particle, and if we reflect it we choose adirection and adjust its power.

Note that we want the particle to be absorbed at some point. For each surfacewe can assign a reflection probabilityp to each surface interaction. The powerφ′ for reflected particles should be adjusted to account for the lost power of theabsorbed particles:

φ′ =Rφ

p

Page 26: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page2

i

i

i

i

i

i

i

i

26 CHAPTER 2. GLOBAL ILLUMINATION

Note thatp can be set to any positive constant less than one, and that this constantcan be different for each interaction. For the remainder of this discussion we setp = 0.5. The path of a single particle in such a system is shown in Figure 2.3.

A key part of this algorithm is that we scatter the light with an appropriatedistribution for Lambertian surfaces. As discussed in Section 1.4.1, we can finda vector with a cosine (Lambertian) distribution by transforming two canonicalrandom numbers(ξ1, ξ2) as follows:

a = (cos (2πξ1)√

ξ2, sin (2πξ1)√

ξ2,√

1− ξ2) (2.2)

Note that this assumes the normal vector is parallel to thez axis. For a trianglewe must establish an orthonormal basis withw parallel to the normal vector. Wecan accomplish this as follows:

w =n‖n‖

u =p1 − p0

‖p1 − p0‖

v = w × u

wherepi are the vertices of the triangle. Then by definition our vector in theappropriate coordinates is:

a = (cos (2πξ1)√

ξ2u + sin (2πξ1)√

ξ2v +√

1− ξ2)w (2.3)

In pseudocode our algorithm forp = 0.5 and one luminaire is:

for (Each ofn particles)doRGBphi = Φ/ncompute uniform random pointa on luminairecompute random directionb with cosine densitydone = falsewhile not donedo

if (ray a + tb hits at some pointc) thenaddntRφ/(πA) to appropriate texelif (ξ1 > 0.5) then

φ = 2Rφchoose random ray from pointc

elsedone=true

Hereξi are cononical random numbers. Once this code has run, the texture mapsstore the radiance of each triangle and can be rendered directly for any viewpointwith no additional computation.

Page 27: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page2

i

i

i

i

i

i

i

i

2.2. PATH TRACING 27

eye

Figure 2.4: In path tracing a ray is followed through a pixel from the eye and scattered

through the scene until it hits a luminaire.

2.2 Path Tracing

While particle tracing is well-suited to precomputation of the radiances of diffusescenes, it is problematic for creating images of scenes with general BRDFs orscenes that contain many objects. The most straightfoward way to create imagesof such scenes is to usepath tracing. This is a probabilistic method that sends raysfrom the eye and traces them back to the light. Often path tracing is used only tocompute the indirect lighting. Here we will present it in a way where it capturesall lighting, which can be inefficient. This is sometimes calledbrute forcepathtracing. In the next chapter more efficient techniques for direct lighting can beadded.

In path tracing we start with the full transport equation:

Ls(ko) = Le(ko) +∫

all ki

ρ(ki,ko)Lf (ki) cos θidσi.

We use Monte Carlo integration to approximate the solution this equation foreach viewing ray. Recall from Section 1.3 that we can use random samples toapprxoimate an integral:

∫x∈S

g(x)dµ ≈ 1N

N∑i=1

g(xi)p(xi)

,

where thexi are random points with probability density functionp. If we apply

Page 28: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page2

i

i

i

i

i

i

i

i

28 CHAPTER 2. GLOBAL ILLUMINATION

this directly to the transport equationn withN = 1 we get:

Ls(ko) ≈ Le(ko) +ρ(ki,ko)Lf (ki) cos θidσi

p(ki).

So if we have a way to select random directionski with a known densityp then wecan get an estimate. The catch is thatLf (ki) is itself an unknown. Fortunatelywe can apply recursion and use a statistical estimate forLf (ki) by sending aray in that direction to find the surface seen in that direction. We end when wehit a luminaire andLe is nonzero (Figure 2.4). This assumes lights have zeroreflectance or we would continue to recurse. In the case of a Lambertian BRDF(ρ = R/π) we can use a cosine density function:

p(ki) =cos θi

π

A direction with this density can be chosen according to Equation 2.3. This allowssome cancellation of cosine terms in our estimate:

Ls(ko) ≈ Le(ko) + RLf (ki).

In pseudocode such a path tracer for Lambertian surfaces would operate just likethe ray tracers described in Chapter??, but theraycolor fuction would be modi-fied:

RGB raycolor( raya + tb, int depth)if (ray hits at some pointc) then

RGBc = Le(−b)if (depth< maxdepth)then

compute random directiondreturn c + R raycolor(c + sd,depth+1)

elsereturn background color

This will result in a very noisy images unless either large luminaires or very largenumbers of samples are used. Note the the color of the luminaires must be wellabove one (sometimes thousands or tens of thousands) to make the surfaces havefinal colors near one, because only those rays that it a luminaire by chance willmake a contribution, and most rays will contribute only a color near zero. Togenerate the random directiond we use the same technique as we do in particletracing (see Equation 2.2).

In the general case we might want to use spectral colors or use a more generalBRDF. In practice we should have the material class contain member functionsto compute a random direction as well as compute thep associated with thatdirection. This way materials can be added transparently to an implementation.

Page 29: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page2

i

i

i

i

i

i

i

i

2.2. PATH TRACING 29

Frequently Asked Questions

My pixel values are no longer in some sensible zero-to-one range.What should I display?

You should use one of thetone reproductiontechniques described in the last chap-ter.

What global illumination techniques are used in practice?

For batch rendering of complex scenes path tracing with one level of reflection isoften used. This is often augmented with a particle tracing preprocess as describedin Jensen’s book in the chapter notes. For walkthrough games some form ofworld-space preprocess is often used, such as the particle tracing described in thischapter. For scenes with very complicated specular transport, a complex methoddescribed by Veach (Metropolis Light Transport, SIGGRAPH 96) may be the bestchoice.

How does the ambient component relate to global illumination?

For diffuse scenes, the radiance of a surface is proportional to the product of theirradiance at the surface and the reflectance of the surface. The ambient com-ponent is just an approximation to the irradiance scaled by the inverse ofπ. Soalthough it is a crude approximation, there can be some methodology to guessingit, and it is probably more accurate than doing nothing, i.e., using zero for the am-bient term. Because the indirect irradiance can vary widely within a scene, usinga different constant for each surface can be used for better results than a globalambient term.

Why do most algorithms compute direct lighting using tradi-tional ray tracing?

Although global illumination algorithms automatically compute direct lighting,and it is in fact slightly more complicated to make them compute only indi-rect lighting, it is usually faster to compute direct lighting seperately. There arethree reasons for this. First, indirect lighting tends to be smooth compared todirect lighting (See Figure 2.1) so coarser representations can be used, e.g., low-resolution texture maps for particle tracing. The second reason is that light sourcestend to be small and it is rare to hit them by chance in a “from the eye” methodsuch as path tracing, while direct shadow rays are efficient. The third reason isthat direct lighting allows stratified sampling so it converges rapidly compared tounstratified sampling. The issue of stratification is why shadow rays are used inMetropolis Light Transport despite the stability of its default technique for dealingwith direct lighting as just one type of path to handle.

Page 30: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page3

i

i

i

i

i

i

i

i

30 CHAPTER 2. GLOBAL ILLUMINATION

Figure A comparison between a rendering and a photo. Figure courtesy Sumant Pat-tanaik and the Cornell Program of Computer Graphics.:

How artificial is it to assume ideal diffuse and specular behav-ior?

For environments that have only matte and mirrored surfaces the Lambertian/specularassumption works well. A comparison between a rendering using that assumptionand a photograph is shown in Figure 2.F.

Notes

Goral, Kajiya, Malley, Arvo, Jensen etc.Sillion startistical path tracing.

Exercises

• For a closed environment where every surface is a diffuse reflector andemittor with reflectanceR and emitted radianceE, what is the total radi-ance at each point?Hint: for R = 0.5 andE = 0.25 the answer is0.5.This is an excellent debugging case.

• Using the definitions from Chapter??, verify Equation 2.1.

• If we want to take a typically-sized room with textures at centimater-squareresolution, approximately how many particles should we send to get anaverage of about 1000 hits per texel?

Page 31: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page3

i

i

i

i

i

i

i

i

Chapter 3

Accurate Direct Lighting

This chapter presents a more physically-based method of direct lighting thanChapter??. This will be useful in making the global illumination algorithmsfrom the last chapter more efficient. The key idea is to send shadow rays to theluminaires as was described in Chapter??, but to do so with careful bookkeepingbased on the transport equation from the last chapter. The global illumination al-gorithms can be adjusted to make sure they compute the direct component exactlyonce. For example, in particle tracing particles coming directly from the luminairewould not be logged, so the particles would only encode indirect lighting. Thismakes nice looking shadows much more efficiently than computing direct lightingin the context of global illumination.

3.1 Mathematical framework

To calculate the direct light from oneluminaire(light emitting object) onto a non-emitting surface, we solve a form of the transport equation from Section??:

Ls(x,ko) =∫

all x′

ρ(ki,ko)Le(x′,−ki)v(x,x′) cos θi cos θ′

‖x− x′‖2 (3.1)

Recall thatLe is the emitted radiance of the source,v is a visibility function thatis one if x “sees”x′ and zero otherwise, and the other variables are defined inFigure 3.1.

x

x’

n

θi

θ’

ki

−ki

n’

ko

Figure 3.1: The directlighting terms for Equa-tion ??.

If we are to sample Equation?? using Monte Carlo integration, we need topick a random pointx′ on the surface of the luminaire with density functionp (sox′ ∼ p). Just plugging into the Equation 1.5 with one sample yields:

Ls(x,ko) ≈ ρ(ki,ko)Le(x′,−ki)v(x,x′) cos θi cos θ′

p(x′)‖x− x′‖2 (3.2)

31

Page 32: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page3

i

i

i

i

i

i

i

i

32 CHAPTER 3. ACCURATE DIRECT LIGHTING

If we pick a uniform random point on the luminaire, thenp = 1/A, whereA isthe area of the luminaire. This gives:

Ls(x,ko) ≈ ρ(ki,ko)Le(x′,−ki)v(x,x′)A cos θi cos θ′

‖x− x′‖2 (3.3)

We can use Equation 3.3 to sample planar (e.g., rectangular) luminaires in astraightforward fashion. We simply pick a random point on each luminaire. Thecode for one luminaire would be:

color directLight(x, ko, n)pick random pointx′ with normal vectorn′ on lightd = x′ − xki = d/‖d‖if (ray x + td has no hits fort < 1− ε) then

returnρ(ki,ko)Le(x′,−ki)(n · d)(−n′ · d)/‖d‖4else

return 0

The above code needs some extra tests such as clamping the cosines to zero ifthey are negative. Note that the term‖d‖4 comes from the distance squared termand the two cosines, e.g.,n · d = ‖d‖ cos θ becaused is not necessarily a unitvector.

Several examples of soft shadows are shown in Figure 3.2.

3.1.1 Sampling a spherical luminaire

Although a sphere with centerc and radiusR can be sampled using Equation 3.3,this will yield a very noisy image because many samples will be on the backof the sphere, and thecos θ′ term varies so much. Instead we can use a morecomplexp(x′) to reduce noise. The first nonuniform density we might try isp(x′) ∝ cos θ′. This turns out to be just as complicated as sampling withp(x′) ∝cos θ′/‖x′−x‖2, so we instead discuss that here. We observe that sampling on theluminaire this way is the same as using a density constant functionq(ki) = constdefined in the space of directions subtended by the luminaire as seen fromx.We now use a coordinate system defined withx at the origin, and a right-handedorthonormal basis withw = (c − x)/‖c − x‖, andv = (w × n)/‖(w × n)‖(see Figure 3.3). We also define(α, φ) to be the azimuthal and polar angles withrespect to theuvwcoordinate system.

The maximumα that includes the spherical luminaire is given by:

αmax = arcsin(

R

‖x− c‖)

= arccos

√1−

(R

‖x− c‖)2

.

Thus a uniform density (with respect to solid angle) within the cone of directionssubtended by the sphere is just the reciprocal of the solid angle2π(1− cos αmax)

Page 33: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page3

i

i

i

i

i

i

i

i

3.1. MATHEMATICAL FRAMEWORK 33

Figure 3.2: Various soft shadows on a backlit sphere with a square and an area light source.

Top: one sample. Bottom: 100 samples. Note that the shape of the light source is less

important than its size in determining shadow appearance.

x

x'

n

θi

θ'

ki

n'

u

R

luminaire

c

α αmaxw

v

Figure 3.3: Geometry for direct lighting at point x from a spherical luminaire.

Page 34: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page3

i

i

i

i

i

i

i

i

34 CHAPTER 3. ACCURATE DIRECT LIGHTING

subtended by the sphere:

q(ki) =1

(1−

√1−

(R

‖x−c‖)2) .

And we get

[cos α

φ

]=

1− ξ1 + ξ1

√1−

(R

‖x−c‖)2

2πξ2

.

This gives us the direction toki. To find the actual point, we need to find the firstpoint on the sphere in that direction. The ray in that direction is just (x + tki),whereki is given by:

ki =

ux vx wx

uy vy wy

uz vz wz

cos φ sin α

sin φ sinαcos α

.

We must also calculatep(x′), the probability density function with respect to thearea measure (recall that the density functionq is defined in solid angle space).Since we know thatq is a valid probability density function using theω measure,and we know thatdΩ = dA(x′) cos θ′/‖x′ − x‖2, we can relate any probabilitydensity functionq(ki) with its associated probability density functionp(x′):

q(ki) =p(x′) cos θ′

‖x′ − x‖2 . (3.4)

So we can solve forp(x′):

p(x′) =cos θ′

2π‖x′ − x‖2(

1−√

1−(

R‖x−c‖

)2) .

A good debugging case for this is shown in Figure 3.3.

3.1.2 Non-diffuse Luminaries

There is no reason the luminance of the luminaire cannot vary with both direc-tion and position. It can vary with position if the luminaire is a television. Itcan vary with direction for car headlights and other directional sources. Noth-ing need change from the previous sections, except thatLe(x′) must change toLe(x′,−ki). The simplest way to vary the intensity with direction is to use a

Page 35: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page3

i

i

i

i

i

i

i

i

3.2. DIRECT LIGHTING FROM MANY LUMINAIRES 35

Figure 3.3: A sphere with Le = 1 touching a sphere of reflectance 1. Where they touch the

reflective sphere should have L(x′) = 1. Left: one sample. Middle: 100 samples. Right:

100 samples, close-up.

phong-like pattern with respect to the normal vectorn′. To keep the total lightoutput independent of exponent, you can use the form:

Le(x′,−ki) =(n + 1)E(x′)

2πcos(n−1)θ′,

whereE(x′) is theradiant exitance(power per unit area) at pointx′, andn is thephong-exponent. You get a diffuse light forn = 1. If the light is non-uniformacross its area, e.g., as a television set is, thenE will not be a constant.

3.2 Direct Lighting from Many Luminaires

Traditionally, whenNL luminaires are in a scene, the direct lighting integral isbroken intoNL separate integrals. This implies at leastNL samples must be takento approximate the direct lighting, or some bias must be introduced. This is whatyou should probably do when you first implement your program. However, youcan later leave the direct lighting integral intact and design a probability densityfunction over allNL luminaires.

As an example, suppose we have two luminaires,l1 and l2, and we devisetwo probability functionsp1(x′) andp2(x′), wherepi(x′) = 0 for x′ not on liandpi(x′) is found by a method such as one of those described previously forgeneratingx′ on li. These functions can be combined into a single density overboth lights by applying a weighted average:

p(x′) = αp1(x′) + (1− α)p2(x′),

whereα ∈ (0, 1). We can see thatp is a probability density function because itsintegral over the two luminaires is one, and it is strictly positive at all points onthe luminaires. Densities that are “mixed” from other densities are often calledmixture densitiesand the coefficientsα and(1−α) are called themixing weights.

To estimateL = (L1+L2), whereL is the direct lighting andLi is the lightingfrom luminaireli, we first choose a random canonical pair(ξ1, ξ2), and use it todecide which luminaire will be sampled. If0 ≤ ξ1 < α, we estimateL1 withe1 using the methods described previously to choosex′ and to evaluatep1(x′),

Page 36: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page3

i

i

i

i

i

i

i

i

36 CHAPTER 3. ACCURATE DIRECT LIGHTING

and we estimateL with e1/α. If ξ1 ≥ α then we estimateL with e2/(1 − α).In either case, once we decide which source to sample, we cannot use(ξ1, ξ2)directly because we have used some knowledge ofξ1. So if we choosel1 (soξ1 < α), then we choose a point onl1 using the random pair(ξ1/α, ξ2). If wesamplel2 (so ξ1 ≥ α), then we use the pair((ξ1 − α)/(1 − α), ξ2). This waya collection of stratified samples will remain stratified in some sense. This basicidea is illustrated in Figure 3.4 for nine samples andα = 0.7.

This basic idea used to estimateL = (L1 + L2) can be extended toNL

luminaires by mixingNL densities

p(x′) = α1p1(x′) + α2p2(x′) + · · ·+ αNLpNL

(x′), (3.5)

where theαi’s sum to one, and where eachαi is positive if li contributes to thedirect lighting. The value ofαi is the probability of selecting a point on theli,andpi is then used to determine which point onli is chosen. Ifli is chosen, thewe estimateL with ei/αi. Given a pair(ξ1, ξ2), we chooseli by enforcing theconditions

i−1∑j=1

αj < ξ1 <

i∑j=1

αj .

And to sample the light we can use the pair(ξ′1, ξ2) where

ξ′1 =ξ1 −

∑i−1j=1 αj

αi.

It cannot be over-stressed that it is important to “reuse” the random samples inthis way to keep the variance low, in the same way we use stratified sampling(jittering) instead of random sampling in the space of the pixel To choose the pointon the luminaireli given(ξ′1, ξ2), we can use the same types ofpi for luminairesas used in the last section (Figure 3.5). The question remaining is what to use forαi.

3.2.1 Constantαi

The simplest way to choose values forαi is, where all weights are made equal:αi = 1/NL for all i. This would definitely make a valid estimator because theαi

sum to one and none of them is zero. This is a good debugging case. Unfortu-nately, in many scenes this estimate would produce a high variance (when theLi

are very different as occurs in most night “walkthroughs”).

3.2.2 Linearαi

Suppose we had perfectpi defined for all the luminaires. A zero variance solutionwould then result if we could setαi ∝ Li, whereLi is the contribution fromthe ith luminaire. If we can makeαi approximately proportional toLi, then we

Page 37: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page3

i

i

i

i

i

i

i

i

3.2. DIRECT LIGHTING FROM MANY LUMINAIRES 37

area = 0.7

eye

light 1 light 2

Figure 3.4: Each viewing ray generates exactly one shadow ray. A set of stratified samples

on the unit square is mapped to luminaire locations, approximately 70% of which will go to

the “more important” light because α = 0.7.

Page 38: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page3

i

i

i

i

i

i

i

i

38 CHAPTER 3. ACCURATE DIRECT LIGHTING

0 0.2 0.60.3 1.0

choose light 020% chance

light 110%

light 230%

light 340%

ξ=0.4

ξ'=0.333...

light 2 is chosen

0 1.0

0.4 is one third of the way from 0.3 to 0.6

first sample coordinate from sampling light 2

Figure 3.5: Diagram of mapping ξ1 to choose li and the resulting remapping to new canon-

ical sample ξ′1.

should have a fairly good estimator. This is called thelinear methodof settingαi

because the time used to choose one sample is linearly proportional toNL, thenumber of luminaires.

To obtain suchαi we get an estimated contributionei atx by approximatingthe rendering equation forli with the geometry term set to one. Theseeis (fromall luminaires) can be directly converted toαi by scaling them so their sum is one:

αi =ei

e1 + e2 + · · ·+ eNL

. (3.6)

This method of choosingαi will be valid because all potentially visible luminaireswill end up with positiveαi. We should expect the highest variance in areas whereshadowing occurs, because this is where setting the geometry term to one causesαi to be a poor estimate ofαi.

Implementing the linearαi method has several subtleties. If the entire lumi-naire is below the tangent plane atx, then the estimate forei should be zero. Aneasy mistake to make is to setei to zero if the center of the luminaire is below thehorizon. This will makeαi take the one value that is not allowed: an incorrectzero. Such a bug will become obvious in pictures of spheres illuminated by lu-minaires that subtend large solid angles, but for many scenes such errors are notnoticeable. To overcome this problem, we make sure that for a polygonal lumi-naires all of its vertices are below the horizon before it is given a zero probabilityof being sampled. For spherical luminaires, we check that the center of the lumi-naire is a distance greater than the sphere radius under the horizon plane before itis given a zero probability of being sampled.

Page 39: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page3

i

i

i

i

i

i

i

i

3.2. DIRECT LIGHTING FROM MANY LUMINAIRES 39

Frequently Asked Questions

How many shadow rays are needed per pixel?

Typically between 16 and 400. Using narrow penumbra, a large ambient term(or a large indirect component), and a masking texture can reduce how many areneeded.

How do I sample something like a filament with a metal reflectorwhere much of the light is reflected from the filament?

Typically the whole light is replaced by a simple source that approximates itsaggregate behavior. For viewing rays the complicated source is used. SO a carheadlight would look complex to the viewer, but the lighting code might see sim-ple disk-shaped lights.

Isn’t something like the sky a luminaire?

Yes, and you can treat it as one. However, such large light sources may not behelped by direct lighting; the brute-force techniques from the last chapter arelikely to work better.

Notes

Using Monte Carlo integration for computing direct lighting was introduced inDistributed Ray Tracing(Cook, SIGGRAPH, 1984).

Exercises

1. Develop a method to take random samples with uniform density from adisk.

2. Develop a method to take random samples with uniform density from atriangle.

3. Develop a method to take uniform random samples on a “sky dome” (theinside of a hemisphere).

Page 40: Fundamentals of Computer Graphics - univ-reims.frmathinfo.univ-reims.fr/...Fundamentals_of_computer_graphics_Shirle… · Fundamentals of Computer Graphics Peter Shirley School of

“cg”2002/8page4

i

i

i

i

i

i

i

i

Index

average, 5

barycentric coordinates, 22

direct lighting, 31

expected value, 9, 11

function inversion, 14

global illumination, 23

illuminationglobal, 23

importance sampling, 12independent random variable, 9indirect lighting, 23integral geometry, 5integration, 4

Monte Carlo, 11, 27quasi-Monte Carlo, 13

inversionfunction, 14

Law of Large Numbers, 11lighting

direct, 31indirect, 23

linenormal coordinates, 7

luminaire, 23non-diffuse, 35

measure2D lines, 63D lines, 7

zero measure sets, 4Metropolis sampling, 18mixture density, 35Monte Carlo integration, 11, 27

importance sampling, 12stratified sampling, 12

mutual illumination, 23

normal coordinates, 7

particle tracing, 24path tracing, 27probability, 8

expected value, 9probability density function, 8

quasi-Monte Carlo integration, 13

radiosity, 24random variable, 9random variables

independent identically distributed,11

rejection method, 16

stratified sampling, 12

transport equation, 27, 31triangle

random point selection, 22

variance, 10

zero measure sets, 4

40