12 static bool proc_node(Scene *scn, struct ts_node *node);
13 static bool proc_scenefile(Scene *scn, struct ts_node *node);
14 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name);
16 static void print_scene_graph(SceneNode *n, int level);
18 bool load_scene(Scene *scn, const char *fname)
20 struct ts_node *root = ts_load(fname);
21 if(!root || strcmp(root->name, "scene") != 0) {
23 fprintf(stderr, "failed to load scene metadata: %s\n", fname);
27 bool res = proc_node(scn, root);
30 printf("loaded scene: %s\n", fname);
31 printf("scene graph:\n");
32 print_scene_graph(scn->nodes, 0);
36 static bool proc_node(Scene *scn, struct ts_node *node)
38 struct ts_node *c = node->child_list;
40 if(!proc_node(scn, c)) {
46 // do this last to allow other contents of the node to do their thing
47 if(strcmp(node->name, "scenefile") == 0) {
48 return proc_scenefile(scn, node);
50 } else if(strcmp(node->name, "remap") == 0) {
51 const char *match = ts_get_attr_str(node, "match");
52 const char *replace = ts_get_attr_str(node, "replace");
53 if(match && replace) {
54 datamap_map(match, replace);
61 static bool proc_scenefile(Scene *scn, struct ts_node *node)
63 const char *fname = ts_get_attr_str(node, "file");
66 struct ts_attr *adpath = attr_inscope(node, "datapath");
67 if(adpath && adpath->val.type == TS_STRING) {
68 printf("adding data path: %s\n", adpath->val.str);
69 datamap_set_path(adpath->val.str);
73 char *walkmesh_regexp = 0;
74 struct ts_attr *awmesh = attr_inscope(node, "walkmesh");
75 if(awmesh && awmesh->val.type == TS_STRING) {
76 walkmesh_regexp = awmesh->val.str;
79 int namesz = datamap_lookup(fname, 0, 0);
80 char *namebuf = (char*)alloca(namesz + 1);
81 if(datamap_lookup(fname, namebuf, namesz + 1)) {
85 if(!(scn->load(fname, SCNLOAD_FLIPTEX))) {
89 // extract the walk mesh if necessary
90 // XXX as written, this will match even objects loaded by previous scenefile blocks
92 if(walkmesh_regexp && (wscn = scn->extract_nodes(walkmesh_regexp))) {
93 // apply all transformations to the meshes
96 int nmeshes = wscn->meshes.size();
97 for(int i=0; i<nmeshes; i++) {
98 Mesh *m = wscn->meshes[i];
101 scn->walk_mesh->append(*m);
104 wscn->remove_mesh(m); // to save it from destruction
116 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
118 struct ts_attr *attr = 0;
120 while(node && !(attr = ts_get_attr(node, name))) {
126 static void print_scene_graph(SceneNode *n, int level)
130 for(int i=0; i<level; i++) {
134 int nobj = n->get_num_objects();
136 printf("%s - %d obj\n", n->get_name(), n->get_num_objects());
138 printf("%s\n", n->get_name());
141 for(int i=0; i<n->get_num_children(); i++) {
142 print_scene_graph(n->get_child(i), level + 1);