1 #include <msp/core/hash.h>
2 #include <msp/strings/format.h>
3 #include "basicmaterial.h"
5 #include "pbrmaterial.h"
15 Program *Material::create_compatible_shader() const
17 return new Program(create_program_source());
20 const Program *Material::create_compatible_shader(DataFile::Collection &coll) const
22 string source = create_program_source();
23 string name = format("_material_%016x.glsl", hash64(source));
24 Program *shprog = coll.find<Program>(name);
28 shprog = new Program(create_program_source());
31 coll.add(name, shprog);
42 void Material::attach_texture_to(const Texture *tex, Texturing &texturing, ProgramData &tex_shdata, const string &name) const
49 if(const Uniform *uni = tex_shdata.find_uniform(name))
50 if(const Uniform1i *uni_int = dynamic_cast<const Uniform1i *>(uni))
51 unit = uni_int->get();
54 unit = texturing.find_free_unit(name);
56 throw runtime_error("no free texunit");
58 texturing.attach(unit, *tex, sampler);
59 tex_shdata.uniform(name, unit);
62 Material::MaterialRegistry &Material::get_material_registry()
64 static MaterialRegistry registry;
65 static bool initialized = false;
69 registry.register_type<BasicMaterial>("basic");
70 registry.register_type<PbrMaterial>("pbr");
76 Material::Loader::Loader(Material &m):
77 CollectionObjectLoader(m, 0)
80 Material::Loader::Loader(Material &m, Collection &c):
81 CollectionObjectLoader(m, &c)
84 void Material::Loader::init_actions()
86 add("sampler", &Loader::sampler);
89 void Material::Loader::sampler(const std::string &name)
91 obj.sampler = &get_collection().get<Sampler>(name);
95 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
97 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
100 set_actions(shared_actions);
103 void Material::GenericLoader::init_actions()
105 get_material_registry().add_all(*this);