rudimentary scenegraph treeview
[laserbrain_demo] / src / scene.h
1 #ifndef SCENE_H_
2 #define SCENE_H_
3
4 #include <vector>
5 #include <list>
6 #include <string>
7 #include "mesh.h"
8 #include "snode.h"
9 #include "texture.h"
10 #include "dataset.h"
11 #include "datamap.h"
12
13 enum {
14         SCNLOAD_FLIPYZ = 1,
15         SCNLOAD_FLIPTEX = 2,
16
17         SCNLOAD_STAGE_IO = 0x4000,
18         SCNLOAD_STAGE_GL = 0x8000
19 };
20
21 class MetaScene;
22
23 class Scene {
24 public:
25         std::string name;
26
27         MetaScene *metascn;
28         DataMap datamap;
29
30         // meshes objects and nodes are owned by Scene
31         std::vector<Mesh*> meshes;
32         std::vector<Object*> objects;
33         SceneNode *nodes;
34
35         Mesh *walk_mesh;
36
37         TextureSet *texset;
38         void *loader_data;
39
40         explicit Scene();
41         ~Scene();
42
43         Scene(const Scene &rhs) = delete;
44         Scene &operator =(const Scene &rhs) = delete;
45
46         void destroy();
47         void clear();   // clear all contents (meshes, objects, and nodes)
48
49         bool load(const char *fname, unsigned int flags = 0);
50
51         // merge scn into this scene, leaving scn empty and safe to destroy
52         bool merge(Scene *scn);
53
54         void add_object(Object *obj);
55         bool remove_object(Object *obj);
56         bool have_object(Object *obj) const;
57
58         void add_mesh(Mesh *m);
59         bool remove_mesh(Mesh *m);
60         bool have_mesh(Mesh *m) const;
61
62         void add_node(SceneNode *n);
63         bool remove_node(SceneNode *n);
64         bool have_node(SceneNode *n) const;
65
66         // find node by name
67         SceneNode *find_node(const char *name) const;
68         // match nodes with regexp and return the first that matches
69         SceneNode *match_node(const char *qstr) const;
70         // match nodes with regexp and return a list of matches
71         std::list<SceneNode*> match_nodes(const char *qstr) const;
72
73         /* find and remove all nodes whose names match the regexp
74          * XXX at the moment this has the effect of flattening the hierarchy in the
75          * result scene. If we ever need verbatim extraction of whole subtrees we'll
76          * need to change the algorithm.
77          */
78         Scene *extract_nodes(const char *qstr);
79
80         /* bake all node transformations to their respective meshes and make the
81          * node transformations identity.
82          * XXX this assumes no mesh is shared by two nodes.
83          */
84         void apply_xform();
85
86         void update(float dt);
87         void draw() const;
88 };
89
90 //! Resource manager for Scenes
91 class SceneSet : public DataSet<Scene*> {
92 private:
93         static Scene *create_scene();
94         static bool load_scene(Scene *scn, const char *fname);
95         static bool done_scene(Scene *scn);
96         static void free_scene(Scene *scn);
97
98 public:
99         SceneSet();
100 };
101
102 #endif  // SCENE_H_