Hello,
I am trying to implement the cook torrance lighting model. I use the GGX Trowbridge-Reitz function as D, the Schlick approximation as F, and Smith for G.
I am following the equations from this : http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
Looking at the result of the functions alone, it seems that D is working :
My code :
float G_Schlick( vec3 normal, vec3 v, float k )
{
float ndotv = abs( dot( normal, v ) );
float den = ndotv * ( 1.0 - k ) + k;
return ndotv / den;
}
float Geometric_Smith( vec3 normal, vec3 lightDir, vec3 viewDir, float roughness )
{
float k = ( roughness + 1 ) * ( roughness + 1 ) / 8;
//float k = roughness * sqrt( 2 / pi );
return G_Schlick( normal, lightDir, k ) * G_Schlick( normal, viewDir, k );
}
void main()
{
float error = 0.01;
float roughness = clamp( material.roughness - error, 0.0, 1.0 ) + error;
vec3 normal = normalize( Normal );
vec3 viewDir = normalize( cameraPos - Position );
vec3 lightDir = normalize( lightPos - Position );
vec3 half = normalize( viewDir + lightDir );
float D = GGX_TrowbridgeReitz( normalize( Normal ), half, roughness );
float G = Geometric_Smith( normal, lightDir, viewDir, roughness );
float ndotl = max( dot( normal, lightDir ), 0.0 );
float ndotv = max( dot( normal, viewDir ), 0.0 );
// i dont know how to visualize G, so I tried 3 things
vec3 finalColor = vec3( G );
vec3 finalColor = vec3( G / ( 4.0 * ndotl * ndotv ) );
vec3 finalColor = vec3( G / ( ndotl * ndotv ) );
Color = vec4( finalColor, 1.0 );
}
I tried with the normal and the half vector as "normal" parameter, and it seems that normal gives the best results, but some resources says that the half vector should be used.
Results with roughness = 0 :
And with roughness = 1.0 :
( The light source is just on top of the sphere )
What am I doing wrong? According to this image, the results seems to be very different :
Thanks.