]> git.tdb.fi Git - libs/gl.git/commitdiff
Add sampler support to materials
authorMikko Rasa <tdb@tdb.fi>
Sat, 6 Feb 2021 21:37:41 +0000 (23:37 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 6 Feb 2021 21:37:41 +0000 (23:37 +0200)
source/basicmaterial.cpp
source/material.cpp
source/material.h
source/pbrmaterial.cpp

index 708c8a5b6ccb876cb9124be8cd2d1cc1fecea4b4..362fc9db14e6a8636960bec3a746cd8e62715ed8 100644 (file)
@@ -139,6 +139,7 @@ BasicMaterial::Loader::Loader(BasicMaterial &m, Collection &c):
 
 void BasicMaterial::Loader::init_actions()
 {
+       Material::PropertyLoader<BasicMaterial>::init_actions();
        add_property("diffuse", &BasicMaterial::set_diffuse, &BasicMaterial::set_diffuse_map, true);
        add_property("specular", &BasicMaterial::set_specular, &BasicMaterial::set_specular_map, false);
        add_property("normal", &BasicMaterial::set_normal_map);
index 9109b786ecb4d414a3d455c57fe6421a06a7d314..a99f3c2cccc83f235b1da411f474288f386cffe7 100644 (file)
@@ -55,7 +55,7 @@ void Material::attach_texture_to(const Texture *tex, Texturing &texturing, Progr
        if(unit<0)
                throw runtime_error("no free texunit");
 
-       texturing.attach(unit, *tex);
+       texturing.attach(unit, *tex, sampler);
        tex_shdata.uniform(name, unit);
 }
 
@@ -72,6 +72,25 @@ Material::MaterialRegistry &Material::get_material_registry()
 }
 
 
+Material::Loader::Loader(Material &m):
+       CollectionObjectLoader(m, 0)
+{ }
+
+Material::Loader::Loader(Material &m, Collection &c):
+       CollectionObjectLoader(m, &c)
+{ }
+
+void Material::Loader::init_actions()
+{
+       add("sampler", &Loader::sampler);
+}
+
+void Material::Loader::sampler(const std::string &name)
+{
+       obj.sampler = &get_collection().get<Sampler>(name);
+}
+
+
 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
 
 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
index 3b9321fed482bba9748de130a7d03418b47d9050..571f4cb027c4da3a41e8d317e1e4c042603a2706 100644 (file)
@@ -15,6 +15,19 @@ 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 PropertyLoader: public DataFile::CollectionObjectLoader<Material>
+       class PropertyLoader: public DataFile::DerivedObjectLoader<Material, Loader>
        {
        protected:
-               PropertyLoader(C &m): CollectionObjectLoader<Material, Loader>(m, 0) { }
-               PropertyLoader(C &m, Collection &c): CollectionObjectLoader<Material, Loader>(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,9 +89,10 @@ private:
        typedef DataFile::LoadableTypeRegistry<GenericLoader, GenericLoader::AddType> MaterialRegistry;
 
 protected:
+       const Sampler *sampler;
        ProgramData shdata;
 
-       Material() { }
+       Material(): sampler(0) { }
 public:
        virtual ~Material() { }
 
index 7eaa56f1d1df99a78103ef9119bcd1f78bba1c27..bc7104cc652d485350614b5737b32e6a42771f80 100644 (file)
@@ -124,6 +124,7 @@ PbrMaterial::Loader::Loader(PbrMaterial &m, Collection &c):
 
 void PbrMaterial::Loader::init_actions()
 {
+       Material::PropertyLoader<PbrMaterial>::init_actions();
        add_property("base_color", &PbrMaterial::set_base_color, &PbrMaterial::set_base_color_map, true);
        add_property("normal", &PbrMaterial::set_normal_map);
        add_property("metalness", &PbrMaterial::set_metalness, &PbrMaterial::set_metalness_map);