First pass at blendshapes
[opengl.git] / irradiancefrag.glsl
1 #version 330
2
3 in vec3 localPos;
4 out vec4 fragColor;
5
6 uniform samplerCube environmentMap;
7
8 const float PI = 3.14159265359;
9
10 void main() {
11         // sample dir = hemisphere's orientation
12         vec3 normal = normalize(localPos);
13         vec3 irradiance = vec3(0);
14
15         vec3 up = vec3(0, 1, 0);
16         vec3 right = cross(up, normal);
17         up = cross(normal, right);
18
19         const float sampleDelta = 0.025;
20         float numSamples = 0;
21         for (float phi = 0; phi < 2 * PI; phi += sampleDelta) {
22                 for (float theta = 0; theta < 0.5 * PI; theta += sampleDelta) {
23                         vec3 tangentSample = vec3(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));
24                         // tangent space -> world space
25                         vec3 sampleVec = tangentSample.x * right + tangentSample.y * up + tangentSample.z * normal;
26                         irradiance += texture(environmentMap, sampleVec).rgb * cos(theta) * sin(theta);
27                         numSamples++;
28                 }
29         }
30
31         irradiance = (PI * irradiance) / numSamples;
32
33         fragColor = vec4(irradiance, 1);
34 }