1 #include <msp/core/hash.h>
2 #include <msp/strings/format.h>
3 #include "basicmaterial.h"
5 #include "pbrmaterial.h"
9 #include "unlitmaterial.h"
16 Program *Material::create_compatible_shader() const
18 return new Program(create_program_source());
21 const Program *Material::create_compatible_shader(DataFile::Collection &coll) const
23 string source = create_program_source();
24 string name = format("_material_%016x.glsl", hash64(source));
25 Program *shprog = coll.find<Program>(name);
29 shprog = new Program(create_program_source());
32 coll.add(name, shprog);
43 void Material::attach_texture_to(const Texture *tex, Texturing &texturing, ProgramData &tex_shdata, const string &name) const
50 if(const Uniform *uni = tex_shdata.find_uniform(name))
51 if(const Uniform1i *uni_int = dynamic_cast<const Uniform1i *>(uni))
52 unit = uni_int->get();
55 unit = texturing.find_free_unit(name);
57 throw runtime_error("no free texunit");
59 texturing.attach(unit, *tex, sampler);
60 tex_shdata.uniform(name, unit);
63 Material::MaterialRegistry &Material::get_material_registry()
65 static MaterialRegistry registry;
66 static bool initialized = false;
70 registry.register_type<BasicMaterial>("basic");
71 registry.register_type<PbrMaterial>("pbr");
72 registry.register_type<UnlitMaterial>("unlit");
78 Material::Loader::Loader(Material &m):
79 CollectionObjectLoader(m, 0)
82 Material::Loader::Loader(Material &m, Collection &c):
83 CollectionObjectLoader(m, &c)
86 void Material::Loader::init_actions()
88 add("sampler", &Loader::sampler);
91 void Material::Loader::sampler(const std::string &name)
93 obj.sampler = &get_collection().get<Sampler>(name);
97 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
99 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
102 set_actions(shared_actions);
105 void Material::GenericLoader::init_actions()
107 get_material_registry().add_all(*this);