]> git.tdb.fi Git - libs/gl.git/blob - source/materials/material.cpp
Implement an unlit material type
[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 Program *Material::create_compatible_shader() const
17 {
18         return new Program(create_program_source());
19 }
20
21 const Program *Material::create_compatible_shader(DataFile::Collection &coll) const
22 {
23         string source = create_program_source();
24         string name = format("_material_%016x.glsl", hash64(source));
25         Program *shprog = coll.find<Program>(name);
26         if(shprog)
27                 return shprog;
28
29         shprog = new Program(create_program_source());
30         try
31         {
32                 coll.add(name, shprog);
33         }
34         catch(...)
35         {
36                 delete shprog;
37                 throw;
38         }
39
40         return shprog;
41 }
42
43 void Material::attach_texture_to(const Texture *tex, Texturing &texturing, ProgramData &tex_shdata, const string &name) const
44 {
45         if(!tex)
46                 return;
47
48         int unit = -1;
49
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();
53
54         if(unit<0)
55                 unit = texturing.find_free_unit(name);
56         if(unit<0)
57                 throw runtime_error("no free texunit");
58
59         texturing.attach(unit, *tex, sampler);
60         tex_shdata.uniform(name, unit);
61 }
62
63 Material::MaterialRegistry &Material::get_material_registry()
64 {
65         static MaterialRegistry registry;
66         static bool initialized = false;
67         if(!initialized)
68         {
69                 initialized = true;
70                 registry.register_type<BasicMaterial>("basic");
71                 registry.register_type<PbrMaterial>("pbr");
72                 registry.register_type<UnlitMaterial>("unlit");
73         }
74         return registry;
75 }
76
77
78 Material::Loader::Loader(Material &m):
79         CollectionObjectLoader(m, 0)
80 { }
81
82 Material::Loader::Loader(Material &m, Collection &c):
83         CollectionObjectLoader(m, &c)
84 { }
85
86 void Material::Loader::init_actions()
87 {
88         add("sampler", &Loader::sampler);
89 }
90
91 void Material::Loader::sampler(const std::string &name)
92 {
93         obj.sampler = &get_collection().get<Sampler>(name);
94 }
95
96
97 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
98
99 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
100         coll(c)
101 {
102         set_actions(shared_actions);
103 }
104
105 void Material::GenericLoader::init_actions()
106 {
107         get_material_registry().add_all(*this);
108 }
109
110 } // namespace GL
111 } // namespace Msp