Skip to content

Instantly share code, notes, and snippets.

@linniksergey
Created August 8, 2015 13:14
Show Gist options
  • Save linniksergey/0439fb5f8360f980d12c to your computer and use it in GitHub Desktop.
Save linniksergey/0439fb5f8360f980d12c to your computer and use it in GitHub Desktop.
#include "math.h"
vector Schlick(float CosT; vector FZero)
{
return (FZero + (1-FZero) * pow(1-CosT, 5));
}
void myCookTorrance(vector nN, FZero, specular; float roughness; string categories, lightmask)
{
vector nI = normalize(-I); //from eye to surface
float r2 = roughness * roughness;
illuminance(P, nN, M_PI, bouncemask("reflect"), "categories", categories, "lightmask", lightmask)
{
//prepare variables
vector nL = normalize(L); //direction to light
vector H = normalize(nI + nL); // half vector
float NdotH = dot(nN, H);
float NdotL = dot(nN, nL);
float IdotH = dot(nI, H);
float NdotI = dot(nN, nI);
//fresnel
vector kr = Schlick(NdotI, FZero);
//Geometry Function
float G = min(1, (2*NdotH*NdotI/IdotH), (2*NdotH*NdotL/IdotH));
//beckman
float roughness_a = 1.0f / ( 4.0f * r2 * pow( NdotH, 4 ) );
float roughness_b = NdotH * NdotH - 1.0f;
float roughness_c = r2 * NdotH * NdotH;
//Distribution Function
float D = roughness_a * exp( roughness_b / roughness_c );
//final result
vector _specular = shadow(Cl) * (kr * D * G) / (M_PI * NdotI);
storelightexport(getlightname(), "specular", _specular);
specular += _specular;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment