From 218b613dc4d3898996bfda8e3ce1db32f7e1f929 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 6 Feb 2021 23:37:41 +0200 Subject: [PATCH] Add sampler support to materials --- source/basicmaterial.cpp | 1 + source/material.cpp | 21 ++++++++++++++++++++- source/material.h | 22 ++++++++++++++++++---- source/pbrmaterial.cpp | 1 + 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/source/basicmaterial.cpp b/source/basicmaterial.cpp index 708c8a5b..362fc9db 100644 --- a/source/basicmaterial.cpp +++ b/source/basicmaterial.cpp @@ -139,6 +139,7 @@ BasicMaterial::Loader::Loader(BasicMaterial &m, Collection &c): void BasicMaterial::Loader::init_actions() { + Material::PropertyLoader::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); diff --git a/source/material.cpp b/source/material.cpp index 9109b786..a99f3c2c 100644 --- a/source/material.cpp +++ b/source/material.cpp @@ -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(name); +} + + DataFile::Loader::ActionMap Material::GenericLoader::shared_actions; Material::GenericLoader::GenericLoader(DataFile::Collection *c): diff --git a/source/material.h b/source/material.h index 3b9321fe..571f4cb0 100644 --- a/source/material.h +++ b/source/material.h @@ -15,6 +15,19 @@ class Texturing; class Material { +private: + class Loader: public DataFile::CollectionObjectLoader + { + protected: + Loader(Material &); + Loader(Material &, Collection &); + + virtual void init_actions(); + + private: + void sampler(const std::string &); + }; + protected: template struct Property @@ -26,11 +39,11 @@ protected: }; template - class PropertyLoader: public DataFile::CollectionObjectLoader + class PropertyLoader: public DataFile::DerivedObjectLoader { protected: - PropertyLoader(C &m): CollectionObjectLoader(m, 0) { } - PropertyLoader(C &m, Collection &c): CollectionObjectLoader(m, c) { } + PropertyLoader(C &m): DerivedObjectLoader(m) { } + PropertyLoader(C &m, Collection &c): DerivedObjectLoader(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 MaterialRegistry; protected: + const Sampler *sampler; ProgramData shdata; - Material() { } + Material(): sampler(0) { } public: virtual ~Material() { } diff --git a/source/pbrmaterial.cpp b/source/pbrmaterial.cpp index 7eaa56f1..bc7104cc 100644 --- a/source/pbrmaterial.cpp +++ b/source/pbrmaterial.cpp @@ -124,6 +124,7 @@ PbrMaterial::Loader::Loader(PbrMaterial &m, Collection &c): void PbrMaterial::Loader::init_actions() { + Material::PropertyLoader::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); -- 2.43.0