# Cranberry Lair

## Walking the programming parameter space

I’ve recently been learning more about BRDFs and their properties and I stumbled across the concept of energy conservation. Energy conservation simply states that your BRDF should not emit more energy than it receives. To determine if a BRDF is energy conserving, you accumulate all the light emitted by a surface across a hemisphere centered at the normal of that surface and assure that the accumulated light is less than or equal to the amount of received light.

The mathematical equation for this is:

$\int_{\Omega}BRDF*cos(i)*Li*d\omega<=Li$

Where $Li$ is the incoming light, $i$ is the angle from our normal to our current accumulation direction.

Since $Li$ is a constant, we can remove it and we’re left with:

$\int_{\Omega}BRDF*cos(i)*d\omega<=1$

Faced with this equation, I was struck. Where did that cosine come from?

## Where did you come from? Where did you go? Where did you come from Cosine I Joe.

My first instinct was to brush it off. “It’s just the Lambert cosine factor”. False! The Lambert cosine factor was applied in $Li$ which we removed.

I then began my slow decline into madness.

I was very confused. With a lot of stumbling I came up with various answers, each not as satisfying as I would like them to be.

Radiant exitance I yelped! Is it related to this “physics” and this “poynting vector” that you find when researching radiant flux? It might be, but I’m not proficient in the dark magic of physics. And so I moved on.

Can we instead think of it using the Helmholtz reciprocity principle that says that we can reverse our view and light vectors? This would allow us to flip the meaning of $cos(i)$ from our viewing direction to our light direction and then simply say it’s the Lambertian cosine factor. But this didn’t feel right. We’re not talking about Lambertian materials, we’re talking about accumulating light from a generalized material. I decided to move on from this theory.

Finally, it struck me. The circle had done it! It’s always the circle.

## It’s just geometry

Let’s take a look at what we’re doing when we’re calculating how much light is being emitted by a surface in 2D.

If we start with the integration of the BRDF across a 2D circle without the cosine factor, we get:

$\int_{\Omega}BRDF*dS$

Where $dS$ is our integration segment.

What this means is that We’re splitting the circle into equal lines and multiplying our emitted light by the width of each viewing segment.

Let’s take a look at each integration part in succession. Our integration method implies that we want to multiply our current light value by the width of our integration, as a result, we’ll draw 2 parallel lines for each line segment to represent the shape that would be the result of our multiplication.

When viewed top down, everything is great!

Oops! That doesn’t line up with our observed area, our perceived area is containing more than our desired $dA$. What do we do about it?

Let’s look at it from the perspective of $dA$ projecting onto a line at different angles

Notice how our area is getting smaller as our angle gets steeper? That’s what our integration segment should be doing to remove the extra area that we observed.

Let’s figure out the proportion of that shrinking area.

Notice that we can calculate the length of our projection using a right angle triangle (It always comes back to triangles).

Our hypotenuse is $dA$, our angle is $i$ and our projected area is $dP$.
Remembering our identities, SOH CAH TOA, Sine of our angle is equal to our opposite over our hypotenuse. This gives us $dP/dA = sin(i)$ which we can reformulate as $dP=sin(i)*dA$.

$dP$ here is the area of our integration segment, it is the value by which we want to multiply our accumulated light. Now that we know how to get $dP$, we want to formulate $dA$ in terms of our integration segment $dS$.

Since we know that $dS$ from the top down (when $i$ is $\pi/2$) matches $dA$ we can say that $dS = sin(\pi/2)*dA$ which simplifies to $dS = dA$, as a result, we can replace our $dA$ in the above to get $dP = sin(i)*dS$.

We can therefore modify our integral to be $\int_{\Omega}BRDF*dP$ and substituting $dP$ we get $\int_{\Omega}BRDF*sin(i)*dS$. Close! But why is it a sine and not a cosine? This is because our angle $i$ is the angle from the plane of our normal to our normal but the information that we have is the angle from our normal to our plane (using N dot V). Let’s call the angle from the normal to the viewing direction $\theta$. We know that $\theta=\pi/2-i$. If we solve for $i$ we get $\pi/2-\theta=i$ and substituting to get $\int_{\Omega}BRDF*sin(\pi/2-\theta)*dS$ which finally becomes $\int_{\Omega}BRDF*cos(\theta)*dS$!

We’ve reached the end. This is where our cosine comes from!

## Final thoughts

I spent quite some time trying to unravel this mystery, but it’s always exciting to get just that little bit of extra intuition. Hopefully this helps you understand it just a little bit more!

## Side notes

Q: Why don’t we always do this cosine when integrating over a hemisphere?

A: Because we’re not always integrating a differential area ($dA$). It would be wrong to always apply that rule if we’re instead integrating a smaller hemisphere, or some other geometrical concept

Q: Why don’t we multiply our light by this cosine factor when we’re rendering?

A: Because it’s implicit in our perspective projection. When we’re viewing the surface it already covers less of our viewing surface when viewed at an angle. If we applied the cosine when rendering we would be applying it twice. We apply it here because we don’t have any perspective projection applied when doing our integration.

## Premise

I’ve recently been thinking about different methods to improve my code quality. In this case quality equates to the number of failure states (crashes) a program has. One option I’ve been considering is programming through what I affectionately called Expected Failure. Or EF (pronounced F!)