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
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 &);
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;
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;
};