# Cook-Torrance / BRDF General

This topic is 745 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I decided to test out Cook-Torrance in place of normalized Blinn-Phong, and while it's not difficult to implement, it's difficult to know if I'm doing it right, mostly because there is some variation depending on what sources you look at. For instance, I've seen three versions for the Cook-Torrance equation:

[attachment=13494:eqn1.png]

I saw PI in the denominator in Cook's paper, I believe I saw nothing in the version in a publication by Wolfgang Engel and in gpwiki.org's D3DBook (probably copied), and on wikipedia there apears a 4, as well as on a video I saw from UC Davis.

Then there is the NDF:

[attachment=13495:eqn2.png]

This is how it apeared in Cook's paper, but I've also seen:

[attachment=13496:eqn3.png]

Someplace I saw that PI was a normilization factor. I'm not sure if this is correct, and if it is why it was not present in Cook's paper or other papers for that matter. I want to be sure that everything is normalized and energy conserving.

Maybe as one last question, I'd like to know the propper way for ensuring that there is energy concervation between specular and diffuse.

##### Share on other sites

Here you go (slide 58)

To ensure energy conservation between specular and diffuse:

fresnelDiffuse = FSchlick(cSpec, light, normal);
fresnelSpecular = FSchlick(cSpec, light, halfway vector);
finalColor = specularColor*fresnelSpecular + diffuseColor*(1-fresnelDiffuse)

You can even approximate the diffuse fresnel (according to tri-Ace):

finalColor = specularColor*fresnelSpecular + diffuseColor*(1-cSpec)


You can use the Schlick Fresnel approximation (in slide 44) to calculate the fresnel.

EDIT: Distinction between diffuse and specular fresnel.

Edited by TiagoCosta

##### Share on other sites
Tiago, that's an approximation, even with the real Fresnel equations, but is not really correct.

Fresnel is dependent on the microfacets oriented towards the halfway vector, but diffuse actually is all the light that is not being reflected, not being absorbed and scattered back out, independent of the microfacets oriented towards the halfway vector. So simply the complement of a single fresnel value won't do it. You would need to solve the integral over all the microfacet orientations with a modified microfacet model:

Your approximation might actually be worse than not having a factor for diffuse at all. If anything I'd use the macro surface normal instead of the halfway vector (just for diffuse though, for specular you should use the microfacet normal):

Edited by CryZe

##### Share on other sites

Tiago, that's an approximation, even with the real Fresnel equations, but is not really correct.

Isn't real-time graphics programming all about approximations?

This is simply the way I've been doing it (that I learned from papers/presentations).

Your approximation might actually be worse than not having a factor for diffuse at all. If anything I'd use the macro surface normal instead of the halfway vector (just for diffuse though, for specular you should use the microfacet normal):

You're right! I just checked my shaders and there's indeed a (1-Fdiffuse) calculated using the macro surface normal and Fspec using the halfway vector).

I just checked a tri-Ace paper and they even approximate the diffuse term like this:

diffuse = (diffuseColor/PI) * (1-f0)
//where f0 is the reflectance at normal incidence


P.S: Could you fix the equations in you're post?

##### Share on other sites
As an employee of tri-Ace I can confirm that we use the Fresnel approximation.  In the world of graphics, “approximation” always means “less accurate, but faster.”

But to use the Fresnel term to modify the diffuse is not always correct.
Imagine if the specular color is [0, 0, 0].  It is the same as there being no specular term.  Which means if you try to balance the energy between the specular and diffuse by just using the Fresnel term, and if the Fresnel term is close to 1, you would have multiplied the diffuse by almost 0 and yet added almost nothing from the specular term.  Since energy conservation depends on how much gets reflected directly as the specular term vs. how much gets reflected as the diffuse term, the balance depends on the specular term itself as well as other factors (such as microfacets).

But I am not the originator of those papers, my boss is.  And I am still learning from him the wonderful world of physically based rendering.
I don’t know yet myself how to balance the terms for every rendering equation, but I am learning, and I know enough to at least say that the Fresnel term alone is not enough.
To be quite frank, I would love it if anyone could post how to go about balancing these terms for any rendering equation.  Not to be spoon-fed the way for just one equation, but to be taught the method I could use to determine a solution for any equation.

L. Spiro Edited by L. Spiro

##### Share on other sites

So here is the implementation I'm ussing at the moment:

float3 H = normalize(L + V);
float NdotL = max(0, dot(N, L));
float NdotH = dot(N, H);
float NdotV = dot(N, V);
float VdotH = dot(V, H);

