]> git.tdb.fi Git - libs/gl.git/blob - source/materials/material.h
Migrate from LoadableTypeRegistry to TypeRegistry
[libs/gl.git] / source / materials / material.h
1 #ifndef MSP_GL_MATERIAL_H_
2 #define MSP_GL_MATERIAL_H_
3
4 #include <msp/core/typeregistry.h>
5 #include <msp/datafile/collection.h>
6 #include <msp/datafile/objectloader.h>
7 #include "color.h"
8 #include "programdata.h"
9 #include "texture.h"
10
11 namespace Msp {
12 namespace GL {
13
14 class Texturing;
15
16 class Material
17 {
18 private:
19         class Loader: public DataFile::CollectionObjectLoader<Material>
20         {
21         protected:
22                 Loader(Material &);
23                 Loader(Material &, Collection &);
24
25                 virtual void init_actions();
26
27         private:
28                 void sampler(const std::string &);
29         };
30
31 protected:
32         template<typename T>
33         struct Property
34         {
35                 T value;
36                 const Texture *texture;
37
38                 Property(): value(T()), texture(0) { }
39         };
40
41         template<typename C>
42         class PropertyLoader: public DataFile::DerivedObjectLoader<Material, Loader>
43         {
44         protected:
45                 PropertyLoader(C &m): DerivedObjectLoader<Material, Loader>(m) { }
46                 PropertyLoader(C &m, Collection &c): DerivedObjectLoader<Material, Loader>(m, c) { }
47
48                 void add_property(const std::string &, void (C::*)(float), void (C::*)(const Texture *));
49                 void add_property(const std::string &, void (C::*)(const Color &), void (C::*)(const Texture *), bool);
50                 void add_property(const std::string &, void (C::*)(const Texture *));
51
52                 void property_value_scalar(void (C::*)(float), float);
53                 void property_value_rgb(void (C::*)(const Color &), float, float, float);
54                 void property_value_rgba(void (C::*)(const Color &), float, float, float, float);
55                 void property_value_srgb(void (C::*)(const Color &), float, float, float);
56                 void property_value_srgb_alpha(void (C::*)(const Color &), float, float, float, float);
57                 void property_texture(void (C::*)(const Texture *), const std::string &);
58         };
59
60 public:
61         class GenericLoader: public DataFile::Loader
62         {
63         private:
64                 template<typename T>
65                 struct AddType
66                 {
67                         void operator()(const std::string &kw, GenericLoader &ldr) const { ldr.add(kw, &GenericLoader::typed_material<T>); }
68                 };
69
70                 DataFile::Collection *coll;
71                 RefPtr<Material> material;
72
73                 static ActionMap shared_actions;
74
75         public:
76                 GenericLoader(DataFile::Collection * = 0);
77
78                 Material *get_material() { return material.release(); }
79         private:
80                 virtual void init_actions();
81
82                 template<typename T>
83                 void typed_material();
84
85                 friend class Material;
86         };
87
88 private:
89         typedef TypeRegistry<GenericLoader::AddType, GenericLoader &> MaterialRegistry;
90
91 protected:
92         const Sampler *sampler;
93         ProgramData shdata;
94
95         Material(): sampler(0) { }
96 public:
97         virtual ~Material() { }
98
99         virtual const Program *create_compatible_shader(DataFile::Collection &) const;
100 protected:
101         virtual void fill_program_info(std::string &, std::map<std::string, int> &) const = 0;
102
103 public:
104         /** Returns the uniforms for the material. */
105         const ProgramData &get_shader_data() const { return shdata; }
106
107 protected:
108         DEPRECATED void attach_texture_to(const Texture *, Texturing &, ProgramData &, const std::string &) const;
109 public:
110         DEPRECATED virtual void attach_textures_to(Texturing &, ProgramData &) const = 0;
111
112         virtual const Tag *get_texture_tags() const = 0;
113         virtual const Texture *get_texture(Tag) const = 0;
114         const Sampler *get_sampler() const { return sampler; }
115
116         template<typename T>
117         static void register_type(const std::string &);
118 private:
119         static MaterialRegistry &get_material_registry();
120 };
121
122 template<typename T>
123 void Material::register_type(const std::string &kw)
124 {
125         get_material_registry().register_type<T>(kw);
126 }
127
128
129 template<typename C>
130 void Material::PropertyLoader<C>::add_property(const std::string &kw, void (C::*set_value)(float), void (C::*set_texture)(const Texture *))
131 {
132         add(kw, &PropertyLoader<C>::property_value_scalar, set_value);
133         if(set_texture)
134                 add(kw+"_map", &PropertyLoader<C>::property_texture, set_texture);
135 }
136
137 template<typename C>
138 void Material::PropertyLoader<C>::add_property(const std::string &kw, void (C::*set_value)(const Color &), void (C::*set_texture)(const Texture *), bool allow_alpha)
139 {
140         add(kw, &PropertyLoader<C>::property_value_rgb, set_value);
141         add(kw+"_srgb", &PropertyLoader<C>::property_value_srgb, set_value);
142         if(allow_alpha)
143         {
144                 add(kw, &PropertyLoader<C>::property_value_rgba, set_value);
145                 add(kw+"_srgb", &PropertyLoader<C>::property_value_srgb_alpha, set_value);
146         }
147         if(set_texture)
148                 add(kw+"_map", &PropertyLoader<C>::property_texture, set_texture);
149 }
150
151 template<typename C>
152 void Material::PropertyLoader<C>::add_property(const std::string &kw, void (C::*set_texture)(const Texture *))
153 {
154         add(kw+"_map", &PropertyLoader<C>::property_texture, set_texture);
155 }
156
157 template<typename C>
158 void Material::PropertyLoader<C>::property_value_scalar(void (C::*set_value)(float), float value)
159 {
160         (static_cast<C &>(obj).*set_value)(value);
161 }
162
163 template<typename C>
164 void Material::PropertyLoader<C>::property_value_rgb(void (C::*set_value)(const Color &), float r, float g, float b)
165 {
166         (static_cast<C &>(obj).*set_value)(Color(r, g, b));
167 }
168
169 template<typename C>
170 void Material::PropertyLoader<C>::property_value_rgba(void (C::*set_value)(const Color &), float r, float g, float b, float a)
171 {
172         (static_cast<C &>(obj).*set_value)(Color(r, g, b, a));
173 }
174
175 template<typename C>
176 void Material::PropertyLoader<C>::property_value_srgb(void (C::*set_value)(const Color &), float r, float g, float b)
177 {
178         (static_cast<C &>(obj).*set_value)(Color(r, g, b).to_linear());
179 }
180
181 template<typename C>
182 void Material::PropertyLoader<C>::property_value_srgb_alpha(void (C::*set_value)(const Color &), float r, float g, float b, float a)
183 {
184         (static_cast<C &>(obj).*set_value)(Color(r, g, b, a).to_linear());
185 }
186
187 template<typename C>
188 void Material::PropertyLoader<C>::property_texture(void (C::*set_texture)(const Texture *), const std::string &name)
189 {
190         /* The static_cast around get_collection is needed because otherwise Android
191         SDK's g++ 4.9 fails to parse get<Texture> as a template function call */
192         (static_cast<C &>(obj).*set_texture)(&static_cast<Collection &>(get_collection()).get<Texture>(name));
193 }
194
195
196 template<typename T>
197 void Material::GenericLoader::typed_material()
198 {
199         if(material)
200                 throw std::logic_error("Material was already loaded");
201         RefPtr<T> mat = new T;
202         if(coll)
203                 load_sub(*mat, *coll);
204         else
205                 load_sub(*mat);
206         material = mat;
207 }
208
209 } // namespace GL
210 } // namespace Msp
211
212 #endif