#version 330 core in vert_t { vec3 position; } vert; out vec4 frag_color; uniform samplerCube cubemap; const float PI = 3.14159265359; void main(void) { vec3 normal = normalize(vert.position); vec3 up = vec3(0.0, 1.0, 0.0); vec3 right = cross(up, normal); up = cross(normal, right); vec3 irradiance = vec3(0.0); float delta = 0.025; int nsamples = 0; for (float phi = 0.0; phi < 2.0 * PI; phi += delta) { for (float theta = 0.0; theta < 0.5 * PI; theta += delta) { vec3 tangent = vec3(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta)); vec3 v = tangent.x * right + tangent.y * up + tangent.z * normal; irradiance += texture(cubemap, v).rgb * cos(theta) * sin(theta); nsamples++; } } irradiance *= PI / nsamples; frag_color = vec4(irradiance, 1.0); }