TNCG15: Advanced Global Illumination and Rendering, Lecture 6 (Monte-Carlo raytracing) Mark Eric Dieckmann, MIT group, ITN Campus Norrköping SE-60174 Norrköping September 26, 2016 Outline of the lecture ◮ We have previously discussed how raytracing works and we have addressed the special case of Whitted raytracing. ◮ Whitted raytracing works well for specular or transparent objects, but it can not be used for general BRDFs. ◮ We have seen that Monte-Carlo integration is a fast method to integrate multi-dimensional integrals. Hence it can be used for computing the integral in the rendering equation. ◮ Now we show how we do that in practice. Basic idea Our Whitted ray-tracer computed the reflected and refracted rays using physical laws: we obtain photorealistic renderings of mirrors and glas. This is not possible for general BRDF’s: we use instead the Monte-Carlo scheme to integrate the rendering equation. The starting point of Whitted ray-tracing is the same as that of MC ray tracing. The first importance ray is launched at the eye position e and goes through a point on the pixel. We compute the intersection of the ray with an object or with a wall. Basic idea The point x is the first point of intersection between the camera ray and a surface point. The camera is located in the direction ωout . We can use this direction to set up the local coordinate system around x: ~ N ~ ~v1 = ωout − (ωout · N) Our aim is to compute the radiance L(x → ωout ) using L(x ← ωin ). ωin = (ϕi , θi ) has random values for the azimuth ϕi (relative to ~v1 ) and inclination θi . N ω out ω in θi ρi V1 X The rendering equation The rendering equation is L(x → ωout ) = Le (x → ωout ) + Z fr (x, ωin , ωout ) L(x ← ωin ) cos θin dωin We can compute the emitted radiance L(x → ωout ) once we know the self-emission Le (x → ωout ) of the point x (light source term) and once we have solved the integral. Monte-Carlo integration approximates a two-dimensional integral RR PN I = f (x, y ) dx dy by an estimator hI i = N −1 i =1 f (xi , yi )/p(xi , yi ). We need to choose a suitable PDF and determine how we transform the integral into a form that matches the equation for MC integration. Random numbers and probability distributions We must choose a suitable probability distribution p(x, y ) for the random numbers. Two questions spring to mind: (1) Does it matter, which PDF we take for the evaluation of PN hI i = N −1 i =1 f (xi , yi )/p(xi , yi )? The estimator hI i for the integral I is linked to the expected value of a function of random numbers and the variance σ 2 estimates its accuracy. (2) How do we compute random numbers with a distribution p(xi , yi )? What kind of random numbers can we get from a computer? The ideal probability distribution The expected value of the estimator hI i of an integral I in 1D is E [hI i] = E [N −1 N X f (xi )/p(xi )] = I i =1 The variance determines how close the hI i are to I . 2 2 σ = E [(hI i − I ) ] = E [ N −1 N X f (xi ) p(xi ) i =1 ! −I !2 The variance becomes in the limit of large N 1 σ = N 2 Z f (x) −I p(x) 2 p(x)dx The variance σ 2 = 0 if p(x) = f (x)/I . Can we use this PDF? ] The ideal probability distribution The variance σ 2 = 0 for the PDF p(x) = f (x)/I . R∞ We would need to fulfill 1 = −∞ p(x) dx and we can not integrate f (x). f(x) 0 0 a p(x) Choose an integrable PDF p(x) that is similar to f (x). c d b x Draw random positions xi from p(x). We get more xi in the domain [c, d] than in [a, c] or [d, b]. a c d b x We thus place most samples xi into those x-intervals where f (x) is largest. These intervals contribute most to I . Getting random numbers with the CDF How can we compute random numbers xi with a PDF p(x) using uniformly distributed random numbers (p(x) = constant)? p(x) Given is a continuous R xPDF p(x). Its cumulative distribution function (CDF) is cfd(x) = −∞ p(x̃) d x̃. 2 1.5 1 0.5 0 0 0.2 0.4 0.6 0.8 1 cfd(x) x 1 0.8 0.6 0.4 0.2 0 0 0.2 0.4 The value of cfd(x) grows fastest where p(x) is large. 0.6 x The values of cfd(x) range from 0 R∞ to 1 due to −∞ p(x) dx = 1 and p(x) ≥ 0. 0.8 1 cfd(x) increases strictly with x in intervals with p(x) > 0. If a function increases strictly in an interval, it can be inverted. Sometimes we can find an analytic expression for the inverse. Getting random numbers with the CDF The cumulative distribution function is cfd(x) = cfd(x) −∞ p(x̃ ) d x̃. The red horizontal lines have a constant spacing along the vertical y-axis. 1 0.8 0.6 0.4 0.2 0 0 Rx 0.2 0.4 0.6 0.8 1 The vertical blue lines go through the intersection of the red lines with cfd(x). x If we place lines with equal spacing along y = cfd(x), the spacing of the vertical lines along x is not equal. The blue lines are concentrated in intervals with a large slope of the CDF. The slope of the CDF is proportional to p(x). Getting random numbers A fast way to get random numbers with a probability distribution function p(x) is: Step 1: Integrate the R x PDF, which gives the cumulative distribution function cfd(x) = −∞ p(x) dx. Step 2: Invert the CDF: cfd(x) maps values on the x-axis onto y = cfd(x). Its inverse maps the y-axis onto x = cfd −1 (y ). Step 3: Calculate random numbers yi using a random number generator with a uniform PDF p(y ) = 1 on 0 ≤ y ≤ 1. Step 4: Map the uniform random numbers onto random numbers with the PDF p(x) using xi = cfd −1 (yi ). If you use in your project a PDF p(x) that gives an invertible CDF then you can easily compute random numbers that are distributed like p(x) using uniform random numbers. Rejection sampling If the elegant way does not work, use the brute-force one. Take the 2D array of size: [ a , b ] × [ 0 , M ], where p(x) ≤ M. Calculate pairs x, y in the 2D field. If for a pair xi , yi we get yi > p(xi ), the sample is rejected. The xi are then distributed according to p(x). The probability distribution for the renderer We want to compute the integral of the rendering equation with an MC scheme. Z fr (x, ωin , ωout ) L(x ← ωin ) cos θin dωin The distribution L(x ← ωin ) is not initially known and we can not use it as the PDF. The BRDF fr (x, ωin , ωout ) rarely gives an invertible CDF (works for Phong’s reflection models and some others). You should use in your project the remaining term p(θ, ϕ) = π −1 cos θ for the Lambertian and Oren-Nayar reflectors. Radiance flow (almost) along the surface normal contributes most to the irradiance at the point x. The PDF above sends most rays that way. The probability distribution for the renderer R The CDF of p(θ, ϕ) = π −1 cos θ is F = π −1 cos θ dω, where ω is the solid angle and dω = sin θ dθ dϕ. Rϕ ′ R θ ′ ′ ′ We integrate it as F (θ , ϕ) = π −1 0 dϕ 0 cos θ sin θ dθ . ′ = (ϕ/π) (− cos2 θ /2)|θ0 = (ϕ/2π) (1 − cos2 θ). We can separate the function into two terms Fϕ = (ϕ/2π) and Fθ = 1 − cos2 θ, which we invert separately. We draw a pair of random numbers u1 , u2 ǫ [0, 1). Using them as the argument of the inverse CDF gives us the random numbers that follow p(θ, ϕ): √ The azimuth ϕi = 2πu1 and the elevation θi = cos−1 ( u2 ). Computing the reflected importance The rendering integral with no source term for radiance is Z L(x → ωout ) = fr (x, ωin , ωout ) L(x ← ωin ) cos θin dωin . We draw random numbers with the PDF p(ϕ, θ) = cos θin /π. Z L(x → ωout ) = π fr (x, ωin , ωout ) L(x ← ωin ) p(ϕ, θ) dωin ≈ N π X fr (x, ωin , ωout ) L(x ← ωin ) N i =1 We use only one random ray with direction ωin = (ϕi , ωi ): L(x → ωout ) = πfr (x, ωin , ωout )L(x ← ωin ) Example of Lambertian reflector: fr (x, ωin , ωout ) = ρ/π. Computing the reflected importance We have approximated the rendering integral as L(x → ωout ) = πfr (x, ωin , ωout )L(x ← ωin ). Since L(x → ωout )W (x ← ωout ) = L(x ← ωin )W (x → ωin ): W (x → ωin ) = πfr (x, ωin , ωout ). W (x ← ωout ) We have defined ωin and ωout with respect to the radiance flow. Importance flows the other way: W2 = πfr (x, ωin , ωout )W1 We give the importance W2 to ray r2 and we use this expression to compute the importances of all consecutive rays. Ray termination Our Monte-Carlo scheme allows us to track rays as they move through the scene. We need to specify a ray termination strategy for our renderer: We will not terminate rays when they interact with reflecting or transparent surfaces. We will terminate rays on area light sources and on Lambertian / Oren-Nayar reflectors (walls and objects). We terminate them with an unbiased scheme. Ray termination First idea: select an absorption probability α for a ray that hits the surface. The value of α can be connected to surface properties. Draw a random number 0 ≤ t ≤ 1. If t > 1 − α we terminate the ray. Let’s see what happens. We approximate I = R f (x) dx by hI i = N −1 PN i =1 f (xi )/p(xi ). The expected value of the random estimator is E (hI i) = I . Let us set NP = 4. We draw 4 random values x1 , x2 , x3 , x4 to compute 4 hI i = (1/4) i =1 f (xi )/p(xi ). We compute a large number M of estimators (each giving a random PM value hI ij ) and average them to get E (hI i) ≈ M −1 j=1 hI ij . Ray termination We cut off rays with a probability of α = 0.25. This value forPα removes on average one ray from every estimator hI i∗ = (1/4) 3i =1 f (xi )/p(xi ). The expected value obtained from the averaging of M estimators hI ij is now E (hI i∗ ) = (3/4)E (hI i) = 3I /4. The estimator hI i∗ underestimates the integral value and it is thus biased. We can compensate this bias by multiplying f (xi ) by 4/3. Ray termination R ′ We take the integral I = (4/3) f (x) dx, which gives the 4-sample estimator ′ hI i = (1/4) 4 X (4/3)f (xi ) i =1 p(xi ) We remove systematically one ray and we get ′′ hI i = (1/4) 3 X (4/3)f (xi ) i =1 p(xi ) ′ 34 3 E (hI i) = E (hI i) = E (hI i) 4 43 Russian roulette follows up on this idea in a systematic way. ′′ E (hI i) = Ray termination: Russian roulette Let us take the integral I = R1 0 f (x) dx. We rescale x and the function by a factor 0 < P < 1. The x-interval shrinks to [0, P]. The function value is multiplied by 1/P. The integral value is preserved: RP R1 0 f (x/P)/P dx = 0 f (x) dx. We approximate the rescaled integral by the estimator: I = Z 0 P N f (x/P) 1 X f (xi /P) dx ≈ P PN i =1 p(xi ) Russian roulette in the project The rendering integral for the radiance at the surface point x has been approximated by a Monte-Carlo scheme that uses one ray. L(x → ωout ) = πfr (x, ωin , ωout )L(x ← ωin ). We want to terminate the ray path - we launch a ray W (x → ωin ) with a probability P and we do not launch one with a probability (1 − P). To remove the bias we use the equation L(x → ωout ) = π fr (x, ωin , ωout )L(x ← ωin ). P We modify the equation for the random azimuth angle to ϕi = (2π/P) u where the random value 0 ≤ u ≤ 1. If ϕi > 2π we terminate the ray path, if 0 ≤ ϕi ≤ 2π we spawn a ray. The tree We develop a ray tree like we did for Whitted ray tracing. If the ray interacts with an intransparent and non-specular surface a new ray is spawned unless Russian roulette terminates it. If the ray hits a specular reflector (mirror) one new ray is spawned (no Russian roulette). If the ray hits a transparent surface one reflected and one refracted ray are spawned (no Russian roulette). If the ray hits an area light source it is always terminated. Eventually Russian roulette has terminated all rays and we have completed the ray tree. Introducing radiance into the tree We have developed the tree for the importance rays. We follow one ray path until the termination ray rN . The direction of the ray ri is ωi We use area light sources in MC raytracing. (We use triangles). Rays can hit an area light source. We still need direct light for the ray-surface intersections. Area light rN Shadow rays r N−2 O2 r N−1 O1 The ray rN ends here on the point xN+1 on an area light source and Le (xN+1 → −ωN ) > 0. We use Lambertian emitters in our project: Le (xN+1 → −ωN ) = L0 with L0 being the same for all −ωN and xN+1 . Reflected radiance Our rendering equation has been reduced for our case into the form L(x → ωout ) = LD (x → ωout ) + At xN : L(xN → −ωN−1 ) = LD (xN → −ωN−1 ) + π f (xN , ωN , −ωN−1 )L(xN ← ωN ) P r The value Pπ fr (xN , ωN , −ωN−1 ) corresponds to the ratio of the importances of rN and rN−1 . π fr (x, ωin , ωout )L(x ← ωin ) P x N+1 L0 rN Shadow rays r N−2 xN O2 r N−1 O1 The same procedure is true for xN−1 , xN−2 , .., x0 . All that remains to be done is to compute LD using shadow rays. Direct light We want to compute the direct light component (light from the light source that is reflected once): Z LD (xN → −ωN−1) = fr (xN , ωin , −ωN−1)L(xN ← ωin ) cos θin dωin S2 The integration can be approximated with an MC scheme. S1 Light rN We launch shadow rays and perform a visibility test. Use a number M ≫ 1 of shadow rays. 10-20 might be a good compromise. S3 xN r N−1 O2 Our light source is a Lambertian emitter and Le (q → ωout ) = L0 for all points q and light directions ωout of the light source. Direct light We want to approximate the integral Z LD (xN → −ωN−1) = fr (xN , ωin , −ωN−1)L(xN ← ωin ) cos θin dωin by a Monte-Carlo estimator that uses M shadow rays. The PDF p(ϕ, θ) of our light source is nontrivial in hemispherical coordinates. We thus use the integral over the triangle’s surface A. Z LD (xN → −ωN−1 ) = L0 fr (xN , ωin , −ωN−1) V (xN , q) G (xN , q) dA A V (xN , q) is the visibility function for the end point q of a shadow ray. G (xN , q) = cos α cos β/d 2 is the geometric term. The length of the shadow ray is d and α, β are the inclination angles relative to the normals at its start- and endpoint. Computing shadow rays Our lightsource is a triangle with the corner points v0 , v1 and v2 . The triangle surface can be computed by A = 0.5 · ||(v1 − v0 ) × (v2 − v0 )||. You can compute A by hand from the triangle list that defines your scene. We pick random points q on its surface with a uniform PDF p(q) = 1/A. Suggestion: Draw two uniform random numbers u, v . If u + v < 1 you construct a point qi on the triangle with barycentric coordinates qi = (1 − u − v )v0 + uv1 + v v2 . Otherwise you draw new u, v . You draw M valid points on your light source. The endpoint of each shadow ray is qi and the starting point xN of all M shadow rays is located on the surface where you launched it. Direct light We have obtained M shadow rays that go from xN to M points qi on the light source surface. The shadow ray direction is ωS,i and di is its length. We can approximate LD (xN → −ωN−1) = L0 Z A fr (xN , ωS,i , −ωN−1 ) V (xN , qi ) G (xN , qi ) dA, by the estimator < LD (xN → −ωN−1) >= M AL0 X fr (xN , ωS,i , −ωN−1) V (xN , qi ) G (xN , qi ) M i =1 We add the computed radiance value hLD i to the radiance that has been reflected by the point xN . Lecture summary ◮ We have discussed practical issues related to Monte-Carlo raytracing. ◮ We have discussed how we choose the probability distribution function (PDF) that sets the random azimuth and inclination angles for the rays. ◮ We have shown how the cumulative distribution function gives us suitable random numbers. ◮ We have discussed how we build up the ray tree and how we can compute the direct light contributions. ◮ We have completed our discussion of basic Monte-Carlo raytracing.
© Copyright 2026 Paperzz