From: Mikko Rasa Date: Thu, 17 Mar 2022 20:06:10 +0000 (+0200) Subject: Add a loader to InstanceArray and make them loadable in scenes X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=85d86a0d7cddce83578629e5817b6e1b50061540;p=libs%2Fgl.git Add a loader to InstanceArray and make them loadable in scenes --- diff --git a/source/render/instancearray.cpp b/source/render/instancearray.cpp index 0a3d6b68..20dcb888 100644 --- a/source/render/instancearray.cpp +++ b/source/render/instancearray.cpp @@ -194,5 +194,19 @@ void InstanceArrayBase::render(Renderer &renderer, Tag tag) const mesh->draw_instanced(renderer, vtx_setup, instance_count); } + +DataFile::Loader::ActionMap InstanceArrayBase::Loader::shared_actions; + +InstanceArrayBase::Loader::Loader(InstanceArrayBase &o): + ObjectLoader(o) +{ + set_actions(shared_actions); +} + +void InstanceArrayBase::Loader::init_actions() +{ + add("instance", &Loader::instance); +} + } // namespace GL } // namespace Msp diff --git a/source/render/instancearray.h b/source/render/instancearray.h index f2a9b2c0..6f4048ec 100644 --- a/source/render/instancearray.h +++ b/source/render/instancearray.h @@ -17,6 +17,22 @@ class ObjectInstance; class InstanceArrayBase: public Renderable, public NonCopyable { +protected: + class Loader: public DataFile::ObjectLoader + { + private: + static ActionMap shared_actions; + + public: + Loader(InstanceArrayBase &); + + private: + virtual void init_actions(); + + protected: + virtual void instance() = 0; + }; + private: struct Block { @@ -124,6 +140,16 @@ is removed, its address may later be reused for another instance. template class InstanceArray: public InstanceArrayBase { +public: + class Loader: public DataFile::DerivedObjectLoader + { + public: + Loader(InstanceArray &a): DataFile::DerivedObjectLoader(a) { } + + protected: + virtual void instance(); + }; + private: class Instance: public T { @@ -153,6 +179,14 @@ inline void InstanceArray::Instance::set_matrix(const Matrix &m) array.update_instance_matrix(index, *this->get_matrix()); } + +template +void InstanceArray::Loader::instance() +{ + T &inst = this->obj.append(); + this->load_sub(inst); +} + } // namespace GL } // namespace Msp diff --git a/source/render/scene.cpp b/source/render/scene.cpp index a9cf8f96..a05e7110 100644 --- a/source/render/scene.cpp +++ b/source/render/scene.cpp @@ -2,6 +2,7 @@ #include #include #include "camera.h" +#include "instancearray.h" #include "objectinstance.h" #include "occludedscene.h" #include "orderedscene.h" @@ -37,12 +38,21 @@ Scene::Loader::Loader(Scene &s, Collection &c, ContentMap *m): DataFile::CollectionObjectLoader(s, &c), content(m) { + add("array", &Loader::array); add("object", &Loader::object); add("object", &Loader::object_tagged); add("scene", &Loader::scene); add("scene", &Loader::scene_inline); } +void Scene::Loader::array(const string &n) +{ + RefPtr > arr = new InstanceArray<>(get_collection().get(n)); + load_sub(*arr); + get_collection().add(format("_scene_array_%d.array", ++inline_counter), arr.get()); + obj.add(*arr.release()); +} + void Scene::Loader::object(const string &n) { object_tagged(n, string()); diff --git a/source/render/scene.h b/source/render/scene.h index 6f4f48d6..512b020b 100644 --- a/source/render/scene.h +++ b/source/render/scene.h @@ -40,6 +40,7 @@ protected: private: Loader(Scene &, Collection &, ContentMap *); + void array(const std::string &); void object(const std::string &); void object_tagged(const std::string &, const std::string &); void scene(const std::string &);