float NdotH_2 = NdotH * NdotH;
float NdotH_4 = NdotH_2 * NdotH_2;
float m_2 = m * m;
float D = exp((NdotH_2 - 1.0) / (NdotH_2 * m_2)) / (PI * m_2 * NdotH_4);

float3 F = R0 + (1.0 - R0) * pow(1.0 - VdotH, 5.0);

float geo_a = (2.0 * NdotH * NdotV) / VdotH;
float geo_b = (2.0 * NdotH * NdotL) / VdotH;
float G = min(1.0, max(0, min(geo_a, geo_b)));

float3 Rs = (D * F * G) / (4 * NdotV * NdotL);
float3 Rd = Cdiff * NdotL;
float3 R = Rs * NdotL + Rd * (1.0 - R0);


Is this correct?

##### Share on other sites

I've approximated the diffuse transmittance integral and created a BRDF which is pretty lightweight but also pretty physically accurate. Use this instead of Lambert if you want to have proper energy conservation. It's based on GGX roughness though, so you might need to convert your roughness to GGX roughness:

It's actually just a single MAD instruction per light if you implement it, the rest can be done on a per pixel basis.

Edited by CryZe

##### Share on other sites

Ok, so I've been giving GGX a look. Unfortunately I haven't been able to find much information on it, at least not nearly as much as Beckmann's.

I gather this is the correct normalized version?

[attachment=13573:eqn.png]

Currently I've only evaluated the Blinn-Phong NDF, Beckmann and what I think is the correct GGX NDF. One thing that I noticed right away is that Blinn-Phong and Beckmann are nearly identical for roughness values below 0.3 or so. From 0.3 to 0.5 they are still pretty similar. GGX on the other hand is substantially different across the board, and that was with the Cook-Torrance G function. Next I tried your BRDF and the differences grew even further. I'm not sure which is more physically correct, but to my eyes I think I might prefer Beckmann + Cook-Torrance.

Here is a graph of theta H for a roughness of about 1.2:

[attachment=13574:Untitled.png]

Blue is Beckmann and C-T G. Red is GGX and C-T G. Green is your BRDF.

Edited by Chris_F

##### Share on other sites

The only difference between mine and your GGX, is that I'm using the Walter GGX geometric term which is specifically calculated using Smith's "blackbox" function to convert any distribution function into a perfectly matching geometric term. The thing is, that the cook torrance geometric term is more or less completely absurd and unrealistic, because the shadowing and masking should be dependent on the microfacet distribution, but the cook torrance geometric term completely ignores that. That's why you get that unrealistic cut at 45 degrees. I'd recommend you take a look at Naty Hoffmann's presentation and Disney's presentation at http://blog.selfshadow.com/publications/s2012-shading-course/. You should also check out Disney's BRDF Explorer and the MERL database. With the BRDF explorer, you can validate how well your BRDF matches actual materials from the MERL database.

Also, your GGX is the correctly normalized version ;)

Edited by CryZe

##### Share on other sites

Looking through one of Disney's papers, I came across what they refer to as "Generalized Trowbridge-Reitz."

[attachment=13595:Untitled.png]

The one in the middle is supposedly Trowbridge-Reitz/GGX, and the one on the bottom is the generalized version, but I don't understand the way they have their equations set up.

Edit:

Ok, after looking at a more detailed paper, it looks like the normalized version of their Generalized Trowbridge-Reitz is this:

[attachment=13596:eqn.png]

This is interesting because by altering gamma you can adjust the size of the specular tail. Adjusting it doesn't seem very intuitive though. Adjusting gamma not only changes the size of the specular tail, but also changes the point at which it peaks. Is there a way to make alpha and gamma independent?

Edited by Chris_F

##### Share on other sites

Hello !

(1 - f0) is really bad for larger than 0 Roughness values.

You should integrate the fresnel with GGX or some other microfacet models.

But I know a good approximation, u can just simply integrate fresnel from 0 to Roughness then divide by Roughness
Assume Fresnel(0, SpecularColor) gives you SpecularColor
Assume Fresnel(1, SpecularColor) gives you 1

finalcolor = specular + someNonFresnelDiffuseModel * integrate( (1 - Fresnel) / R , from 0 to R )

R division needed because the same amount of microfacets should lie between 0 and R

FGD / ( 4 * NoV * NoL) is the good Cook Torrance equation. I checked it numerically and analitically, It's perfect for geometrical behavior. Diffraction is another question

Edited by HSDXPRO