How to do it...

We'll define a function for each of the three terms in the specular BRDF. Use the following steps:

  1. Define a function for the Fresnel term using the Schlick approximation:
vec3 schlickFresnel( float lDotH ) {  vec3 f0 = vec3(0.04);  // Dielectrics  if( Material.Metal ) {    f0 = Material.Color;  }  return f0 + (1 - f0) * pow(1.0 - lDotH, 5);}
  1. Define a function for the geometry term G:  
float geomSmith( float dotProd ) {  float k = (Material.Rough + 1.0) * (Material.Rough + 1.0) / 8.0;  float denom = dotProd * (1 - k) + k;  return 1.0 / denom;}
  1. The normal distribution function D, based on GGX/Trowbridge-Reitz:
float ggxDistribution( float nDotH ) { float alpha2 = Material.Rough * Material.Rough * Material.Rough * Material.Rough; ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.