]> git.tdb.fi Git - libs/gl.git/blob - source/render/scene.h
33678b7998900fac18f25cb113f181f5d654e9f3
[libs/gl.git] / source / render / scene.h
1 #ifndef MSP_GL_SCENE_H_
2 #define MSP_GL_SCENE_H_
3
4 #include <vector>
5 #include <msp/core/typeregistry.h>
6 #include <msp/datafile/objectloader.h>
7 #include "matrix.h"
8 #include "renderable.h"
9 #include "vector.h"
10
11 namespace Msp {
12 namespace GL {
13
14 /**
15 Scenes are containers for other Renderables.  This is a base class that can't
16 be instantiated.  Examples of available Scene types are SimpleScene,
17 InstancedScene and OrderedScene.
18 */
19 class Scene: public Renderable
20 {
21 protected:
22         class Loader: public DataFile::CollectionObjectLoader<Scene>
23         {
24         public:
25                 typedef std::map<std::string, Renderable *> ContentMap;
26
27         private:
28                 ContentMap *content;
29
30         public:
31                 Loader(Scene &, Collection &);
32                 Loader(Scene &, Collection &, ContentMap &);
33         private:
34                 void init();
35
36                 void object(const std::string &);
37                 void object_tagged(const std::string &, const std::string &);
38                 void scene(const std::string &);
39         };
40
41 public:
42         class GenericLoader: public DataFile::Loader
43         {
44         private:
45                 template<typename T>
46                 struct CreateScene
47                 {
48                         void operator()(const std::string &, GenericLoader &) const;
49                 };
50
51                 DataFile::Collection &coll;
52                 Scene *scene;
53                 Loader *scene_loader;
54
55                 static ActionMap shared_actions;
56
57         public:
58                 GenericLoader(DataFile::Collection &);
59                 ~GenericLoader();
60
61                 Scene *get_scene() { Scene *s = scene; scene = 0; return s; }
62         private:
63                 virtual void init_actions();
64
65                 void type(const DataFile::Symbol &);
66
67                 friend class Scene;
68         };
69
70 private:
71         typedef TypeRegistry<GenericLoader::CreateScene, GenericLoader &> SceneRegistry;
72
73 protected:
74         // XXX If a loaded renderable is removed from the scene it needs to be removed from here as well
75         std::vector<Renderable *> owned_data;
76         mutable Matrix culling_matrix;
77         mutable Vector4 frustum_edges[6];
78
79         Scene() { }
80 private:
81         Scene(const Scene &);
82         Scene &operator=(const Scene &);
83 public:
84         virtual ~Scene();
85
86         virtual void add(Renderable &) = 0;
87         virtual void remove(Renderable &) = 0;
88
89 protected:
90         bool setup_frustum(const Renderer &) const;
91         bool frustum_cull(const Renderable &) const;
92
93 public:
94         template<typename T>
95         static void register_type(const std::string &);
96 private:
97         static SceneRegistry &get_scene_registry();
98 };
99
100 template<typename T>
101 void Scene::register_type(const std::string &kw)
102 {
103         get_scene_registry().register_type<T>(kw);
104 }
105
106 template<typename T>
107 void Scene::GenericLoader::CreateScene<T>::operator()(const std::string &, GenericLoader &ldr) const
108 {
109         if(ldr.scene)
110                 throw std::logic_error("Scene type was already specified");
111
112         T *scene = new T;
113         ldr.scene = scene;
114         ldr.scene_loader = new typename T::Loader(*scene, ldr.coll);
115         ldr.add_auxiliary_loader(*ldr.scene_loader);
116 }
117
118 } // namespace GL
119 } // namespace Msp
120
121 #endif