summaryrefslogtreecommitdiff
path: root/pbr/2.1.2.ibl_irradiance/convolution.frag
blob: 69ce8af702fe0b7ad5e14c65baff7e81309474e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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);
}