我正在使用“Moving Frostbite to PBR course notes”在OpenGL中的渲染引擎中实现IBL,但在对等式的镜面反射分量进行预集成时遇到了一些麻烦。
正如您从我的下一张图像中看到的那样,该问题在预先过滤的重要性采样的多维数据集结果的mipmap中可见。
这是正X面Mip链:
这是X面负Mip链:
这是我使用的环境贴图(它是基于HDR格式的基于物理的天空纹理动态创建的):
这是环境贴图的正X面Mip链:
如您所见,在正X面的尖头的右侧,似乎采样方向与尖头的左侧的采样方向相似。
另外,在正X面的第二个Mip上有那些“点状”形状,我认为这可能是由于样本量少所致?
这是我用于预先集成镜面反射IBL的代码:
float radicalInverse_VdC(uint bits)
{
bits = (bits << 16u) | (bits >> 16u);
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
return float(bits) * 2.3283064365386963e-10; // / 0x100000000
}
//
// Attributed to:
// http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html
// Holger Dammertz.
//
vec2 Hammersley(uint i, uint N)
{
return vec2(float(i)/float(N), radicalInverse_VdC(i));
}
// Based on GGX example in:
// http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
vec3 importanceSampleGGX(vec2 u, float roughness, vec3 N, vec3 upVector, vec3 tangentX, vec3 tangentY)
{
float a = roughness * roughness;
float phiH = u.x * PI * 2.0f;
float cosThetaH = sqrt((1.0f - u.y) / (1.0f + (a * a - 1.0f) * u.y));
float sinThetaH = sqrt(1.0f - min(1.0f, cosThetaH * cosThetaH));
vec3 H = vec3(sinThetaH * cos(phiH), sinThetaH * sin(phiH), cosThetaH);
H = normalize(tangentX * H.x + tangentY * H.y + N * H.z);
return H;
}
// D(h) for GGX.
// http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html
float D_GGX(float roughness, float NdotH)
{
float a = roughness * roughness;
float a2 = a * a;
float NdotH2 = NdotH * NdotH;
float f = 1.0f + (NdotH2 * (a2 - 1.0f));
return a2 / (f * f);
}
float D_GGX_Divide_Pi(float roughness, float NdotH)
{
return D_GGX(roughness, NdotH) / PI;
}
vec3 ImportanceSample (vec3 N)
{
vec3 V = N;
float size2 = ConvolutionSrcSize * ConvolutionSrcSize;
vec3 upVector = abs(N.z) < 0.999 ? vec3(0.0f, 0.0f, 1.0f) : vec3(1.0f, 0.0f, 0.0f);
vec3 tangentX = normalize(cross(upVector, N));
vec3 tangentY = cross(N, tangentX);
vec3 accBrdf = vec3(0.0f);
float accBrdfWeight = 0.0f;
float roughness = ConvolutionRoughness;
uint samplesCount = uint(ConvolutionSampleCount);
for(uint i = uint(0); i < samplesCount; i++)
{
vec2 eta = Hammersley(i, samplesCount);
vec3 H = importanceSampleGGX(eta, roughness, N, upVector, tangentX, tangentY);
vec3 L = 2.0f * dot(V, H) * H - V;
float NdotL = dot(N, L);
if(NdotL > 0.0f)
{
float NdotH = saturate(dot(N, H));
float LdotH = saturate(dot(L, H));
float pdf = D_GGX_Divide_Pi(roughness, NdotH) * NdotH / (4.0f * LdotH);
float omegaS = 1.0f / (samplesCount * pdf);
float omegaP = 4.0f * PI / (6.0f * size2);
float mipLevel = roughness == 0.0f ? 0.0f : clamp(0.5f * log2(omegaS / omegaP), 0.0f, ConvolutionMipCount);
vec4 Li = textureLod(ConvolutionSrc, L, mipLevel);
accBrdf += Li.rgb * NdotL;
accBrdfWeight += NdotL;
}
}
if(accBrdfWeight > 0.0f)
return accBrdf * (1.0f / accBrdfWeight);
else
return accBrdf;
}
void main()
{
// VertexIn.textureCoord is the normal of a sphere I use as mesh to draw to the IBL cubemap
FragColor = vec4(ImportanceSample(VertexIn.textureCoord), 1.0f);
}
最佳答案
发现这是由于立方体贴面边缘附近的立方体贴图接缝问题所致。启用GL_TEXTURE_CUBE_MAP_SEAMLESS
可解决此问题。我没有注意到这一点,因为我使用NSight调试着色器,但是NSight不支持glTexParameteri
和GL_TEXTURE_CUBE_MAP_SEAMLESS
,因此我始终禁用它来调试着色器,但是我发现NSight接受glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS)
。
但是,仍然存在一个问题,就是杂音非常嘈杂,并且采样数量也很大。使用1024个样本,我得到了: