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_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);
50 // Not actually used here, but put it in to satisfy the interface
51 shdata.uniform("roughness", 0.0f);
53 const Mesh &mesh = resources.get<Mesh>("_fullscreen_quad.mesh");
54 Framebuffer fresnel_lookup_fbo;
55 fresnel_lookup_fbo.attach(COLOR_ATTACHMENT0, *fresnel_lookup);
56 Bind bind_fbo(fresnel_lookup_fbo);
58 renderer.set_shader_program(&shprog, &shdata);
61 return *fresnel_lookup;
64 void PbrMaterial::fill_program_info(string &module_name, map<string, int> &spec_values) const
66 module_name = "cooktorrance.glsl";
67 spec_values["use_base_color_map"] = (base_color.texture!=0);
68 spec_values["use_normal_map"] = (normal.texture!=0);
69 spec_values["use_metalness_map"] = (metalness.texture!=0);
70 spec_values["use_roughness_map"] = (roughness.texture!=0);
71 spec_values["use_occlusion_map"] = (occlusion.texture!=0);
72 bool use_emission = (emission.texture || emission.value.r || emission.value.g || emission.value.b);
73 spec_values["use_emission"] = use_emission;
74 spec_values["use_emission_map"] = (emission.texture!=0);
77 const Texture *PbrMaterial::get_texture(Tag tag) const
79 if(tag==texture_tags[0])
80 return base_color.texture;
81 else if(tag==texture_tags[1])
82 return normal.texture;
83 else if(tag==texture_tags[2])
84 return metalness.texture;
85 else if(tag==texture_tags[3])
86 return roughness.texture;
87 else if(tag==texture_tags[4])
88 return occlusion.texture;
89 else if(tag==texture_tags[5])
90 return emission.texture;
91 else if(tag==texture_tags[6])
92 return &fresnel_lookup;
97 void PbrMaterial::set_base_color(const Color &color)
99 base_color.value = color;
100 shdata.uniform("pbr_material.base_color", color);
103 void PbrMaterial::set_base_color_map(const Texture *tex)
105 base_color.texture = tex;
108 void PbrMaterial::set_normal_map(const Texture *tex)
110 normal.texture = tex;
113 void PbrMaterial::set_metalness(float value)
115 metalness.value = value;
116 shdata.uniform("pbr_material.metalness", value);
119 void PbrMaterial::set_metalness_map(const Texture *tex)
121 metalness.texture = tex;
124 void PbrMaterial::set_roughness(float value)
126 roughness.value = value;
127 shdata.uniform("pbr_material.roughness", value);
130 void PbrMaterial::set_roughness_map(const Texture *tex)
132 roughness.texture = tex;
135 void PbrMaterial::set_occlusion_map(const Texture *tex)
137 occlusion.texture = tex;
140 void PbrMaterial::set_emission(const Color &color)
142 emission.value = color;
143 shdata.uniform("pbr_material.emission", color);
146 void PbrMaterial::set_emission_map(const Texture *tex)
148 emission.texture = tex;
152 DataFile::Loader::ActionMap PbrMaterial::Loader::shared_actions;
154 PbrMaterial::Loader::Loader(PbrMaterial &m):
155 DerivedObjectLoader<PbrMaterial, Material::PropertyLoader<PbrMaterial> >(m)
157 set_actions(shared_actions);
160 PbrMaterial::Loader::Loader(PbrMaterial &m, Collection &c):
161 DerivedObjectLoader<PbrMaterial, Material::PropertyLoader<PbrMaterial> >(m, c)
163 set_actions(shared_actions);
166 void PbrMaterial::Loader::init_actions()
168 Material::PropertyLoader<PbrMaterial>::init_actions();
169 add_property("base_color", &PbrMaterial::set_base_color, &PbrMaterial::set_base_color_map, true);
170 add_property("normal", &PbrMaterial::set_normal_map);
171 add_property("metalness", &PbrMaterial::set_metalness, &PbrMaterial::set_metalness_map);
172 add_property("roughness", &PbrMaterial::set_roughness, &PbrMaterial::set_roughness_map);
173 add_property("occlusion", &PbrMaterial::set_occlusion_map);
174 add_property("emission", &PbrMaterial::set_emission, &PbrMaterial::set_emission_map, false);