X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fmaterials%2Fpbrmaterial.cpp;h=ebd3321f2268a05054c05f086c035e5b88fdb39a;hb=b0db98d71aeb32d9e57315b8a06ff4068aeccbca;hp=7c73996fd2f7b8e91af2ab4eb27d08fd5592de70;hpb=cb7db94f7837e6a3be037d07575dc248177d9426;p=libs%2Fgl.git diff --git a/source/materials/pbrmaterial.cpp b/source/materials/pbrmaterial.cpp index 7c73996f..ebd3321f 100644 --- a/source/materials/pbrmaterial.cpp +++ b/source/materials/pbrmaterial.cpp @@ -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,10 +18,13 @@ const Tag PbrMaterial::texture_tags[] = Tag("roughness_map"), Tag("occlusion_map"), Tag("emission_map"), + Tag("fresnel_lookup"), Tag() }; -PbrMaterial::PbrMaterial() +PbrMaterial::PbrMaterial(): + fresnel_lookup(get_or_create_fresnel_lookup()), + fresnel_sampler(Resources::get_global().get("_linear_clamp.samp")) { set_base_color(0.8f); set_metalness(0.0f); @@ -24,6 +32,38 @@ PbrMaterial::PbrMaterial() set_emission(0.0f); } +const Texture2D &PbrMaterial::get_or_create_fresnel_lookup() +{ + Resources &resources = Resources::get_global(); + + static const string name = "_pbr_fresnel_lookup.tex"; + 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((COLOR_ATTACHMENT,RG8)); + fresnel_lookup_fbo.attach(COLOR_ATTACHMENT, *fresnel_lookup); + Renderer renderer; + renderer.begin(); + renderer.set_framebuffer(&fresnel_lookup_fbo); + renderer.set_shader_program(&shprog, &shdata); + mesh.draw(renderer); + renderer.end(); + + return *fresnel_lookup; +} + void PbrMaterial::fill_program_info(string &module_name, map &spec_values) const { module_name = "cooktorrance.glsl"; @@ -37,19 +77,6 @@ void PbrMaterial::fill_program_info(string &module_name, map &spec_ spec_values["use_emission_map"] = (emission.texture!=0); } -#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"); - attach_texture_to(metalness.texture, texturing, tex_shdata, "metalness_map"); - attach_texture_to(roughness.texture, texturing, tex_shdata, "roughness_map"); - attach_texture_to(normal.texture, texturing, tex_shdata, "normal_map"); - 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]) @@ -64,10 +91,20 @@ 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; } +const Sampler *PbrMaterial::get_sampler(Tag tag) const +{ + if(tag==texture_tags[6]) + return &fresnel_sampler; + else + return sampler; +} + void PbrMaterial::set_base_color(const Color &color) { base_color.value = color; @@ -125,12 +162,6 @@ void PbrMaterial::set_emission_map(const Texture *tex) DataFile::Loader::ActionMap PbrMaterial::Loader::shared_actions; -PbrMaterial::Loader::Loader(PbrMaterial &m): - DerivedObjectLoader >(m) -{ - set_actions(shared_actions); -} - PbrMaterial::Loader::Loader(PbrMaterial &m, Collection &c): DerivedObjectLoader >(m, c) {