]> git.tdb.fi Git - libs/gl.git/blob - source/render/scene.h
305bf62aefd5a2ed3fb4748bf9258772643eb52b
[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         };
39
40 public:
41         class GenericLoader: public DataFile::Loader
42         {
43         private:
44                 template<typename T>
45                 struct CreateScene
46                 {
47                         void operator()(const std::string &, GenericLoader &) const;
48                 };
49
50                 DataFile::Collection &coll;
51                 Scene *scene;
52                 Loader *scene_loader;
53
54                 static ActionMap shared_actions;
55
56         public:
57                 GenericLoader(DataFile::Collection &);
58                 ~GenericLoader();
59
60                 Scene *get_scene() { Scene *s = scene; scene = 0; return s; }
61         private:
62                 virtual void init_actions();
63
64                 void type(const DataFile::Symbol &);
65
66                 friend class Scene;
67         };
68
69 private:
70         typedef TypeRegistry<GenericLoader::CreateScene, GenericLoader &> SceneRegistry;
71
72 protected:
73         // XXX If a loaded renderable is removed from the scene it needs to be removed from here as well
74         std::vector<Renderable *> owned_data;
75         mutable Matrix culling_matrix;
76         mutable Vector4 frustum_edges[6];
77
78         Scene() { }
79 private:
80         Scene(const Scene &);
81         Scene &operator=(const Scene &);
82 public:
83         virtual ~Scene();
84
85         virtual void add(Renderable &) = 0;
86         virtual void remove(Renderable &) = 0;
87
88 protected:
89         bool setup_frustum(const Renderer &) const;
90         bool frustum_cull(const Renderable &) const;
91
92 public:
93         template<typename T>
94         static void register_type(const std::string &);
95 private:
96         static SceneRegistry &get_scene_registry();
97 };
98
99 template<typename T>
100 void Scene::register_type(const std::string &kw)
101 {
102         get_scene_registry().register_type<T>(kw);
103 }
104
105 template<typename T>
106 void Scene::GenericLoader::CreateScene<T>::operator()(const std::string &, GenericLoader &ldr) const
107 {
108         if(ldr.scene)
109                 throw std::logic_error("Scene type was already specified");
110
111         T *scene = new T;
112         ldr.scene = scene;
113         ldr.scene_loader = new typename T::Loader(*scene, ldr.coll);
114         ldr.add_auxiliary_loader(*ldr.scene_loader);
115 }
116
117 } // namespace GL
118 } // namespace Msp
119
120 #endif