• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# BRDF gone wrong

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.

3 replies to this topic

### #1cifa  Members

Posted 20 April 2014 - 03:21 AM

Hi all,

After reading 'bout BRDF I tried to implement it in a shader, but things apparently goes wrong, but don't know why. It's my first attempt to go beyond a basic phong shading, so sorry if the following is a silly question.

First of all here's the model with just the diffusion component:

Now if I activate the specular component as well the result is strange:

The right side of the face is completely blackened and overall it looks strange.  This was with the beckmann distribution, if I switch to Blinn:

Same issue.

My frag shader (relevant part) is:

float beckmannD(float roughness,float NdotH){
float roughness2 = roughness*roughness;
float roughness4 = roughness2*roughness2;
float NdotH2 = NdotH*NdotH;

float term1 = 1/(roughness4*NdotH2*NdotH2);
float expTerm = exp((NdotH2 - 1)/(roughness4*NdotH2));
return term1*expTerm;
}

float blinnD(float roughness, float NdotH){
return (roughness + 2)/2 * pow(NdotH, roughness);
}

float kelemenG(float NdotL, float NdotV, float LdotV){
float numerator = 2*NdotL*NdotV;
float denominator = 1+LdotV;
return numerator/denominator;
}

float implicitG(float NdotL, float NdotV){
return NdotL*NdotV;
}

....

void main(void){

vec3 N = texture2D(uNormalSampler, vTextureCoord).xyz;
N =  N*2.0 - 1.0;
N = normalize(N*uNMatrix);
vec3 L = normalize(uLightPosition-vPosition.xyz);
vec3 V = normalize(-vPosition.xyz);

...

vec3 H = normalize(V+L);
float NdotH = max(dot(N,H),0.0);
float NdotL = max(dot(N,L),0.0);
float NdotV = max(dot(N,V),0.0);

D = beckmannD(roughness, NdotH);
// or
D = blinnD(roughness,NdotH);

float LdotV = max(dot(L,V),0.0);
G = kelemenG(NdotL, NdotV, LdotV);
//or
G = implicitG(NdotL,NdotV);

// Note I'm not considering Fresnel yet
specularWeighting = D*G/(3.14*NdotL*NdotV);
vec3 diffuseColor = vec3(1.0,0.6,0.6);
vec3 diffuseReflection = uLightColor * diffuseColor * diffuseWeighting;
vec3 specularReflection = specularColor * specularWeighting;

fragmentColor = vec4(uAmbientColor+diffuseReflection+specularReflection,1.0);

}



What can be? Thank you very much, and sorry for the probably silly issue.

### #2tanzanite7  Members

Posted 20 April 2014 - 05:01 AM

Unexpected black patches are indicative of NaN values - might want to check/pinpoint that (glsl has "isnan" function from 1.3 onwards ... iirc).

Possible sources of NaN: http://en.wikipedia.org/wiki/NaN

### #3MJP  Moderators

Posted 20 April 2014 - 02:50 PM

POPULAR

The most common cause of NaN in a shader is division by 0. In your case you will get division by 0 whenever NdotL or NdotV is 0, since your denominator has those terms in in it. To make that work with your current setup, you would need to wrap your specular calculations in an if statement that checks if both N dot L and N dot V are greater than 0. However in many cases it's possible to write your code in such a way that there's no chance of division by 0. For instance, take your "implicit G" function. This is meant to cancel out the NdotL * NdotV in the denominator by putting the same terms in the numerator. So in that case, it would be better if you canceled it out in your code by removing the implicitG function and then also removing the N dot L and N dot V from the denominator.

Also I should point out another common mistake that you're making, which is that you need to multiply your entire BRDF by NdotL. If you look up the definition of the BRDF, you'll find that it's the ratio of lighting scattered towards the eye (which is the value you're computing in your fragment shader) relative to the irradiance incident to the surface. When you're dealing point lights/spot lights/directional lights/etc. the irradiance is equal to LightIntensity * LightAttenuation * Shadowing * NdotL. In your case you don't have shadows and you don't seem to be using an attenuation factor (which is fine), you 'll want to multiply your specular by (uLightColor * NdotL). A lot of people tend to associate the NdotL with diffuse, but really it's not part of the diffuse BRDF. A lambertian diffuse BRDF is actually just a constant value, the NdotL is part of the irradiance calculations.

### #4cifa  Members

Posted 22 April 2014 - 05:33 PM

The most common cause of NaN in a shader is division by 0. In your case you will get division by 0 whenever NdotL or NdotV is 0, since your denominator has those terms in in it. To make that work with your current setup, you would need to wrap your specular calculations in an if statement that checks if both N dot L and N dot V are greater than 0. However in many cases it's possible to write your code in such a way that there's no chance of division by 0. For instance, take your "implicit G" function. This is meant to cancel out the NdotL * NdotV in the denominator by putting the same terms in the numerator. So in that case, it would be better if you canceled it out in your code by removing the implicitG function and then also removing the N dot L and N dot V from the denominator.

Also I should point out another common mistake that you're making, which is that you need to multiply your entire BRDF by NdotL. If you look up the definition of the BRDF, you'll find that it's the ratio of lighting scattered towards the eye (which is the value you're computing in your fragment shader) relative to the irradiance incident to the surface. When you're dealing point lights/spot lights/directional lights/etc. the irradiance is equal to LightIntensity * LightAttenuation * Shadowing * NdotL. In your case you don't have shadows and you don't seem to be using an attenuation factor (which is fine), you 'll want to multiply your specular by (uLightColor * NdotL). A lot of people tend to associate the NdotL with diffuse, but really it's not part of the diffuse BRDF. A lambertian diffuse BRDF is actually just a constant value, the NdotL is part of the irradiance calculations.

Oh thank you very much! I indeed re-wrote everything (that snippet is an extract) so to  avoid division as much as possible and it now works like a charm. As for the NdotL issue thank you for pointing out, I did know that on back of my head but I probably got confused by what was in a old piece of code of mine!

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.