aboutsummaryrefslogtreecommitdiff
path: root/shaders/irradiance_map.frag
diff options
context:
space:
mode:
Diffstat (limited to 'shaders/irradiance_map.frag')
-rw-r--r--shaders/irradiance_map.frag65
1 files changed, 65 insertions, 0 deletions
diff --git a/shaders/irradiance_map.frag b/shaders/irradiance_map.frag
new file mode 100644
index 0000000..8200e73
--- /dev/null
+++ b/shaders/irradiance_map.frag
@@ -0,0 +1,65 @@
1precision highp float;
2
3#define PI 3.1415926535897932384626433832795
4#define EPS 0.001
5#define NUM_SAMPLES_AZIMUTH 250
6#define NUM_SAMPLES_ZENITH 50
7#define MAX_AZIMUTH (2*PI)
8#define MAX_ZENITH (PI/2.0)
9#define AZIMUTH_DELTA (MAX_AZIMUTH / float(NUM_SAMPLES_AZIMUTH))
10#define ZENITH_DELTA (MAX_ZENITH / float(NUM_SAMPLES_ZENITH))
11
12uniform samplerCube Sky;
13
14in vec3 Ray;
15
16// DEBUG
17// in vec2 Texcoord;
18
19layout (location = 0) out vec4 Color;
20
21void main()
22{
23 // Tangent space:
24 //
25 // N
26 // |
27 // |
28 // |
29 // |_ _ _ _ _ B
30 // /
31 // /
32 // T
33 vec3 N = normalize(Ray);
34 vec3 B = (abs(N.x) - 1.0 <= EPS) ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
35 vec3 T = normalize(cross(B, N));
36 B = normalize(cross(N, T));
37
38 int num_samples = 0;
39 vec3 irradiance = vec3(0.0);
40 for (float theta = 0.0; theta < MAX_AZIMUTH; theta += AZIMUTH_DELTA) {
41 for (float phi = 0.0; phi < MAX_ZENITH; phi += ZENITH_DELTA) {
42 // Spherical to Cartesian.
43 vec3 sample_tangent_space = vec3(
44 sin(phi) * cos(theta),
45 sin(phi) * sin(theta),
46 cos(phi));
47 // Tangent space to world space.
48 vec3 sample_world_space =
49 sample_tangent_space.x * B +
50 sample_tangent_space.y * T +
51 sample_tangent_space.z * N;
52
53 irradiance += texture(Sky, sample_world_space).rgb * sin(phi) * cos(phi);
54 num_samples += 1;
55 }
56 }
57 irradiance = PI * irradiance / float(num_samples);
58
59 // For debugging in trace.
60 //irradiance = texture(Sky, Ray).rgb;
61 // irradiance = vec3(Texcoord, 0.0);
62 //irradiance = pow(irradiance, vec3(1.0/2.2));
63
64 Color = vec4(irradiance, 1.0);
65}