]> git.tdb.fi Git - libs/gl.git/blobdiff - source/material.h
Remove the deprecated ProgramBuilder class
[libs/gl.git] / source / material.h
index df55a56767c6318ad9f6f3effed314520604fcd7..571f4cb027c4da3a41e8d317e1e4c042603a2706 100644 (file)
@@ -6,15 +6,28 @@
 #include <msp/datafile/objectloader.h>
 #include "color.h"
 #include "programdata.h"
+#include "texture.h"
 
 namespace Msp {
 namespace GL {
 
-class Texture;
 class Texturing;
 
 class Material
 {
+private:
+       class Loader: public DataFile::CollectionObjectLoader<Material>
+       {
+       protected:
+               Loader(Material &);
+               Loader(Material &, Collection &);
+
+               virtual void init_actions();
+
+       private:
+               void sampler(const std::string &);
+       };
+
 protected:
        template<typename T>
        struct Property
@@ -26,11 +39,11 @@ protected:
        };
 
        template<typename C>
-       class LoaderBase: public DataFile::CollectionObjectLoader<Material>
+       class PropertyLoader: public DataFile::DerivedObjectLoader<Material, Loader>
        {
        protected:
-               LoaderBase(C &m): CollectionObjectLoader<Material>(m, 0) { }
-               LoaderBase(C &m, Collection &c): CollectionObjectLoader<Material>(m, &c) { }
+               PropertyLoader(C &m): DerivedObjectLoader<Material, Loader>(m) { }
+               PropertyLoader(C &m, Collection &c): DerivedObjectLoader<Material, Loader>(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);
@@ -76,12 +89,18 @@ private:
        typedef DataFile::LoadableTypeRegistry<GenericLoader, GenericLoader::AddType> MaterialRegistry;
 
 protected:
+       const Sampler *sampler;
        ProgramData shdata;
 
-       Material() { }
+       Material(): sampler(0) { }
 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; }
@@ -105,65 +124,67 @@ void Material::register_type(const std::string &kw)
 
 
 template<typename C>
-void Material::LoaderBase<C>::add_property(const std::string &kw, void (C::*set_value)(float), void (C::*set_texture)(const Texture *))
+void Material::PropertyLoader<C>::add_property(const std::string &kw, void (C::*set_value)(float), void (C::*set_texture)(const Texture *))
 {
-       add(kw, &LoaderBase<C>::property_value_scalar, set_value);
-       add(kw+"_map", &LoaderBase<C>::property_texture, set_texture);
+       add(kw, &PropertyLoader<C>::property_value_scalar, set_value);
+       add(kw+"_map", &PropertyLoader<C>::property_texture, set_texture);
 }
 
 template<typename C>
-void Material::LoaderBase<C>::add_property(const std::string &kw, void (C::*set_value)(const Color &), void (C::*set_texture)(const Texture *), bool allow_alpha)
+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)
 {
-       add(kw, &LoaderBase<C>::property_value_rgb, set_value);
-       add(kw+"_srgb", &LoaderBase<C>::property_value_srgb, set_value);
+       add(kw, &PropertyLoader<C>::property_value_rgb, set_value);
+       add(kw+"_srgb", &PropertyLoader<C>::property_value_srgb, set_value);
        if(allow_alpha)
        {
-               add(kw, &LoaderBase<C>::property_value_rgba, set_value);
-               add(kw+"_srgb", &LoaderBase<C>::property_value_srgb_alpha, set_value);
+               add(kw, &PropertyLoader<C>::property_value_rgba, set_value);
+               add(kw+"_srgb", &PropertyLoader<C>::property_value_srgb_alpha, set_value);
        }
-       add(kw+"_map", &LoaderBase<C>::property_texture, set_texture);
+       add(kw+"_map", &PropertyLoader<C>::property_texture, set_texture);
 }
 
 template<typename C>
-void Material::LoaderBase<C>::add_property(const std::string &kw, void (C::*set_texture)(const Texture *))
+void Material::PropertyLoader<C>::add_property(const std::string &kw, void (C::*set_texture)(const Texture *))
 {
-       add(kw+"_map", &LoaderBase<C>::property_texture, set_texture);
+       add(kw+"_map", &PropertyLoader<C>::property_texture, set_texture);
 }
 
 template<typename C>
-void Material::LoaderBase<C>::property_value_scalar(void (C::*set_value)(float), float value)
+void Material::PropertyLoader<C>::property_value_scalar(void (C::*set_value)(float), float value)
 {
        (static_cast<C &>(obj).*set_value)(value);
 }
 
 template<typename C>
-void Material::LoaderBase<C>::property_value_rgb(void (C::*set_value)(const Color &), float r, float g, float b)
+void Material::PropertyLoader<C>::property_value_rgb(void (C::*set_value)(const Color &), float r, float g, float b)
 {
        (static_cast<C &>(obj).*set_value)(Color(r, g, b));
 }
 
 template<typename C>
-void Material::LoaderBase<C>::property_value_rgba(void (C::*set_value)(const Color &), float r, float g, float b, float a)
+void Material::PropertyLoader<C>::property_value_rgba(void (C::*set_value)(const Color &), float r, float g, float b, float a)
 {
        (static_cast<C &>(obj).*set_value)(Color(r, g, b, a));
 }
 
 template<typename C>
-void Material::LoaderBase<C>::property_value_srgb(void (C::*set_value)(const Color &), float r, float g, float b)
+void Material::PropertyLoader<C>::property_value_srgb(void (C::*set_value)(const Color &), float r, float g, float b)
 {
        (static_cast<C &>(obj).*set_value)(Color(r, g, b).to_linear());
 }
 
 template<typename C>
-void Material::LoaderBase<C>::property_value_srgb_alpha(void (C::*set_value)(const Color &), float r, float g, float b, float a)
+void Material::PropertyLoader<C>::property_value_srgb_alpha(void (C::*set_value)(const Color &), float r, float g, float b, float a)
 {
        (static_cast<C &>(obj).*set_value)(Color(r, g, b, a).to_linear());
 }
 
 template<typename C>
-void Material::LoaderBase<C>::property_texture(void (C::*set_texture)(const Texture *), const std::string &name)
+void Material::PropertyLoader<C>::property_texture(void (C::*set_texture)(const Texture *), const std::string &name)
 {
-       (static_cast<C &>(obj).*set_texture)(&get_collection().get<GL::Texture>(name));
+       /* The static_cast around get_collection is needed because otherwise Android
+       SDK's g++ 4.9 fails to parse get<Texture> as a template function call */
+       (static_cast<C &>(obj).*set_texture)(&static_cast<Collection &>(get_collection()).get<Texture>(name));
 }