]> git.tdb.fi Git - libs/gl.git/blob - source/materials/material.cpp
Check the flat qualifier from the correct member
[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 "pbrmaterial.h"
5 #include "program.h"
6 #include "resources.h"
7 #include "splatmaterial.h"
8 #include "unlitmaterial.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 Material::Material()
16 {
17         set_alpha_cutoff(0.0f);
18         set_alpha_feather(1.0f);
19 }
20
21 const Program *Material::create_compatible_shader(const map<string, int> &extra_spec) const
22 {
23         string module_name;
24         map<string, int> spec_values;
25         if(alpha_cutoff>0.0f)
26                 spec_values["use_alpha_cutoff"] = true;
27
28         fill_program_info(module_name, spec_values);
29
30         for(const auto &kvp: extra_spec)
31                 spec_values[kvp.first] = kvp.second;
32
33         uint64_t info_hash = hash<64>(module_name);
34         for(const auto &kvp: spec_values)
35         {
36                 info_hash = hash_update<64>(info_hash, kvp.first);
37                 info_hash = hash_update<64>(info_hash, kvp.second);
38         }
39
40         Resources &res = Resources::get_global();
41         string name = format("_material_%016x.shader", info_hash);
42         Program *shprog = res.find<Program>(name);
43         if(shprog)
44                 return shprog;
45
46         const Module &module = res.get<Module>(module_name);
47         shprog = new Program(module, spec_values);
48         try
49         {
50                 res.add(name, shprog);
51         }
52         catch(...)
53         {
54                 delete shprog;
55                 throw;
56         }
57
58         return shprog;
59 }
60
61 void Material::set_alpha_cutoff(float a)
62 {
63         alpha_cutoff = a;
64         shdata.uniform("alpha_cutoff.cutoff", a);
65 }
66
67 void Material::set_alpha_feather(float f)
68 {
69         alpha_feather = f;
70         shdata.uniform("alpha_cutoff.feather", f);
71 }
72
73 void Material::set_debug_name(const string &name)
74 {
75 #ifdef DEBUG
76         shdata.set_debug_name(name+" [UBO]");
77 #else
78         (void)name;
79 #endif
80 }
81
82 Material::GenericLoader::TypeRegistry &Material::get_material_registry()
83 {
84         static GenericLoader::TypeRegistry registry;
85         static bool initialized = false;
86         if(!initialized)
87         {
88                 initialized = true;
89                 registry.register_type<BasicMaterial>("basic");
90                 registry.register_type<PbrMaterial>("pbr");
91                 registry.register_type<SplatMaterial>("splat");
92                 registry.register_type<UnlitMaterial>("unlit");
93         }
94         return registry;
95 }
96
97
98 Material::Loader::Loader(Material &m, Collection &c):
99         CollectionObjectLoader(m, &c)
100 { }
101
102 void Material::Loader::init_actions()
103 {
104         add("alpha_cutoff", &Loader::alpha_cutoff);
105         add("alpha_cutoff", &Loader::alpha_cutoff_feather);
106         add("sampler", &Loader::sampler);
107 }
108
109 void Material::Loader::alpha_cutoff(float a)
110 {
111         obj.set_alpha_cutoff(a);
112 }
113
114 void Material::Loader::alpha_cutoff_feather(float a, float f)
115 {
116         obj.set_alpha_cutoff(a);
117         obj.set_alpha_feather(f);
118 }
119
120 void Material::Loader::sampler(const string &name)
121 {
122         obj.sampler = &get_collection().get<Sampler>(name);
123 }
124
125 } // namespace GL
126 } // namespace Msp