From: Mikko Rasa Date: Mon, 12 Apr 2021 12:38:54 +0000 (+0300) Subject: Rearrange type specification in material datafiles X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=a4b9ae04a0a89bb2cf3ab4235d7376d3ff70af7b Rearrange type specification in material datafiles The type is now specified as a separate statement with no substatements. This is what I wanted to do originally but couldn't figure out a clean way to do it. No legacy support because the new material system as a whole is still fairly new and hasn't been used in many places. --- diff --git a/blender/io_mspgl/export_material.py b/blender/io_mspgl/export_material.py index c7ca765c..10be7dcb 100644 --- a/blender/io_mspgl/export_material.py +++ b/blender/io_mspgl/export_material.py @@ -67,24 +67,23 @@ class MaterialExporter: return create_technique_resource(material, resources) def export_material(self, material, *, resources): - from .datafile import Resource, Statement + from .datafile import Resource, Statement, Token mat_res = Resource(material.name+".mat", "material") if material.type!="pbr" and material.type!="unlit": raise Exception("Can't export unknown material type "+material.type) - st = Statement(material.type) + mat_res.statements.append(Statement("type", Token(material.type))); for p in material.properties: - ss = self.create_property_statement(mat_res, p, resources) - if ss: - st.sub.append(ss) + st = self.create_property_statement(mat_res, p, resources) + if st: + mat_res.statements.append(st) if self.use_textures: textures = [p.texture for p in material.properties if p.texture] if textures and not textures[0].default_filter: from .export_texture import SamplerExporter sampler_export = SamplerExporter() - st.sub.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(textures[0])])) - mat_res.statements.append(st) + mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(textures[0])])) return mat_res @@ -134,10 +133,9 @@ class MaterialAtlasExporter: mat_name = material_atlas.name+".mat" if mat_name not in resources: mat_res = Resource(mat_name, "material") - st = Statement("pbr") - st.sub.append(mat_res.create_reference_statement("base_color_map", base_color_res)) - st.sub.append(mat_res.create_reference_statement("sampler", sampler_res)) - mat_res.statements.append(st) + mat_res.statements.append(Statement("type", Token('pbr'))) + mat_res.statements.append(mat_res.create_reference_statement("base_color_map", base_color_res)) + mat_res.statements.append(mat_res.create_reference_statement("sampler", sampler_res)) resources[mat_name] = mat_res diff --git a/source/materials/material.cpp b/source/materials/material.cpp index d87f2f83..36d0958f 100644 --- a/source/materials/material.cpp +++ b/source/materials/material.cpp @@ -103,14 +103,27 @@ void Material::Loader::sampler(const string &name) DataFile::Loader::ActionMap Material::GenericLoader::shared_actions; Material::GenericLoader::GenericLoader(DataFile::Collection *c): - coll(c) + coll(c), + material(0), + mat_loader(0) { set_actions(shared_actions); } +Material::GenericLoader::~GenericLoader() +{ + delete material; + delete mat_loader; +} + void Material::GenericLoader::init_actions() { - get_material_registry().invoke_all(*this); + add("type", &GenericLoader::type); +} + +void Material::GenericLoader::type(const DataFile::Symbol &sym) +{ + get_material_registry().invoke(sym.name, *this); } } // namespace GL diff --git a/source/materials/material.h b/source/materials/material.h index 2f8f38ab..278fc4dc 100644 --- a/source/materials/material.h +++ b/source/materials/material.h @@ -62,31 +62,32 @@ public: { private: template - struct AddType + struct CreateMaterial { - void operator()(const std::string &kw, GenericLoader &ldr) const { ldr.add(kw, &GenericLoader::typed_material); } + void operator()(const std::string &, GenericLoader &) const; }; DataFile::Collection *coll; - RefPtr material; + Material *material; + Loader *mat_loader; static ActionMap shared_actions; public: GenericLoader(DataFile::Collection * = 0); + ~GenericLoader(); - Material *get_material() { return material.release(); } + Material *get_material() { Material *m = material; material = 0; return m; } private: virtual void init_actions(); - template - void typed_material(); + void type(const DataFile::Symbol &); friend class Material; }; private: - typedef TypeRegistry MaterialRegistry; + typedef TypeRegistry MaterialRegistry; protected: const Sampler *sampler; @@ -194,16 +195,18 @@ void Material::PropertyLoader::property_texture(void (C::*set_texture)(const template -void Material::GenericLoader::typed_material() +void Material::GenericLoader::CreateMaterial::operator()(const std::string &, GenericLoader &ldr) const { - if(material) - throw std::logic_error("Material was already loaded"); - RefPtr mat = new T; - if(coll) - load_sub(*mat, *coll); + if(ldr.material) + throw std::logic_error("Material type was already specified"); + + T *mat = new T; + ldr.material = mat; + if(ldr.coll) + ldr.mat_loader = new typename T::Loader(*mat, *ldr.coll); else - load_sub(*mat); - material = mat; + ldr.mat_loader = new typename T::Loader(*mat); + ldr.add_auxiliary_loader(*ldr.mat_loader); } } // namespace GL