]> git.tdb.fi Git - libs/gl.git/blobdiff - builtin_data/_envmap_specular.glsl
Implement image-based lighting in PbrMaterial
[libs/gl.git] / builtin_data / _envmap_specular.glsl
diff --git a/builtin_data/_envmap_specular.glsl b/builtin_data/_envmap_specular.glsl
new file mode 100644 (file)
index 0000000..d39cc61
--- /dev/null
@@ -0,0 +1,29 @@
+import cubemap_effect;
+import _pbr_prefilter;
+
+#pragma MSP stage(fragment)
+layout(location=0) out vec3 frag_color;
+void main()
+{
+       vec3 normal = normalize(texcoord);
+       vec3 tangent = normalize(abs(normal.x)>abs(normal.y) ? vec3(-normal.z, 0.0, normal.x) : vec3(0.0, -normal.z, normal.y));
+       mat3 orientation = mat3(tangent, cross(normal, tangent), normal);
+
+       vec3 sum = vec3(0.0);
+       float weight = 0.0;
+       for(int i=0; i<n_samples; ++i)
+       {
+               vec3 halfway = orientation*ndist_ggxtr_importance_sample(hammersley(i, n_samples), roughness);
+               vec3 light_dir = reflect(-normal, halfway);
+
+               float n_dot_light = dot(normal, light_dir);
+
+               if(n_dot_light>0)
+               {
+                       sum += textureLod(environment_map, light_dir, 0).rgb*n_dot_light;
+                       weight += n_dot_light;
+               }
+       }
+
+       frag_color = sum/weight;
+}