]> git.tdb.fi Git - libs/gl.git/commitdiff
Generalize instancing to all Renderables
authorMikko Rasa <tdb@tdb.fi>
Wed, 12 Jan 2011 19:54:13 +0000 (19:54 +0000)
committerMikko Rasa <tdb@tdb.fi>
Wed, 12 Jan 2011 19:54:13 +0000 (19:54 +0000)
source/instancescene.cpp
source/instancescene.h
source/objectinstance.h
source/renderable.h

index adaebbba3ef364b34522df0a49dad2802c990b2f..fb99dda881f83ca8630a4cfcba36e42b3ca5a51a 100644 (file)
@@ -15,37 +15,25 @@ namespace GL {
 
 void InstanceScene::add(const Renderable &r)
 {
-       if(const ObjectInstance *oi = dynamic_cast<const ObjectInstance *>(&r))
-               objects[&oi->get_object()].insert(oi);
-       else
-               renderables.insert(&r);
+       renderables[r.get_instance_key()].insert(&r);
 }
 
 void InstanceScene::remove(const Renderable &r)
 {
-       if(const ObjectInstance *oi = dynamic_cast<const ObjectInstance *>(&r))
+       InstanceMap::iterator i = renderables.find(r.get_instance_key());
+       if(i!=renderables.end())
        {
-               ObjectMap::iterator i = objects.find(&oi->get_object());
-               if(i!=objects.end())
-               {
-                       i->second.erase(oi);
-                       if(i->second.empty())
-                               objects.erase(i);
-               }
+               i->second.erase(&r);
+               if(i->second.empty())
+                       renderables.erase(i);
        }
-       else
-               renderables.erase(&r);
 }
 
 void InstanceScene::render(Renderer &renderer, const Tag &tag) const
 {
-       // XXX Check that the object has this pass to avoid some unnecessary function calls
-       for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
-               for(InstanceSet::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
+       for(InstanceMap::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
+               for(RenderableSet::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
                        (*j)->render(renderer, tag);
-
-       for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
-               (*i)->render(renderer, tag);
 }
 
 } // namespace GL
index 55bd266d7afbd80ea77dabbb725b620b14b7c1de..18229d72cc59b59a43408c6bd2e6dd9de1decdb3 100644 (file)
@@ -15,22 +15,18 @@ Distributed under the LGPL
 namespace Msp {
 namespace GL {
 
-class Object;
-class ObjectInstance;
-
 /**
-A Scene optimized for rendering ObjectInstances.  All instances of the same
-Object are rendered in one go; otherwise the rendering order is unspecified.
+A Scene optimized for rendering instanced Renderables, such as ObjectInstances.
+All Renderables with the same instance key are rendered consecutively; within
+the same key rendering order is unspecified.
 */
 class InstanceScene: public Scene
 {
 private:
-       typedef std::set<const ObjectInstance *> InstanceSet;
-       typedef std::map<const Object *, InstanceSet> ObjectMap;
        typedef std::set<const Renderable *> RenderableSet;
+       typedef std::map<long, RenderableSet> InstanceMap;
 
-       ObjectMap objects;
-       RenderableSet renderables;
+       InstanceMap renderables;
 
 public:
        virtual void add(const Renderable &);
index d665851fbb5d40fb0201d17c255ea8667572a04c..e7f85e3fbc4c3bc17bf3dbe12aa499cd5c6834b3 100644 (file)
@@ -31,6 +31,7 @@ public:
        ObjectInstance(const Object &);
 
        const Object &get_object() const { return object; }
+       virtual long get_instance_key() const { return reinterpret_cast<long>(&object); }
 
        virtual void render(const Tag &tag = Tag()) const;
        virtual void render(Renderer &, const Tag & = Tag()) const;
index 8e31d5a7ac79e0f4a379b581da4b68ef37823c05..9fe46956540d4af243e61b2a9316c7eb08ccf020 100644 (file)
@@ -23,6 +23,9 @@ protected:
 public:
        virtual ~Renderable() { }
 
+       /** Returns a key used for grouping Renderables in an InstanceScene. */
+       virtual long get_instance_key() const { return 0; }
+
        virtual void render(const Tag & = Tag()) const;
        virtual void render(Renderer &, const Tag & = Tag()) const;
 };