## Energy-Conserving Phong Specular Lobe

Practical and theoretical implementation discussion.
Geometrian
Posts: 29
Joined: Sun Jul 28, 2013 6:53 pm
Contact:

### Energy-Conserving Phong Specular Lobe

I am trying to get correct Phong specular for a simple path tracer.

The test scene is a plane with no diffuse but a specular component. Above the plane is a dome emitting light <0.5,0.5,0.5>, with no reflection at all. The camera is inside the dome. The dome appears gray and, since every ray bouncing off the plane intersects the dome, the plane should appear the same.

I have not been able to get energy-conserving results (the plane appears too dark or too bright).

Q1: Generating rays with equal probability on a hemisphere, what is the BRDF?

Q2: How should I generate rays for importance sampling, and what is the BRDF in that case? I can generate rays according to a cos^n lobe along a vector (say the reflection vector). Should I generate cos^n weighted rays around R (the reflection vector) and then regenerate the rays that fall below the ecliptic plane?

For what it's worth, I tried e.g. this code (edited to pseudocode for clarity), which I implemented from the Global Illumination Compendium 66.a:

Code: Select all

function radiance(...) {
//Intersection and stuff

result += emission;

//GET BRDF
Float w_i = get_hemisphere(reflected); //uniform sample on hemisphere
Float pdf_w_i = 1 / (2*PI);

Vec3 ks = refl_spec * (refl_exp+2)/(2*PI);
Vec3 brdf = ks * pow(dot(reflected,w_i),refl_exp);

//USE BRDF & RECURSE
result += radiance(...) * brdf * dot(N,w_i) / pdf_w_i;

//stuff
}

graphicsMan
Posts: 167
Joined: Mon Nov 28, 2011 7:28 pm

### Re: Energy-Conserving Phong Specular Lobe

Just making sure... you've looked at Lafortune's paper on the topic? I believe it addresses sampling.

Geometrian
Posts: 29
Joined: Sun Jul 28, 2013 6:53 pm
Contact:

### Re: Energy-Conserving Phong Specular Lobe

This one? I took a quick look just now, and there seems to be some semi-promising things.

My progress so far has been to take the Monte Carlo expression of the rendering equation:
L'=\int_\Omega L f_r(N\cdot\omega_i)d\omega_i=E\left[\frac{L f_r(N\cdot\omega_i)}{pdf(\omega_i)}\right]
Under the energy conserving constraint and uniform pdf:
L=L'
pdf(\omega_i)=\frac{1}{2\pi}
And solve it for "x" in a specular BRDF of the form:
f=k_s(R\cdot\omega_i)^n x

This results in code for sampling (thus solving Q1):

Code: Select all

Float gamma = acos(dot(w_o,N));

w_i = get_hemisphere(N); //uniform hemispherical sample around N
pdf_w_i = 1 / (2*PI);

if (dot(R,w_i)<=0) return Vec3(0,0,0);

return refl_spec * pow(dot(reflected,w_i),refl_exp) / _get_spec_integral_31a(gamma);
I have worked through something similar for importance sampling, but I'm screwing up somewhere. My code for importance sampling looks like:

Code: Select all

Float integral_ratio = ((2*PI)/(refl_exp+1)) / _get_spec_integral_31b(gamma);

do {
w_i = get_hemisphere_cosine_n(R,refl_exp);
} while (dot(w_i,N)<=0);
pdf_w_i = (refl_exp+1)/(2*PI) * pow(dot(w_i,R),refl_exp) * integral_ratio;

return refl_spec * pow(dot(w_i,R),refl_exp) * (refl_exp+1)/PI * integral_ratio;

tarlack
Posts: 27
Joined: Mon Feb 10, 2014 7:48 am

### Re: Energy-Conserving Phong Specular Lobe

I think you mix two independent things together in the single word "BRDF" :
- on one side, the BRDF, which is the ratio of reflected radiance over incident intensity (purely physical quantity)
- completely independently of that, the sampling method, which exists only for Monte-Carlo. It is just a way to select an outgoing direction. Once this outgoing direction is known, you can evaluate the BRDF for it. If you do simple whitted raytracing for instance, you do not have a sampling method, you only evaluate the BRDF for the specularly reflected direction.

What links the two in a mathematical expression is the monte-carlo estimator, which computes the final contribution from the BRDF value and the probability density from your sampling method.

The crucial point is that you can use any combination of BRDF and sampling method, the result, in the limit, will always be the same. Well, this will be the case as long as the sampling method can generate all the directions for which the BRDF is non-zero. This invariance allows you to verify that, when implementing a new sampling method, the PDF computation and sampling algorithm are coherent (ie directions are actually generated with a density that follows the PDF).

graphicsMan
Posts: 167
Joined: Mon Nov 28, 2011 7:28 pm

### Re: Energy-Conserving Phong Specular Lobe

Geometrian
Posts: 29
Joined: Sun Jul 28, 2013 6:53 pm
Contact:

### Re: Energy-Conserving Phong Specular Lobe

As I understand it, PDFs are the probability density associated with choosing a ray; they correspond directly to the method of sampling. BRDFs define a proportion of light reflected. I think I've got that pretty well.

I redid everything more carefully and have arrived at the following:

Code: Select all

Vec3 R = get_reflected_around(w_o,N);

w_i = get_hemisphere_cosine_n<Float>(R,refl_exp);
if (dot(w_i,N)<=0) return Vec3(0,0,0);

pdf_w_i = (refl_exp+1)/(2*PI) * pow(dot(w_i,R),refl_exp);
Followed by (my derivation):

Code: Select all

