]> git.tdb.fi Git - libs/gl.git/blob - builtin_data/_pbr_fresnel_lookup.glsl
Implement ambient lighting in the Cook-Torrance shader
[libs/gl.git] / builtin_data / _pbr_fresnel_lookup.glsl
1 import _pbr_prefilter;
2
3 #pragma MSP stage(vertex)
4 layout(location=0) in vec4 vertex;
5 void main()
6 {
7         gl_Position = vertex;
8         out vec2 texcoord = vertex.xy*0.5+0.5;
9 }
10
11 #pragma MSP stage(fragment)
12 layout(location=0) out vec2 frag_out;
13 void main()
14 {
15         vec3 normal = vec3(0.0, 0.0, 1.0);
16         vec3 look_dir = vec3(sqrt(1.0-texcoord.y*texcoord.y), 0.0, -texcoord.y);
17         float roughness = texcoord.x;
18
19         float geom_k = roughness*roughness/2.0;
20         float geom_look = -look_dir.z/(geom_k-look_dir.z*(1.0-geom_k));
21
22         vec2 sum = vec2(0.0);
23         for(int i=0; i<n_samples; ++i)
24         {
25                 vec3 halfway = ndist_ggxtr_importance_sample(hammersley(i, n_samples), roughness);
26                 vec3 light_dir = reflect(look_dir, halfway);
27
28                 if(light_dir.z>0)
29                 {
30                         float geom_light = light_dir.z/(geom_k+light_dir.z*(1.0-geom_k));
31                         float geom = geom_look*geom_light;
32
33                         // Look_dir points towards the surface, so the dot product is negated
34                         float half_dot_look = dot(halfway, look_dir);
35                         float ng = geom*half_dot_look/(halfway.z*look_dir.z);
36                         float a = pow(max(1+half_dot_look, 0.0), 5.0);
37
38                         sum += vec2(ng*(1-a), ng*a);
39                 }
40         }
41
42         frag_out = sum/n_samples;
43 }