From: Mikko Rasa Date: Wed, 12 Jan 2011 19:54:13 +0000 (+0000) Subject: Generalize instancing to all Renderables X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=0e8ffd3b59e9b5c52c66c22c9ae4f9d3544aa6bf;p=libs%2Fgl.git Generalize instancing to all Renderables --- diff --git a/source/instancescene.cpp b/source/instancescene.cpp index adaebbba..fb99dda8 100644 --- a/source/instancescene.cpp +++ b/source/instancescene.cpp @@ -15,37 +15,25 @@ namespace GL { void InstanceScene::add(const Renderable &r) { - if(const ObjectInstance *oi = dynamic_cast(&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(&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 diff --git a/source/instancescene.h b/source/instancescene.h index 55bd266d..18229d72 100644 --- a/source/instancescene.h +++ b/source/instancescene.h @@ -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 InstanceSet; - typedef std::map ObjectMap; typedef std::set RenderableSet; + typedef std::map InstanceMap; - ObjectMap objects; - RenderableSet renderables; + InstanceMap renderables; public: virtual void add(const Renderable &); diff --git a/source/objectinstance.h b/source/objectinstance.h index d665851f..e7f85e3f 100644 --- a/source/objectinstance.h +++ b/source/objectinstance.h @@ -31,6 +31,7 @@ public: ObjectInstance(const Object &); const Object &get_object() const { return object; } + virtual long get_instance_key() const { return reinterpret_cast(&object); } virtual void render(const Tag &tag = Tag()) const; virtual void render(Renderer &, const Tag & = Tag()) const; diff --git a/source/renderable.h b/source/renderable.h index 8e31d5a7..9fe46956 100644 --- a/source/renderable.h +++ b/source/renderable.h @@ -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; };