]> git.tdb.fi Git - libs/gl.git/commitdiff
Add a loader to InstanceArray and make them loadable in scenes
authorMikko Rasa <tdb@tdb.fi>
Thu, 17 Mar 2022 20:06:10 +0000 (22:06 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 17 Mar 2022 20:06:37 +0000 (22:06 +0200)
source/render/instancearray.cpp
source/render/instancearray.h
source/render/scene.cpp
source/render/scene.h

index 0a3d6b685ad3ffb98b9ee6265db0eba2c3328431..20dcb8885ea86061905949d8f995e47a0fd2ad46 100644 (file)
@@ -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<InstanceArrayBase>(o)
+{
+       set_actions(shared_actions);
+}
+
+void InstanceArrayBase::Loader::init_actions()
+{
+       add("instance", &Loader::instance);
+}
+
 } // namespace GL
 } // namespace Msp
index f2a9b2c00be5930c281a8341851d838af949ba1f..6f4048ece6ea580dfcdf30f78de4d8388c82d3a5 100644 (file)
@@ -17,6 +17,22 @@ class ObjectInstance;
 
 class InstanceArrayBase: public Renderable, public NonCopyable
 {
+protected:
+       class Loader: public DataFile::ObjectLoader<InstanceArrayBase>
+       {
+       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<typename T = ObjectInstance>
 class InstanceArray: public InstanceArrayBase
 {
+public:
+       class Loader: public DataFile::DerivedObjectLoader<InstanceArray, InstanceArrayBase::Loader>
+       {
+       public:
+               Loader(InstanceArray &a): DataFile::DerivedObjectLoader<InstanceArray, InstanceArrayBase::Loader>(a) { }
+
+       protected:
+               virtual void instance();
+       };
+
 private:
        class Instance: public T
        {
@@ -153,6 +179,14 @@ inline void InstanceArray<T>::Instance::set_matrix(const Matrix &m)
        array.update_instance_matrix(index, *this->get_matrix());
 }
 
+
+template<typename T>
+void InstanceArray<T>::Loader::instance()
+{
+       T &inst = this->obj.append();
+       this->load_sub(inst);
+}
+
 } // namespace GL
 } // namespace Msp
 
index a9cf8f96b326f76d388fed95187fbacd4dcdfa25..a05e7110ce770def4ab7a49559b507151ed2312a 100644 (file)
@@ -2,6 +2,7 @@
 #include <msp/fs/utils.h>
 #include <msp/strings/format.h>
 #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<Scene>(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<InstanceArray<> > arr = new InstanceArray<>(get_collection().get<GL::Object>(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());
index 6f4f48d675e6bce3db2850ca24eb78466157769e..512b020b8e3b29d5b930baff4c7bc29cc58cfb5d 100644 (file)
@@ -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 &);