I am following the "Ray Tracing - the Rest of your Life" book by Peter Shirley and I am facing some troubles in the implementation of Monte Carlo importance sampling, and Direct light sampling.
Monte Carlo theory is clear to me. During the implementation, the way rays are scattered does not actually change and remains random. What changes looks to be the contribution of each ray.
So my first question is:

Do I understand this correctly? Rays, after hitting a surface, are still scattered randomly, but the value of the color is adjusted based on the probability of that direction?

For those familiar with the book
When the author implements the Direct light sampling, he modifies the function color(), while we previously modified scatter() and scattering_pdf(). This way, scatter() and scattering_pdf() are sampling based on a specific pdf - proportional to cos(theta) - while color() introduces a new pdf related to the direction of the light. It is not clear to me how these two things coexist, and if I am supposed to disregard the previous changes to scatter() and scattering_pdf().


During the implementation, the way rays are scattered does not actually change and remains random.

Actually the way rays are scattered does change, specifically when you sample a light. In chapter 8 he makes a mixture pdf in order to sample either the light or the bsdf.

What changes looks to be the contribution of each ray.

This does change, but not specifically due to the sampling of the lights but rather due to importance sampling. But that was already introduced in chapter 4.

Basically you have the following: $$I = \int_{\Omega}f(x)\,dx = \int_{\Omega}\frac{f(x)}{p(x)}p(x)\,dx = E\left[\frac{f(X)}{p(X)}\right] = \frac{1}{N}\sum_{i=1}^{N}E\left[\frac{f(X_i)}{p(X_i)}\right] \approx \frac{1}{N}\sum_{i=1}^{N}\frac{f(x_i)}{p(x_i)}$$ Where $X$ and $X_i$ are distributed according to the probability density $p$. As evident the main difference to the Monte Carlo formulation you are probably used to is the $p(x)$ factor, and the fact that $x_i$ are not uniformly sampled but rather sampled according to $p$. Note that for uniform sampling $p(x)=1$ and the equality still holds. Thus this is simply a generalization.

| improve this answer | |
  • $\begingroup$ Thank you for your answer. Two comments: the way rays are scattered changes when he makes a mixture pdf in chapter 8, which I haven't implemented yet, but before that (when we are playing around with different pdf) this doesn't seem to change. Second, so the contribution of each ray changes both 1) for the sampling, which makes rays with lower theta more likely to be sampled, 2) and for the multiplication term which depends on the probability. Is this correct? $\endgroup$ – maurock Mar 21 '19 at 10:32
  • $\begingroup$ @MauroComi If you're talking about chapter 7 only (which you should have mentioned explicitly) then he doesn't do brdf sampling at all, as obvious by the code and the images. You can see that he returns the contribution after only one bounce. In chapter 8 brdf and light sampling are combined. The contribution changes since you're not sampling uniformly anymore - I have derived why this is necessary in my answer above, and in this case - yes, directions closer to the normal are more likely to be sampled, the pdf weight 'compensates' for this. I don't get what mult term are you talking about. $\endgroup$ – lightxbulb Mar 21 '19 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.