1 #include "framebuffer.h"
3 #include "pbrmaterial.h"
13 const Tag PbrMaterial::texture_tags[] =
15 Tag("base_color_map"),
21 Tag("fresnel_lookup"),
25 PbrMaterial::PbrMaterial():
26 fresnel_lookup(get_or_create_fresnel_lookup())
34 const Texture2D &PbrMaterial::get_or_create_fresnel_lookup()
36 Resources &resources = Resources::get_global();
38 static const string name = "_pbr_env_fresnel_lookup.tex2d";
39 Texture2D *fresnel_lookup = resources.find<Texture2D>(name);
41 return *fresnel_lookup;
43 fresnel_lookup = new Texture2D;
44 fresnel_lookup->storage(RG8, 128, 128, 1);
45 resources.add(name, fresnel_lookup);
47 const Program &shprog = resources.get<Program>("_pbr_fresnel_lookup.glsl.shader");
49 shdata.uniform("n_samples", 1024);
51 const Mesh &mesh = resources.get<Mesh>("_fullscreen_quad.mesh");
52 Framebuffer fresnel_lookup_fbo;
53 fresnel_lookup_fbo.attach(COLOR_ATTACHMENT0, *fresnel_lookup);
54 Bind bind_fbo(fresnel_lookup_fbo);
56 renderer.set_shader_program(&shprog, &shdata);
59 return *fresnel_lookup;
62 void PbrMaterial::fill_program_info(string &module_name, map<string, int> &spec_values) const
64 module_name = "cooktorrance.glsl";
65 spec_values["use_base_color_map"] = (base_color.texture!=0);
66 spec_values["use_normal_map"] = (normal.texture!=0);
67 spec_values["use_metalness_map"] = (metalness.texture!=0);
68 spec_values["use_roughness_map"] = (roughness.texture!=0);
69 spec_values["use_occlusion_map"] = (occlusion.texture!=0);
70 bool use_emission = (emission.texture || emission.value.r || emission.value.g || emission.value.b);
71 spec_values["use_emission"] = use_emission;
72 spec_values["use_emission_map"] = (emission.texture!=0);
75 #pragma GCC diagnostic push
76 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
77 void PbrMaterial::attach_textures_to(Texturing &texturing, ProgramData &tex_shdata) const
79 attach_texture_to(base_color.texture, texturing, tex_shdata, "base_color_map");
80 attach_texture_to(metalness.texture, texturing, tex_shdata, "metalness_map");
81 attach_texture_to(roughness.texture, texturing, tex_shdata, "roughness_map");
82 attach_texture_to(normal.texture, texturing, tex_shdata, "normal_map");
83 attach_texture_to(occlusion.texture, texturing, tex_shdata, "occlusion_map");
84 attach_texture_to(emission.texture, texturing, tex_shdata, "emission_map");
86 #pragma GCC diagnostic pop
88 const Texture *PbrMaterial::get_texture(Tag tag) const
90 if(tag==texture_tags[0])
91 return base_color.texture;
92 else if(tag==texture_tags[1])
93 return normal.texture;
94 else if(tag==texture_tags[2])
95 return metalness.texture;
96 else if(tag==texture_tags[3])
97 return roughness.texture;
98 else if(tag==texture_tags[4])
99 return occlusion.texture;
100 else if(tag==texture_tags[5])
101 return emission.texture;
102 else if(tag==texture_tags[6])
103 return &fresnel_lookup;
108 void PbrMaterial::set_base_color(const Color &color)
110 base_color.value = color;
111 shdata.uniform("pbr_material.base_color", color);
114 void PbrMaterial::set_base_color_map(const Texture *tex)
116 base_color.texture = tex;
119 void PbrMaterial::set_normal_map(const Texture *tex)
121 normal.texture = tex;
124 void PbrMaterial::set_metalness(float value)
126 metalness.value = value;
127 shdata.uniform("pbr_material.metalness", value);
130 void PbrMaterial::set_metalness_map(const Texture *tex)
132 metalness.texture = tex;
135 void PbrMaterial::set_roughness(float value)
137 roughness.value = value;
138 shdata.uniform("pbr_material.roughness", value);
141 void PbrMaterial::set_roughness_map(const Texture *tex)
143 roughness.texture = tex;
146 void PbrMaterial::set_occlusion_map(const Texture *tex)
148 occlusion.texture = tex;
151 void PbrMaterial::set_emission(const Color &color)
153 emission.value = color;
154 shdata.uniform("pbr_material.emission", color);
157 void PbrMaterial::set_emission_map(const Texture *tex)
159 emission.texture = tex;
163 DataFile::Loader::ActionMap PbrMaterial::Loader::shared_actions;
165 PbrMaterial::Loader::Loader(PbrMaterial &m):
166 DerivedObjectLoader<PbrMaterial, Material::PropertyLoader<PbrMaterial> >(m)
168 set_actions(shared_actions);
171 PbrMaterial::Loader::Loader(PbrMaterial &m, Collection &c):
172 DerivedObjectLoader<PbrMaterial, Material::PropertyLoader<PbrMaterial> >(m, c)
174 set_actions(shared_actions);
177 void PbrMaterial::Loader::init_actions()
179 Material::PropertyLoader<PbrMaterial>::init_actions();
180 add_property("base_color", &PbrMaterial::set_base_color, &PbrMaterial::set_base_color_map, true);
181 add_property("normal", &PbrMaterial::set_normal_map);
182 add_property("metalness", &PbrMaterial::set_metalness, &PbrMaterial::set_metalness_map);
183 add_property("roughness", &PbrMaterial::set_roughness, &PbrMaterial::set_roughness_map);
184 add_property("occlusion", &PbrMaterial::set_occlusion_map);
185 add_property("emission", &PbrMaterial::set_emission, &PbrMaterial::set_emission_map, false);