Skip to content

Instantly share code, notes, and snippets.

@skitaoka
Forked from jdupuy/SampleVndf_GGX.cpp
Created July 30, 2023 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skitaoka/a14c477c49259ddee6fc0360d8b37d8a to your computer and use it in GitHub Desktop.
Save skitaoka/a14c477c49259ddee6fc0360d8b37d8a to your computer and use it in GitHub Desktop.
Sampling Visible GGX Normals with Spherical Caps
// Helper function: sample the visible hemisphere from a spherical cap
vec3 SampleVndf_Hemisphere(vec2 u, vec3 wi)
{
// sample a spherical cap in (-wi.z, 1]
float phi = 2.0f * M_PI * u.x;
float z = fma((1.0f - u.y), (1.0f + wi.z), -wi.z);
float sinTheta = sqrt(clamp(1.0f - z * z, 0.0f, 1.0f));
float x = sinTheta * cos(phi);
float y = sinTheta * sin(phi);
vec3 c = vec3(x, y, z);
// compute halfway direction;
vec3 h = c + wi;
// return without normalization as this is done later (see line 25)
return h;
}
// Sample the GGX VNDF
vec3 SampleVndf_GGX(vec2 u, vec3 wi, vec2 alpha)
{
// warp to the hemisphere configuration
vec3 wiStd = normalize(vec3(wi.xy * alpha, wi.z));
// sample the hemisphere
vec3 wmStd = SampleVndf_Hemisphere(u, wiStd);
// warp back to the ellipsoid configuration
vec3 wm = normalize(vec3(wmStd.xy * alpha, wmStd.z));
// return final normal
return wm;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment