]> git.tdb.fi Git - libs/gl.git/blob - builtin_data/_envmap_specular.glsl
Simplify Program by removing transient data
[libs/gl.git] / builtin_data / _envmap_specular.glsl
1 import cubemap_effect;
2 import _pbr_prefilter;
3
4 #pragma MSP stage(fragment)
5 layout(location=0) out vec3 frag_color;
6 void main()
7 {
8         vec3 normal = normalize(texcoord);
9         vec3 tangent = normalize(abs(normal.x)>abs(normal.y) ? vec3(-normal.z, 0.0, normal.x) : vec3(0.0, -normal.z, normal.y));
10         mat3 orientation = mat3(tangent, cross(normal, tangent), normal);
11
12         vec3 sum = vec3(0.0);
13         float weight = 0.0;
14         for(int i=0; i<n_samples; ++i)
15         {
16                 vec3 halfway = orientation*ndist_ggxtr_importance_sample(hammersley(i, n_samples), roughness);
17                 vec3 light_dir = reflect(-normal, halfway);
18
19                 float n_dot_light = dot(normal, light_dir);
20
21                 if(n_dot_light>0)
22                 {
23                         sum += textureLod(environment_map, light_dir, 0).rgb*n_dot_light;
24                         weight += n_dot_light;
25                 }
26         }
27
28         frag_color = sum/weight;
29 }