importance_sampling_specular.frag 956 B

12345678910111213141516171819202122232425262728293031323334
  1. uniform samplerCube tex;
  2. uniform samplerBuffer samples;
  3. uniform float ViewportSize;
  4. uniform mat4 PermutationMatrix;
  5. out vec4 FragColor;
  6. void main(void)
  7. {
  8. vec2 uv = gl_FragCoord.xy / ViewportSize;
  9. vec3 RayDir = 2. * vec3(uv, 1.) - 1.;
  10. RayDir = normalize((PermutationMatrix * vec4(RayDir, 0.)).xyz);
  11. vec4 FinalColor = vec4(0.);
  12. vec3 up = (RayDir.y < .99) ? vec3(0., 1., 0.) : vec3(0., 0., 1.);
  13. vec3 Tangent = normalize(cross(up, RayDir));
  14. vec3 Bitangent = cross(RayDir, Tangent);
  15. float weight = 0.;
  16. for (int i = 0; i < 1024; i++)
  17. {
  18. float Theta = texelFetch(samples, i).r;
  19. float Phi = texelFetch(samples, i).g;
  20. vec3 L = cos(Theta) * RayDir + sin(Theta) * cos(Phi) * Tangent + sin(Theta) * sin(Phi) * Bitangent;
  21. float NdotL = clamp(dot(RayDir, L), 0., 1.);
  22. FinalColor += textureLod(tex, L, 0.) * NdotL;
  23. weight += NdotL;
  24. }
  25. FragColor = FinalColor / weight;
  26. }