You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.6 KiB

  1. /*******************************************************************************************
  2. *
  3. * rPBR [shader] - Irradiance cubemap fragment shader
  4. *
  5. * Copyright (c) 2017 Victor Fisac
  6. *
  7. **********************************************************************************************/
  8. #version 330
  9. // Input vertex attributes (from vertex shader)
  10. in vec3 fragPos;
  11. // Input uniform values
  12. uniform samplerCube environmentMap;
  13. // Constant values
  14. const float PI = 3.14159265359f;
  15. // Output fragment color
  16. out vec4 finalColor;
  17. void main()
  18. {
  19. // The sample direction equals the hemisphere's orientation
  20. vec3 normal = normalize(fragPos);
  21. vec3 irradiance = vec3(0.0);
  22. vec3 up = vec3(0.0, 1.0, 0.0);
  23. vec3 right = cross(up, normal);
  24. up = cross(normal, right);
  25. float sampleDelta = 0.025f;
  26. float nrSamples = 0.0f;
  27. for (float phi = 0.0; phi < 2.0*PI; phi += sampleDelta)
  28. {
  29. for (float theta = 0.0; theta < 0.5*PI; theta += sampleDelta)
  30. {
  31. // Spherical to cartesian (in tangent space)
  32. vec3 tangentSample = vec3(sin(theta)*cos(phi), sin(theta)*sin(phi), cos(theta));
  33. // tangent space to world
  34. vec3 sampleVec = tangentSample.x*right + tangentSample.y*up + tangentSample.z*normal;
  35. // Fetch color from environment cubemap
  36. irradiance += texture(environmentMap, sampleVec).rgb*cos(theta)*sin(theta);
  37. nrSamples++;
  38. }
  39. }
  40. // Calculate irradiance average value from samples
  41. irradiance = PI*irradiance*(1.0/float(nrSamples));
  42. // Calculate final fragment color
  43. finalColor = vec4(irradiance, 1.0);
  44. }