• Create Account

## Cook-Torrance / BRDF General

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1Chris_F  Members

3018
Like
0Likes
Like

Posted 02 February 2013 - 10:54 AM

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:

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:

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

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.

### #2TiagoCosta  Members

3599
Like
0Likes
Like

Posted 02 February 2013 - 03:25 PM

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, 03 February 2013 - 05:31 AM.

### #3CryZe  Members

773
Like
2Likes
Like

Posted 02 February 2013 - 08:09 PM

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, 03 February 2013 - 11:09 AM.

### #4TiagoCosta  Members

3599
Like
0Likes
Like

Posted 03 February 2013 - 05:22 AM

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?

### #5L. Spiro  Members

24842
Like
2Likes
Like

Posted 03 February 2013 - 06:00 AM

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, 10 February 2013 - 07:35 AM.

### #6Chris_F  Members

3018
Like
0Likes
Like

Posted 03 February 2013 - 06:30 AM

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?

### #7CryZe  Members

773
Like
5Likes
Like

Posted 03 February 2013 - 11:22 AM

POPULAR

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)));

You can improve this part of the code this way:
float g_min = min(NdotV, NdotL);
float G = saturate(2 * NdotH * g_min / VdotH);
Also, don't ever use max(0, dot(a, b)). Instead use saturate(dot(a, b)) which compiles into a single instruction.

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

It is, but is using the complement of Fspecular actually a good one? I don't think so (unless you're using Fdiffuse).

I think someone should approximate a diffuse BRDF using the equation I posted in my post above. That would be a way better approximation.

To get back to the original topic:

The last one is the correct Cook-Torrance microfacet model. Sometimes you find (ns + 2) / (2 * pi) or (ns + 2) / (8 * pi) as the normalization factor for Blinn-Phong. The second one is already pre-multiplied with the 1/4 while the first one is the distribution function for the microfacet model.

And this one is the correct Beckmann NDF:

I wouldn't recommend the Beckmann NDF though. It's pretty damn slow in comparison to other NDFs because of 2 reciprocals and the exponential function. (Y u no use GGX xD)

This is the BRDF I'm using:

I'm using GGX as the distribution function, Schlick's approximation of fresnel as fresnel term and Walter's geometric term for the GGX distribution function.

I color-coded everything for implementation details. The grey parts are just parts of the BRDF and don't need to be implemented. The green parts can be calculated once for every pixel. And the red parts are the only parts, that actually need to be calculated for every light.

Edited by CryZe, 04 February 2013 - 06:36 AM.

### #8CryZe  Members

773
Like
2Likes
Like

Posted 10 February 2013 - 06:30 AM

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, 10 February 2013 - 01:14 PM.

### #9Chris_F  Members

3018
Like
0Likes
Like

Posted 10 February 2013 - 01:13 PM

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?

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:

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

Edited by Chris_F, 10 February 2013 - 01:23 PM.

### #10CryZe  Members

773
Like
0Likes
Like

Posted 10 February 2013 - 04:21 PM

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, 10 February 2013 - 04:22 PM.

### #11Chris_F  Members

3018
Like
0Likes
Like

Posted 11 February 2013 - 02:27 PM

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

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:

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, 11 February 2013 - 03:51 PM.

### #12HSDXPRO  Members

103
Like
0Likes
Like

Posted 08 January 2016 - 11:25 AM

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, 08 January 2016 - 11:30 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.