--- /dev/null
+/* $Id$
+
+This file is part of libmspgl
+Copyright © 2007-2008, 2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include "object.h"
+#include "objectinstance.h"
+#include "instancescene.h"
+
+namespace Msp {
+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);
+}
+
+void InstanceScene::remove(const Renderable &r)
+{
+ if(const ObjectInstance *oi = dynamic_cast<const ObjectInstance *>(&r))
+ {
+ ObjectMap::iterator i = objects.find(&oi->get_object());
+ if(i!=objects.end())
+ {
+ i->second.erase(oi);
+ if(i->second.empty())
+ objects.erase(i);
+ }
+ }
+ else
+ renderables.erase(&r);
+}
+
+void InstanceScene::render(const Tag &tag) const
+{
+ for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
+ i->first->render(i->second.begin(), i->second.end(), tag);
+
+ for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
+ (*i)->render(tag);
+}
+
+} // namespace GL
+} // namespace Msp
--- /dev/null
+/* $Id$
+
+This file is part of libmspgl
+Copyright © 2007-2008, 2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#ifndef MSP_GL_INSTANCESCENE_H_
+#define MSP_GL_INSTANCESCENE_H_
+
+#include <map>
+#include <set>
+#include "scene.h"
+
+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.
+*/
+class InstanceScene: public Scene
+{
+private:
+ typedef std::set<const ObjectInstance *> InstanceSet;
+ typedef std::map<const Object *, InstanceSet> ObjectMap;
+ typedef std::set<const Renderable *> RenderableSet;
+
+ ObjectMap objects;
+ RenderableSet renderables;
+
+public:
+ virtual void add(const Renderable &);
+ virtual void remove(const Renderable &);
+
+ virtual void render(const Tag &tag = Tag()) const;
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif
--- /dev/null
+/* $Id$
+
+This file is part of libmspgl
+Copyright © 2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include <algorithm>
+#include "orderedscene.h"
+
+namespace Msp {
+namespace GL {
+
+void OrderedScene::add(const Renderable &r)
+{
+ renderables.push_back(&r);
+}
+
+void OrderedScene::remove(const Renderable &r)
+{
+ RenderableList::iterator end = std::remove(renderables.begin(), renderables.end(), &r);
+ renderables.erase(end, renderables.end());
+}
+
+void OrderedScene::prepend(const Renderable &r)
+{
+ renderables.push_front(&r);
+}
+
+void OrderedScene::insert(unsigned index, const Renderable &r)
+{
+ RenderableList::iterator i = renderables.begin();
+ for(; (i!=renderables.end() && index); ++i, --index) ;
+ renderables.insert(i, &r);
+}
+
+void OrderedScene::insert_after(const Renderable &after, const Renderable &r)
+{
+ RenderableList::iterator i = renderables.begin();
+ for(; (i!=renderables.end() && *i!=&after); ++i) ;
+ renderables.insert(i, &r);
+}
+
+void OrderedScene::render(const Tag &tag) const
+{
+ for(RenderableList::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
+ (*i)->render(tag);
+}
+
+} // namespace GL
+} // namespace Msp
--- /dev/null
+/* $Id$
+
+This file is part of libmspgl
+Copyright © 2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#ifndef MSP_GL_ORDEREDSCENE_H_
+#define MSP_GL_ORDEREDSCENE_H_
+
+#include <list>
+#include <msp/gl/scene.h>
+
+namespace Msp {
+namespace GL {
+
+/**
+A scene that renders its contents in a specific order. Inserting Renderables
+in the middle and removing them are O(N) operations.
+*/
+class OrderedScene: public Scene
+{
+private:
+ typedef std::list<const Renderable *> RenderableList;
+
+ RenderableList renderables;
+
+public:
+ virtual void add(const Renderable &);
+ virtual void remove(const Renderable &);
+ void prepend(const Renderable &);
+ void insert(unsigned, const Renderable &);
+ void insert_after(const Renderable &, const Renderable &);
+
+ virtual void render(const Tag & = Tag()) const;
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif
+++ /dev/null
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include "object.h"
-#include "objectinstance.h"
-#include "scene.h"
-
-namespace Msp {
-namespace GL {
-
-void Scene::add(const Renderable &r)
-{
- if(const ObjectInstance *oi = dynamic_cast<const ObjectInstance *>(&r))
- objects[&oi->get_object()].insert(oi);
- else
- renderables.insert(&r);
-}
-
-void Scene::remove(const Renderable &r)
-{
- if(const ObjectInstance *oi = dynamic_cast<const ObjectInstance *>(&r))
- {
- ObjectMap::iterator i = objects.find(&oi->get_object());
- if(i!=objects.end())
- {
- i->second.erase(oi);
- if(i->second.empty())
- objects.erase(i);
- }
- }
- else
- renderables.erase(&r);
-}
-
-void Scene::render(const Tag &tag) const
-{
- for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
- i->first->render(i->second.begin(), i->second.end(), tag);
-
- for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
- (*i)->render(tag);
-}
-
-} // namespace GL
-} // namespace Msp
/* $Id$
This file is part of libmspgl
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+Copyright © 2010 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
#ifndef MSP_GL_SCENE_H_
#define MSP_GL_SCENE_H_
-#include <map>
-#include <set>
#include "renderable.h"
namespace Msp {
namespace GL {
-class Object;
-class ObjectInstance;
-
+/**
+Scenes are containers for other Renderables. This is a base class that can't
+be instantiated. At the moment the available Scene types are SimpleScene,
+InstancedScene and OrderedScene.
+*/
class Scene: public Renderable
{
-private:
- typedef std::set<const ObjectInstance *> InstanceSet;
- typedef std::map<const Object *, InstanceSet> ObjectMap;
- typedef std::set<const Renderable *> RenderableSet;
-
- ObjectMap objects;
- RenderableSet renderables;
-
+protected:
+ Scene() { }
public:
- void add(const Renderable &);
- void remove(const Renderable &);
+ virtual ~Scene() { }
- virtual void render(const Tag &tag = Tag()) const;
+ virtual void add(const Renderable &) = 0;
+ virtual void remove(const Renderable &) = 0;
};
} // namespace GL
--- /dev/null
+/* $Id$
+
+This file is part of libmspgl
+Copyright © 2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include "simplescene.h"
+
+namespace Msp {
+namespace GL {
+
+void SimpleScene::add(const Renderable &r)
+{
+ renderables.insert(&r);
+}
+
+void SimpleScene::remove(const Renderable &r)
+{
+ renderables.erase(&r);
+}
+
+void SimpleScene::render(const Tag &tag) const
+{
+ for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
+ (*i)->render(tag);
+}
+
+} // namespace GL
+} // namespace Msp
--- /dev/null
+/* $Id$
+
+This file is part of libmspgl
+Copyright © 2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#ifndef MSP_GL_SIMPLESCENE_H_
+#define MSP_GL_SIMPLESCENE_H_
+
+#include <set>
+#include "scene.h"
+
+namespace Msp {
+namespace GL {
+
+/**
+The simplest possible Scene. Rendering order is unspecified.
+*/
+class SimpleScene: public Scene
+{
+private:
+ typedef std::set<const Renderable *> RenderableSet;
+
+ RenderableSet renderables;
+
+public:
+ virtual void add(const Renderable &);
+ virtual void remove(const Renderable &);
+
+ virtual void render(const Tag &tag = Tag()) const;
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif