# Importance sampling in a path tracer

So, I'm attempting to implement a monte carlo path tracer that uses the ggx brdf model. However, I'm rather confused about how to sample the light direction. I know that theta and phi can be sampled like this:

$$\theta=\arctan\left(\alpha\sqrt\frac{\xi_1}{1-\xi_1}\right)$$

$$\phi=2\pi\xi_2$$

But whose coordinates are these? Are they the microfacet normal's (half vector), or are they those of the light direction?

If they are the coordinates of the light direction, how should they be transformed into the world-space coordinates?

Also, can I use this $$\theta$$ directly to calculate the BRDF?

I think the answer is: neither, since this GGX distribution is NOT sampling the direction for a ray but a microfacet normal. According to the paper of GGX: Microfacet Models for Refraction through Rough Surfaces, this distribution is used to sample the normal direction for the microfacet, which you can think of as sampling a new normal direction with respect to the original normal direction (macroscopic). Therefore, the coordinate frame is spanned by the original macroscopic normal, for example, we use the macroscopic normal as local z-axis, then the $$\theta$$ here is the angle between the newly sampled microfacet normal and the macroscopic normal, as shown in the following figure.

So, how do you get the direction of the light? First, we get the normal we want just by using GGX, to sample a microfacet normal. Then, maybe we will start to sample surface event like whether we evaluate the diffuse part or the specular part. Take evaluating specular part as an example, now we have a incident direction, a microfacet normal, then we can directly calculate the outgoing direction.

Also, can I use this θ directly to calculate the BRDF?

Yes. Sampling an outgoing direction will also require you to calculate the path throughput. This throughput coefficient is calculated using the parameters you've sampled.

So, in all, sampling a new ray direction is usually pretty local around one specific normal (geometric / shading / sampled microfacet normal), so we will usually set the frame according to the normal. However, this is not the case when you are dealing with medium scattering (phase function). In this case, the sampling is usually done in the frame spanned by original incident ray direction.

• By light direction I suppose you really mean $\omega_i$, i.e. the direction of the next ray bounce. By incident direction I suppose you mean $-\omega_o$, in a backwards ray-tracer setting, and by specular the reflection of $\omega_o$ around the sampled microfacet normal $m$. I believe one has to also account for a Jacobian for this transform too. Jul 16, 2023 at 8:57
• Yes. My answer is just an intuitive illustration for the whole process. To be more rigorous, we should be more careful about these terms and the Jacobian you've mentioned. Jul 16, 2023 at 10:00
• This is just extremely helpful, I've just been completely clueless about what to do with the sampled vector. I had a hunch that it had to be the microfacet normal as if it was the ray direction, then it would have absolutely no correlation to the incident ray, well, the viewing direction, considering that I'm writing a backwards path tracer. There is still the matter of how to handle a ray that reflects into the surface of the object. Intuitively I think that the ray should just terminate, as reflecting into the surface can be interpreted as it being absorbed. Jul 16, 2023 at 10:46
• You terminate if the ray ends up below the surface, the contribution is zero in the rendering equation because n dot w_i becomes negative, i.e. you clamp it to zero. To avoid this and sample until you get a correct direction you would have to have a very specific normalisation computed from an integral not over the hemisphere, but some parts of it, which is probably hard. Jul 16, 2023 at 11:47