No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

50 líneas
1.4 KiB

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