# Implementing the derivation of the Smith Shadowing function for arbitrary distributions

I'm trying to implement the derivation of the masking function G_1 by Smith. The appendix of Walter et al 1, Hammons presentation 2 and this research paper 3 talk in detail about it and I feel as I do understand the majority of the math behind it. But when it comes to turning this into code I cannot seem to get it right.

Some of my questions entail:

In the first integral from minus infinity to infinity over P22(p, q), the way I understand it I can simply iterate over all of my microfacets and use the previous formular (P22(p, q) = D(m) cos^4(theta_m)) and add them together for a riemann sum?

As far as I understand is mu (= |cot(theta_v)|) the slope of the view vector and when using an integral from mu to infinity the goal is to decrease te angle of theta_v until it is zero?

The way I naivly implemented it is as an nested loop of two riemann sums that aim to describe the two integrals. In pseudocode:

calculate mu from view angle
Aux = 0.0;
//for loop uses calculation of the angle theta_v to decide termination of loop
for(float q = mu; angleof(q) > 0.0001f; q += dq)
{
float P2 = 0.0;
for(iterate through all maicrofacets)
{
calulate theta of current microfacet
calculate cos^4(theta)
get D of current microfacet

P2 += D * cos^4(theta) * dp;
}
Aux += (q - mu) * P2 * dq;
}

Aux *= 1.0 / mu;
return 1.0 / (1.0 + Aux)


I write in C++ but I would be very happy with any kind of code or hint as to how to solve this.