]> git.tdb.fi Git - libs/gl.git/blob - source/materials/material.h
Rearrange type specification in material datafiles
[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 CreateMaterial
66                 {
67                         void operator()(const std::string &, GenericLoader &) const;
68                 };
69
70                 DataFile::Collection *coll;
71                 Material *material;
72                 Loader *mat_loader;
73
74                 static ActionMap shared_actions;
75
76         public:
77                 GenericLoader(DataFile::Collection * = 0);
78                 ~GenericLoader();
79
80                 Material *get_material() { Material *m = material; material = 0; return m; }
81         private:
82                 virtual void init_actions();
83
84                 void type(const DataFile::Symbol &);
85
86                 friend class Material;
87         };
88
89 private:
90         typedef TypeRegistry<GenericLoader::CreateMaterial, GenericLoader &> MaterialRegistry;
91
92 protected:
93         const Sampler *sampler;
94         ProgramData shdata;
95
96         Material(): sampler(0) { }
97 public:
98         virtual ~Material() { }
99
100         virtual const Program *create_compatible_shader(DataFile::Collection &) const;
101 protected:
102         virtual void fill_program_info(std::string &, std::map<std::string, int> &) const = 0;
103
104 public:
105         /** Returns the uniforms for the material. */
106         const ProgramData &get_shader_data() const { return shdata; }
107
108 protected:
109         DEPRECATED void attach_texture_to(const Texture *, Texturing &, ProgramData &, const std::string &) const;
110 public:
111         DEPRECATED virtual void attach_textures_to(Texturing &, ProgramData &) const = 0;
112
113         virtual const Tag *get_texture_tags() const = 0;
114         virtual const Texture *get_texture(Tag) const = 0;
115         const Sampler *get_sampler() const { return sampler; }
116
117         template<typename T>
118         static void register_type(const std::string &);
119 private:
120         static MaterialRegistry &get_material_registry();
121 };
122
123 template<typename T>
124 void Material::register_type(const std::string &kw)
125 {
126         get_material_registry().register_type<T>(kw);
127 }
128
129
130 template<typename C>
131 void Material::PropertyLoader<C>::add_property(const std::string &kw, void (C::*set_value)(float), void (C::*set_texture)(const Texture *))
132 {
133         add(kw, &PropertyLoader<C>::property_value_scalar, set_value);
134         if(set_texture)
135                 add(kw+"_map", &PropertyLoader<C>::property_texture, set_texture);
136 }
137
138 template<typename C>
139 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)
140 {
141         add(kw, &PropertyLoader<C>::property_value_rgb, set_value);
142         add(kw+"_srgb", &PropertyLoader<C>::property_value_srgb, set_value);
143         if(allow_alpha)
144         {
145                 add(kw, &PropertyLoader<C>::property_value_rgba, set_value);
146                 add(kw+"_srgb", &PropertyLoader<C>::property_value_srgb_alpha, set_value);
147         }
148         if(set_texture)
149                 add(kw+"_map", &PropertyLoader<C>::property_texture, set_texture);
150 }
151
152 template<typename C>
153 void Material::PropertyLoader<C>::add_property(const std::string &kw, void (C::*set_texture)(const Texture *))
154 {
155         add(kw+"_map", &PropertyLoader<C>::property_texture, set_texture);
156 }
157
158 template<typename C>
159 void Material::PropertyLoader<C>::property_value_scalar(void (C::*set_value)(float), float value)
160 {
161         (static_cast<C &>(obj).*set_value)(value);
162 }
163
164 template<typename C>
165 void Material::PropertyLoader<C>::property_value_rgb(void (C::*set_value)(const Color &), float r, float g, float b)
166 {
167         (static_cast<C &>(obj).*set_value)(Color(r, g, b));
168 }
169
170 template<typename C>
171 void Material::PropertyLoader<C>::property_value_rgba(void (C::*set_value)(const Color &), float r, float g, float b, float a)
172 {
173         (static_cast<C &>(obj).*set_value)(Color(r, g, b, a));
174 }
175
176 template<typename C>
177 void Material::PropertyLoader<C>::property_value_srgb(void (C::*set_value)(const Color &), float r, float g, float b)
178 {
179         (static_cast<C &>(obj).*set_value)(Color(r, g, b).to_linear());
180 }
181
182 template<typename C>
183 void Material::PropertyLoader<C>::property_value_srgb_alpha(void (C::*set_value)(const Color &), float r, float g, float b, float a)
184 {
185         (static_cast<C &>(obj).*set_value)(Color(r, g, b, a).to_linear());
186 }
187
188 template<typename C>
189 void Material::PropertyLoader<C>::property_texture(void (C::*set_texture)(const Texture *), const std::string &name)
190 {
191         /* The static_cast around get_collection is needed because otherwise Android
192         SDK's g++ 4.9 fails to parse get<Texture> as a template function call */
193         (static_cast<C &>(obj).*set_texture)(&static_cast<Collection &>(get_collection()).get<Texture>(name));
194 }
195
196
197 template<typename T>
198 void Material::GenericLoader::CreateMaterial<T>::operator()(const std::string &, GenericLoader &ldr) const
199 {
200         if(ldr.material)
201                 throw std::logic_error("Material type was already specified");
202
203         T *mat = new T;
204         ldr.material = mat;
205         if(ldr.coll)
206                 ldr.mat_loader = new typename T::Loader(*mat, *ldr.coll);
207         else
208                 ldr.mat_loader = new typename T::Loader(*mat);
209         ldr.add_auxiliary_loader(*ldr.mat_loader);
210 }
211
212 } // namespace GL
213 } // namespace Msp
214
215 #endif