]> git.tdb.fi Git - libs/gl.git/blob - source/instancescene.cpp
Add a rendering supervisor class
[libs/gl.git] / source / instancescene.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007-2008, 2010-2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "object.h"
9 #include "objectinstance.h"
10 #include "instancescene.h"
11 #include "renderer.h"
12
13 namespace Msp {
14 namespace GL {
15
16 void InstanceScene::add(const Renderable &r)
17 {
18         if(const ObjectInstance *oi = dynamic_cast<const ObjectInstance *>(&r))
19                 objects[&oi->get_object()].insert(oi);
20         else
21                 renderables.insert(&r);
22 }
23
24 void InstanceScene::remove(const Renderable &r)
25 {
26         if(const ObjectInstance *oi = dynamic_cast<const ObjectInstance *>(&r))
27         {
28                 ObjectMap::iterator i = objects.find(&oi->get_object());
29                 if(i!=objects.end())
30                 {
31                         i->second.erase(oi);
32                         if(i->second.empty())
33                                 objects.erase(i);
34                 }
35         }
36         else
37                 renderables.erase(&r);
38 }
39
40 void InstanceScene::render(Renderer &renderer, const Tag &tag) const
41 {
42         // XXX Check that the object has this pass to avoid some unnecessary function calls
43         for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
44                 for(InstanceSet::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
45                         (*j)->render(renderer, tag);
46
47         for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
48                 (*i)->render(renderer, tag);
49 }
50
51 } // namespace GL
52 } // namespace Msp