X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fmaterial.h;h=e1bfec4caa0707419941b88476394955ab651244;hp=3b9321fed482bba9748de130a7d03418b47d9050;hb=HEAD;hpb=f5c631cecb6602ae31287b0eadd699002175cc74 diff --git a/source/material.h b/source/material.h deleted file mode 100644 index 3b9321fe..00000000 --- a/source/material.h +++ /dev/null @@ -1,193 +0,0 @@ -#ifndef MSP_GL_MATERIAL_H_ -#define MSP_GL_MATERIAL_H_ - -#include -#include -#include -#include "color.h" -#include "programdata.h" -#include "texture.h" - -namespace Msp { -namespace GL { - -class Texturing; - -class Material -{ -protected: - template - struct Property - { - T value; - const Texture *texture; - - Property(): value(T()), texture(0) { } - }; - - template - class PropertyLoader: public DataFile::CollectionObjectLoader - { - protected: - PropertyLoader(C &m): CollectionObjectLoader(m, 0) { } - PropertyLoader(C &m, Collection &c): CollectionObjectLoader(m, c) { } - - void add_property(const std::string &, void (C::*)(float), void (C::*)(const Texture *)); - void add_property(const std::string &, void (C::*)(const Color &), void (C::*)(const Texture *), bool); - void add_property(const std::string &, void (C::*)(const Texture *)); - - void property_value_scalar(void (C::*)(float), float); - void property_value_rgb(void (C::*)(const Color &), float, float, float); - void property_value_rgba(void (C::*)(const Color &), float, float, float, float); - void property_value_srgb(void (C::*)(const Color &), float, float, float); - void property_value_srgb_alpha(void (C::*)(const Color &), float, float, float, float); - void property_texture(void (C::*)(const Texture *), const std::string &); - }; - -public: - class GenericLoader: public DataFile::Loader - { - private: - template - struct AddType - { - static void add(GenericLoader &ldr, const std::string &kw) { ldr.add(kw, &GenericLoader::typed_material); } - }; - - DataFile::Collection *coll; - RefPtr material; - - static ActionMap shared_actions; - - public: - GenericLoader(DataFile::Collection * = 0); - - Material *get_material() { return material.release(); } - private: - virtual void init_actions(); - - template - void typed_material(); - - friend class Material; - }; - -private: - typedef DataFile::LoadableTypeRegistry MaterialRegistry; - -protected: - ProgramData shdata; - - Material() { } -public: - virtual ~Material() { } - - virtual Program *create_compatible_shader() const; - virtual const Program *create_compatible_shader(DataFile::Collection &) const; -protected: - virtual std::string create_program_source() const = 0; - -public: - /** Returns the uniforms for the material. */ - const ProgramData &get_shader_data() const { return shdata; } - -protected: - void attach_texture_to(const Texture *, Texturing &, ProgramData &, const std::string &) const; -public: - virtual void attach_textures_to(Texturing &, ProgramData &) const = 0; - - template - static void register_type(const std::string &); -private: - static MaterialRegistry &get_material_registry(); -}; - -template -void Material::register_type(const std::string &kw) -{ - get_material_registry().register_type(kw); -} - - -template -void Material::PropertyLoader::add_property(const std::string &kw, void (C::*set_value)(float), void (C::*set_texture)(const Texture *)) -{ - add(kw, &PropertyLoader::property_value_scalar, set_value); - add(kw+"_map", &PropertyLoader::property_texture, set_texture); -} - -template -void Material::PropertyLoader::add_property(const std::string &kw, void (C::*set_value)(const Color &), void (C::*set_texture)(const Texture *), bool allow_alpha) -{ - add(kw, &PropertyLoader::property_value_rgb, set_value); - add(kw+"_srgb", &PropertyLoader::property_value_srgb, set_value); - if(allow_alpha) - { - add(kw, &PropertyLoader::property_value_rgba, set_value); - add(kw+"_srgb", &PropertyLoader::property_value_srgb_alpha, set_value); - } - add(kw+"_map", &PropertyLoader::property_texture, set_texture); -} - -template -void Material::PropertyLoader::add_property(const std::string &kw, void (C::*set_texture)(const Texture *)) -{ - add(kw+"_map", &PropertyLoader::property_texture, set_texture); -} - -template -void Material::PropertyLoader::property_value_scalar(void (C::*set_value)(float), float value) -{ - (static_cast(obj).*set_value)(value); -} - -template -void Material::PropertyLoader::property_value_rgb(void (C::*set_value)(const Color &), float r, float g, float b) -{ - (static_cast(obj).*set_value)(Color(r, g, b)); -} - -template -void Material::PropertyLoader::property_value_rgba(void (C::*set_value)(const Color &), float r, float g, float b, float a) -{ - (static_cast(obj).*set_value)(Color(r, g, b, a)); -} - -template -void Material::PropertyLoader::property_value_srgb(void (C::*set_value)(const Color &), float r, float g, float b) -{ - (static_cast(obj).*set_value)(Color(r, g, b).to_linear()); -} - -template -void Material::PropertyLoader::property_value_srgb_alpha(void (C::*set_value)(const Color &), float r, float g, float b, float a) -{ - (static_cast(obj).*set_value)(Color(r, g, b, a).to_linear()); -} - -template -void Material::PropertyLoader::property_texture(void (C::*set_texture)(const Texture *), const std::string &name) -{ - /* The static_cast around get_collection is needed because otherwise Android - SDK's g++ 4.9 fails to parse get as a template function call */ - (static_cast(obj).*set_texture)(&static_cast(get_collection()).get(name)); -} - - -template -void Material::GenericLoader::typed_material() -{ - if(material) - throw std::logic_error("Material was already loaded"); - RefPtr mat = new T; - if(coll) - load_sub(*mat, *coll); - else - load_sub(*mat); - material = mat; -} - -} // namespace GL -} // namespace Msp - -#endif