X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fmaterials%2Fpbrmaterial.cpp;h=7e7bd5cf642dbf5e8c4f93284cea5e1173b47bf4;hb=6b9338845dfee441cd18ad6c633e4feef8ad14e1;hp=16c287b4e40f6ad97987ac517ba57315646dc711;hpb=842c817bb679a5a0abc05e8149e2e6e0ae1a0412;p=libs%2Fgl.git diff --git a/source/materials/pbrmaterial.cpp b/source/materials/pbrmaterial.cpp index 16c287b4..7e7bd5cf 100644 --- a/source/materials/pbrmaterial.cpp +++ b/source/materials/pbrmaterial.cpp @@ -1,12 +1,29 @@ +#include "framebuffer.h" +#include "mesh.h" #include "pbrmaterial.h" +#include "renderer.h" +#include "resources.h" +#include "texture2d.h" using namespace std; namespace Msp { namespace GL { +const Tag PbrMaterial::texture_tags[] = +{ + Tag("base_color_map"), + Tag("normal_map"), + Tag("metalness_map"), + 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); @@ -14,6 +31,36 @@ 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(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("_pbr_fresnel_lookup.glsl.shader"); + ProgramData shdata; + shdata.uniform("n_samples", 1024); + // Not actually used here, but put it in to satisfy the interface + shdata.uniform("roughness", 0.0f); + + const Mesh &mesh = resources.get("_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 &spec_values) const { module_name = "cooktorrance.glsl"; @@ -25,9 +72,10 @@ void PbrMaterial::fill_program_info(string &module_name, map &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 +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" void PbrMaterial::attach_textures_to(Texturing &texturing, ProgramData &tex_shdata) const { attach_texture_to(base_color.texture, texturing, tex_shdata, "base_color_map"); @@ -37,6 +85,27 @@ void PbrMaterial::attach_textures_to(Texturing &texturing, ProgramData &tex_shda attach_texture_to(occlusion.texture, texturing, tex_shdata, "occlusion_map"); attach_texture_to(emission.texture, texturing, tex_shdata, "emission_map"); } +#pragma GCC diagnostic pop + +const Texture *PbrMaterial::get_texture(Tag tag) const +{ + if(tag==texture_tags[0]) + return base_color.texture; + else if(tag==texture_tags[1]) + return normal.texture; + else if(tag==texture_tags[2]) + return metalness.texture; + else if(tag==texture_tags[3]) + return roughness.texture; + else if(tag==texture_tags[4]) + return occlusion.texture; + else if(tag==texture_tags[5]) + return emission.texture; + else if(tag==texture_tags[6]) + return &fresnel_lookup; + else + return 0; +} void PbrMaterial::set_base_color(const Color &color) { @@ -92,11 +161,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; @@ -121,7 +185,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