]> git.tdb.fi Git - libs/gl.git/blobdiff - source/materials/pbrmaterial.cpp
Implement ambient lighting in the Cook-Torrance shader
[libs/gl.git] / source / materials / pbrmaterial.cpp
index 92801204ee0ddb7764e543c271a916363591741a..783a8bed2c37eab456e2283e5274d97394fc2075 100644 (file)
@@ -1,4 +1,9 @@
+#include "framebuffer.h"
+#include "mesh.h"
 #include "pbrmaterial.h"
+#include "renderer.h"
+#include "resources.h"
+#include "texture2d.h"
 
 using namespace std;
 
@@ -13,11 +18,12 @@ const Tag PbrMaterial::texture_tags[] =
        Tag("roughness_map"),
        Tag("occlusion_map"),
        Tag("emission_map"),
+       Tag("fresnel_lookup"),
        Tag()
 };
 
 PbrMaterial::PbrMaterial():
-       receive_shadows(false)
+       fresnel_lookup(get_or_create_fresnel_lookup())
 {
        set_base_color(0.8f);
        set_metalness(0.0f);
@@ -25,6 +31,34 @@ PbrMaterial::PbrMaterial():
        set_emission(0.0f);
 }
 
+const Texture2D &PbrMaterial::get_or_create_fresnel_lookup()
+{
+       Resources &resources = Resources::get_global();
+
+       static const string name = "_pbr_env_fresnel_lookup.tex2d";
+       Texture2D *fresnel_lookup = resources.find<Texture2D>(name);
+       if(fresnel_lookup)
+               return *fresnel_lookup;
+
+       fresnel_lookup = new Texture2D;
+       fresnel_lookup->storage(RG8, 128, 128, 1);
+       resources.add(name, fresnel_lookup);
+
+       const Program &shprog = resources.get<Program>("_pbr_fresnel_lookup.glsl.shader");
+       ProgramData shdata;
+       shdata.uniform("n_samples", 1024);
+
+       const Mesh &mesh = resources.get<Mesh>("_fullscreen_quad.mesh");
+       Framebuffer fresnel_lookup_fbo;
+       fresnel_lookup_fbo.attach(COLOR_ATTACHMENT0, *fresnel_lookup);
+       Bind bind_fbo(fresnel_lookup_fbo);
+       Renderer renderer;
+       renderer.set_shader_program(&shprog, &shdata);
+       mesh.draw(renderer);
+
+       return *fresnel_lookup;
+}
+
 void PbrMaterial::fill_program_info(string &module_name, map<string, int> &spec_values) const
 {
        module_name = "cooktorrance.glsl";
@@ -36,7 +70,6 @@ void PbrMaterial::fill_program_info(string &module_name, map<string, int> &spec_
        bool use_emission = (emission.texture || emission.value.r || emission.value.g || emission.value.b);
        spec_values["use_emission"] = use_emission;
        spec_values["use_emission_map"] = (emission.texture!=0);
-       spec_values["use_shadow_map"] = receive_shadows;
 }
 
 #pragma GCC diagnostic push
@@ -66,6 +99,8 @@ const Texture *PbrMaterial::get_texture(Tag tag) const
                return occlusion.texture;
        else if(tag==texture_tags[5])
                return emission.texture;
+       else if(tag==texture_tags[6])
+               return &fresnel_lookup;
        else
                return 0;
 }
@@ -124,11 +159,6 @@ void PbrMaterial::set_emission_map(const Texture *tex)
        emission.texture = tex;
 }
 
-void PbrMaterial::set_receive_shadows(bool s)
-{
-       receive_shadows = s;
-}
-
 
 DataFile::Loader::ActionMap PbrMaterial::Loader::shared_actions;
 
@@ -153,7 +183,6 @@ void PbrMaterial::Loader::init_actions()
        add_property("roughness", &PbrMaterial::set_roughness, &PbrMaterial::set_roughness_map);
        add_property("occlusion", &PbrMaterial::set_occlusion_map);
        add_property("emission", &PbrMaterial::set_emission, &PbrMaterial::set_emission_map, false);
-       add("receive_shadows", &PbrMaterial::receive_shadows);
 }
 
 } // namespace GL