Float gamma = acos(dot(w_o,N));
//_get_spec_integral_31b1 returns the Global Illumination Compendium eqn. 31.b with specular exponent 1.
return ( refl_spec * pow(dot(R,w_i),refl_exp) * (refl_exp+1) ) / _get_spec_integral_31b1(gamma);
Or else followed by, as near as I could figure out from the paper:

Code: Select all

return (refl_exp+2)/(refl_exp+1) * refl_spec * dot(n,w_i);
Neither conserves energy.

papaboo
Posts: 46
Joined: Fri Jun 21, 2013 10:02 am
Contact:

### Re: Energy-Conserving Phong Specular Lobe

Disclaimer: In a rush, so only glanced at the code. Hope this is still helpful.
The microfacet model is generally not energy conserving, which I personally see as a feature, not a drawback.
The idea is that rays that reflect of your surface and are reflected into the material will bounce around inside the material forever and never escape (or if it does the contribution is so tiny that the difference isn't important.) The result of this is that rough surfaces viewed head-on have a darker/rougher look to them, while having a brighter reflection at grazing viewing angles.
If you really want to get around this you can try to approximate the directional hemispherical reflectance function, rho, and use that to scale your BRDF, which in essence will give you a rho of 1.

Approximating Rho can be done using standard monte carlo and baking it into a texture with cosTheta and roughness as texture coordinates. See Real Shading in Unreal Engine 4 for a more detailed explanation.

viewtopic.php?f=3&t=1939&hilit=microfacet
viewtopic.php?f=3&t=1820&p=3910&hilit=energy#p3910

ypoissant
Posts: 97
Joined: Wed Nov 30, 2011 12:44 pm

### Re: Energy-Conserving Phong Specular Lobe

Geometrian wrote:I am trying to get correct Phong specular for a simple path tracer.

The test scene is a plane with no diffuse but a specular component. Above the plane is a dome emitting light <0.5,0.5,0.5>, with no reflection at all. The camera is inside the dome. The dome appears gray and, since every ray bouncing off the plane intersects the dome, the plane should appear the same.

I have not been able to get energy-conserving results (the plane appears too dark or too bright).
Just a few notes.

The energy conservation scene you describe will work for Lambertian BRDF. But for a Phong based BRDF, because radiance is not reflected uniformly, the plane will 'appear' darker or brighter depending on the camera position and the Phong exponent. That is normal.

To know how your BRDF conserves energy, you must compute the hemispherical-hemispherical reflectance.

Better yet, tabulate the directional-hemispherical reflectance for 0..pi/2 directions and for a good set of Phong exponents to see the energy conservation behavior.

Geometrian
Posts: 29
Joined: Sun Jul 28, 2013 6:53 pm
Contact:

### Re: Energy-Conserving Phong Specular Lobe

papaboo wrote:The microfacet model is generally not energy conserving, which I personally see as a feature, not a drawback.
The idea is that rays that reflect of your surface and are reflected into the material will bounce around inside the material forever and never escape (or if it does the contribution is so tiny that the difference isn't important.) The result of this is that rough surfaces viewed head-on have a darker/rougher look to them, while having a brighter reflection at grazing viewing angles.
In my experiments, it has been just the opposite. The specular lobe ends up intersecting the ecliptic plane, leading to less energy reflected.
papaboo wrote:If you really want to get around this you can try to approximate the directional hemispherical reflectance function, rho, and use that to scale your BRDF, which in essence will give you a rho of 1.
If I understand what you're suggesting, this is what I have been trying. I derived normalization terms for non-importance-sampled Phong on my website here. I have since been able to get a normalization term for the importance-sampled Phong, simply by changing out the sampling technique/PDF (although I still haven't been able to derive it from first principles). For other BRDFs, naturally, closed-forms might not exist and can be approximated with Monte Carlo algorithms (I have done this before myself). Either way, I encode these in a lookup table parameterized on angle and sampled at some high frequency.
The energy conservation scene you describe will work for Lambertian BRDF. But for a Phong based BRDF, because radiance is not reflected uniformly, the plane will 'appear' darker or brighter depending on the camera position and the Phong exponent. That is normal.
Can you clarify what you mean by this? If no energy is lost to absorption, every specular ray hits the light and receives a constant intensity? If the BRDF is normalized (as mine is, above), then by reciprocity this should produce the same output radiance along the viewing ray, no?

ypoissant
Posts: 97
Joined: Wed Nov 30, 2011 12:44 pm

### Re: Energy-Conserving Phong Specular Lobe

Geometrian wrote:
The energy conservation scene you describe will work for Lambertian BRDF. But for a Phong based BRDF, because radiance is not reflected uniformly, the plane will 'appear' darker or brighter depending on the camera position and the Phong exponent. That is normal.
Can you clarify what you mean by this? If no energy is lost to absorption, every specular ray hits the light and receives a constant intensity? If the BRDF is normalized (as mine is, above), then by reciprocity this should produce the same output radiance along the viewing ray, no?
You are using a hemispherical source so the reflected radiance should never go higher than 0.5. But it can go lower. The Phong lobe is oriented around the reflection vector. So the reflection hemisphere is not oriented around the surface normal. In other words, the reflection sampling hemisphere is not coincident with the light source hemisphere. This means that some samples will go under the macrosurface and contribute nothing by not hitting the hemispherical light source. Suppose the reflection ray is 90° from the surface normal. Then 50% of the reflected rays will go under the macrosurface.

This is not only a property of Phong reflection model but also a property of microfacet based BRDF. Some reflection rays will go under the macrosurface and you end up with some loss of energy. This is less severe for microfacet-based BRDF though because the reflection pattern on microfacet normals is better behaved.