]> git.tdb.fi Git - libs/gl.git/blob - source/materials/material.cpp
Add push_back_nocopy to NodeContainer for clarity
[libs/gl.git] / source / materials / material.cpp
1 #include <msp/core/hash.h>
2 #include <msp/strings/format.h>
3 #include "basicmaterial.h"
4 #include "gl.h"
5 #include "pbrmaterial.h"
6 #include "resources.h"
7 #include "texturing.h"
8 #include "uniform.h"
9 #include "unlitmaterial.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 const Program *Material::create_compatible_shader(DataFile::Collection &coll) const
17 {
18         string module_name;
19         map<string, int> spec_values;
20         fill_program_info(module_name, spec_values);
21
22         string info = module_name;
23         for(map<string, int>::const_iterator i=spec_values.begin(); i!=spec_values.end(); ++i)
24                 info += format(",%s:%d", i->first, i->second);
25
26         string name = format("_material_%016x.shader", hash64(info));
27         Program *shprog = coll.find<Program>(name);
28         if(shprog)
29                 return shprog;
30
31         const Module &module = coll.get<Module>(module_name);
32         shprog = new Program(module, spec_values);
33         try
34         {
35                 coll.add(name, shprog);
36         }
37         catch(...)
38         {
39                 delete shprog;
40                 throw;
41         }
42
43         return shprog;
44 }
45
46 void Material::attach_texture_to(const Texture *tex, Texturing &texturing, ProgramData &tex_shdata, const string &name) const
47 {
48         if(!tex)
49                 return;
50
51         int unit = -1;
52
53         if(const Uniform *uni = tex_shdata.find_uniform(name))
54                 if(const Uniform1i *uni_int = dynamic_cast<const Uniform1i *>(uni))
55                         unit = uni_int->get();
56
57         if(unit<0)
58                 unit = texturing.find_free_unit(name);
59         if(unit<0)
60                 throw runtime_error("no free texunit");
61
62         texturing.attach(unit, *tex, sampler);
63         tex_shdata.uniform(name, unit);
64 }
65
66 Material::MaterialRegistry &Material::get_material_registry()
67 {
68         static MaterialRegistry registry;
69         static bool initialized = false;
70         if(!initialized)
71         {
72                 initialized = true;
73                 registry.register_type<BasicMaterial>("basic");
74                 registry.register_type<PbrMaterial>("pbr");
75                 registry.register_type<UnlitMaterial>("unlit");
76         }
77         return registry;
78 }
79
80
81 Material::Loader::Loader(Material &m):
82         CollectionObjectLoader(m, 0)
83 { }
84
85 Material::Loader::Loader(Material &m, Collection &c):
86         CollectionObjectLoader(m, &c)
87 { }
88
89 void Material::Loader::init_actions()
90 {
91         add("sampler", &Loader::sampler);
92 }
93
94 void Material::Loader::sampler(const std::string &name)
95 {
96         obj.sampler = &get_collection().get<Sampler>(name);
97 }
98
99
100 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
101
102 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
103         coll(c)
104 {
105         set_actions(shared_actions);
106 }
107
108 void Material::GenericLoader::init_actions()
109 {
110         get_material_registry().add_all(*this);
111 }
112
113 } // namespace GL
114 } // namespace Msp