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