1 #ifndef MSP_GL_MATERIAL_H_
2 #define MSP_GL_MATERIAL_H_
4 #include <msp/datafile/collection.h>
5 #include <msp/datafile/loadabletyperegistry.h>
6 #include <msp/datafile/objectloader.h>
8 #include "programdata.h"
19 class Loader: public DataFile::CollectionObjectLoader<Material>
23 Loader(Material &, Collection &);
25 virtual void init_actions();
28 void sampler(const std::string &);
36 const Texture *texture;
38 Property(): value(T()), texture(0) { }
42 class PropertyLoader: public DataFile::DerivedObjectLoader<Material, Loader>
45 PropertyLoader(C &m): DerivedObjectLoader<Material, Loader>(m) { }
46 PropertyLoader(C &m, Collection &c): DerivedObjectLoader<Material, Loader>(m, c) { }
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 *));
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 &);
61 class GenericLoader: public DataFile::Loader
67 static void add(GenericLoader &ldr, const std::string &kw) { ldr.add(kw, &GenericLoader::typed_material<T>); }
70 DataFile::Collection *coll;
71 RefPtr<Material> material;
73 static ActionMap shared_actions;
76 GenericLoader(DataFile::Collection * = 0);
78 Material *get_material() { return material.release(); }
80 virtual void init_actions();
83 void typed_material();
85 friend class Material;
89 typedef DataFile::LoadableTypeRegistry<GenericLoader, GenericLoader::AddType> MaterialRegistry;
92 const Sampler *sampler;
95 Material(): sampler(0) { }
97 virtual ~Material() { }
99 virtual Program *create_compatible_shader() const;
100 virtual const Program *create_compatible_shader(DataFile::Collection &) const;
102 virtual std::string create_program_source() const = 0;
105 /** Returns the uniforms for the material. */
106 const ProgramData &get_shader_data() const { return shdata; }
109 void attach_texture_to(const Texture *, Texturing &, ProgramData &, const std::string &) const;
111 virtual void attach_textures_to(Texturing &, ProgramData &) const = 0;
114 static void register_type(const std::string &);
116 static MaterialRegistry &get_material_registry();
120 void Material::register_type(const std::string &kw)
122 get_material_registry().register_type<T>(kw);
127 void Material::PropertyLoader<C>::add_property(const std::string &kw, void (C::*set_value)(float), void (C::*set_texture)(const Texture *))
129 add(kw, &PropertyLoader<C>::property_value_scalar, set_value);
130 add(kw+"_map", &PropertyLoader<C>::property_texture, set_texture);
134 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)
136 add(kw, &PropertyLoader<C>::property_value_rgb, set_value);
137 add(kw+"_srgb", &PropertyLoader<C>::property_value_srgb, set_value);
140 add(kw, &PropertyLoader<C>::property_value_rgba, set_value);
141 add(kw+"_srgb", &PropertyLoader<C>::property_value_srgb_alpha, set_value);
143 add(kw+"_map", &PropertyLoader<C>::property_texture, set_texture);
147 void Material::PropertyLoader<C>::add_property(const std::string &kw, void (C::*set_texture)(const Texture *))
149 add(kw+"_map", &PropertyLoader<C>::property_texture, set_texture);
153 void Material::PropertyLoader<C>::property_value_scalar(void (C::*set_value)(float), float value)
155 (static_cast<C &>(obj).*set_value)(value);
159 void Material::PropertyLoader<C>::property_value_rgb(void (C::*set_value)(const Color &), float r, float g, float b)
161 (static_cast<C &>(obj).*set_value)(Color(r, g, b));
165 void Material::PropertyLoader<C>::property_value_rgba(void (C::*set_value)(const Color &), float r, float g, float b, float a)
167 (static_cast<C &>(obj).*set_value)(Color(r, g, b, a));
171 void Material::PropertyLoader<C>::property_value_srgb(void (C::*set_value)(const Color &), float r, float g, float b)
173 (static_cast<C &>(obj).*set_value)(Color(r, g, b).to_linear());
177 void Material::PropertyLoader<C>::property_value_srgb_alpha(void (C::*set_value)(const Color &), float r, float g, float b, float a)
179 (static_cast<C &>(obj).*set_value)(Color(r, g, b, a).to_linear());
183 void Material::PropertyLoader<C>::property_texture(void (C::*set_texture)(const Texture *), const std::string &name)
185 /* The static_cast around get_collection is needed because otherwise Android
186 SDK's g++ 4.9 fails to parse get<Texture> as a template function call */
187 (static_cast<C &>(obj).*set_texture)(&static_cast<Collection &>(get_collection()).get<Texture>(name));
192 void Material::GenericLoader::typed_material()
195 throw std::logic_error("Material was already loaded");
196 RefPtr<T> mat = new T;
198 load_sub(*mat, *coll);