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.