2016-06-28 62 views
2

我正在'Moving Frostbite to PBR course notes'在OpenGL的渲染引擎中實現IBL,但是我在預積分等式的鏡面反射部分時遇到了一些麻煩。IBL探針BRDF預集成文物

正如您將從我的下一張圖像中看到的那樣,該問題在預過濾重要性採樣的立方體貼圖結果的mipmap中可見。

這是X正面MIP鏈: enter image description here

這是負X面MIP鏈: enter image description here

這是我使用環境地圖(它是動態創建的基於物理的天空紋理在HDR格式): enter image description here

這是環境地圖的正X面MIP鏈: enter image description here

正如您所看到的,在正面X面的mips的右側看起來像採樣方向類似於mip左側的採樣方向。

另外,在正面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); 
} 

回答

0

實測出鄰近立方體面的邊緣是由於立方體貼圖接縫的問題。啓用GL_TEXTURE_CUBE_MAP_SEAMLESS解決了該問題。我沒有注意到這一點,因爲我使用NSight來調試着色器,但是NSight不支持glTexParameteriGL_TEXTURE_CUBE_MAP_SEAMLESS,所以我總是禁用它來調試着色器,但是我發現NSight接受glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS)

雖然一個問題仍然存在,但mips非常嘈雜,並且還有大量樣品。用1024個樣本我得到這個:

enter image description here