- shaders for both lightmapped objects with or without albedo maps
[laserbrain_demo] / src / metascene.cc
1 #include "metascene.h"
2 #include "scene.h"
3 #include "datamap.h"
4 #include "treestore.h"
5 #include "logger.h"
6
7 #ifdef WIN32
8 #include <malloc.h>
9 #else
10 #include <alloca.h>
11 #endif
12
13 static bool proc_node(Scene *scn, struct ts_node *node);
14 static bool proc_scenefile(Scene *scn, struct ts_node *node);
15 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name);
16
17 static void print_scene_graph(SceneNode *n, int level);
18
19 bool load_scene(Scene *scn, const char *fname)
20 {
21         struct ts_node *root = ts_load(fname);
22         if(!root || strcmp(root->name, "scene") != 0) {
23                 ts_free_tree(root);
24                 error_log("failed to load scene metadata: %s\n", fname);
25                 return false;
26         }
27
28         bool res = proc_node(scn, root);
29         ts_free_tree(root);
30
31         info_log("loaded scene: %s\n", fname);
32         info_log("scene graph:\n");
33         print_scene_graph(scn->nodes, 0);
34         return res;
35 }
36
37 static bool proc_node(Scene *scn, struct ts_node *node)
38 {
39         struct ts_node *c = node->child_list;
40         while(c) {
41                 if(!proc_node(scn, c)) {
42                         return false;
43                 }
44                 c = c->next;
45         }
46
47         // do this last to allow other contents of the node to do their thing
48         if(strcmp(node->name, "scenefile") == 0) {
49                 return proc_scenefile(scn, node);
50
51         } else if(strcmp(node->name, "remap") == 0) {
52                 const char *match = ts_get_attr_str(node, "match");
53                 const char *replace = ts_get_attr_str(node, "replace");
54                 if(match && replace) {
55                         datamap_map(match, replace);
56                 }
57         }
58
59         return true;
60 }
61
62 static bool proc_scenefile(Scene *scn, struct ts_node *node)
63 {
64         const char *fname = ts_get_attr_str(node, "file");
65         if(fname) {
66                 // datapath
67                 struct ts_attr *adpath = attr_inscope(node, "datapath");
68                 if(adpath && adpath->val.type == TS_STRING) {
69                         info_log("adding data path: %s\n", adpath->val.str);
70                         datamap_set_path(adpath->val.str);
71                 }
72
73                 // walkmesh
74                 char *walkmesh_regexp = 0;
75                 struct ts_attr *awmesh = attr_inscope(node, "walkmesh");
76                 if(awmesh && awmesh->val.type == TS_STRING) {
77                         walkmesh_regexp = awmesh->val.str;
78                 }
79
80                 int namesz = datamap_lookup(fname, 0, 0);
81                 char *namebuf = (char*)alloca(namesz + 1);
82                 if(datamap_lookup(fname, namebuf, namesz + 1)) {
83                         fname = namebuf;
84                 }
85
86                 if(!(scn->load(fname, SCNLOAD_FLIPTEX))) {
87                         return false;
88                 }
89
90                 // extract the walk mesh if necessary
91                 // XXX as written, this will match even objects loaded by previous scenefile blocks
92                 Scene *wscn;
93                 if(walkmesh_regexp && (wscn = scn->extract_nodes(walkmesh_regexp))) {
94                         // apply all transformations to the meshes
95                         wscn->apply_xform();
96
97                         int nmeshes = wscn->meshes.size();
98                         for(int i=0; i<nmeshes; i++) {
99                                 Mesh *m = wscn->meshes[i];
100
101                                 if(scn->walk_mesh) {
102                                         scn->walk_mesh->append(*m);
103                                 } else {
104                                         scn->walk_mesh = m;
105                                         wscn->remove_mesh(m);   // to save it from destruction
106                                 }
107                         }
108
109                         delete wscn;
110                 }
111         }
112         datamap_reset();
113
114         return true;
115 }
116
117 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
118 {
119         struct ts_attr *attr = 0;
120
121         while(node && !(attr = ts_get_attr(node, name))) {
122                 node = node->parent;
123         }
124         return attr;
125 }
126
127 static void print_scene_graph(SceneNode *n, int level)
128 {
129         if(!n) return;
130
131         for(int i=0; i<level; i++) {
132                 info_log("  ");
133         }
134
135         int nobj = n->get_num_objects();
136         if(nobj) {
137                 info_log("%s - %d obj\n", n->get_name(), n->get_num_objects());
138         } else {
139                 info_log("%s\n", n->get_name());
140         }
141
142         for(int i=0; i<n->get_num_children(); i++) {
143                 print_scene_graph(n->get_child(i), level + 1);
144         }
145 }