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