2 * \brief Metascene implementation
4 * Loading starts at `MetaScene::load`, which calls `ts_load` (libtreestore)
5 * to load the metascene description tree into memory. Then `proc_node` is
6 * called at the root to recursively process the tree. `scenefile` nodes are
7 * handed over to `proc_scenefile`, which will trigger scene loading for any
8 * nodes with a `file` attribute. Scene loading is handled by requesting the
9 * filename from the scene resource manager, which is of type [SceneSet](\ref SceneSet).
14 #include "metascene.h"
16 #include "treestore.h"
20 #if defined(WIN32) || defined(__WIN32__)
32 static bool proc_node(MetaScene *mscn, struct ts_node *node);
33 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node);
34 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node);
35 static bool proc_music(MetaScene *mscn, struct ts_node *node);
36 static void apply_mtledit(Scene *scn, const MaterialEdit &med);
37 static void apply_mtledit(Material *mtl, const MaterialEdit &med);
38 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name);
40 static void print_scene_graph(SceneNode *n, int level);
43 MetaScene::MetaScene()
49 MetaScene::~MetaScene()
55 bool MetaScene::load(const char *fname)
57 struct ts_node *root = ts_load(fname);
58 if(!root || strcmp(root->name, "scene") != 0) {
60 error_log("failed to load scene metadata: %s\n", fname);
64 bool res = proc_node(this, root);
67 /*info_log("loaded scene: %s\n", fname);
68 info_log("scene graph:\n");
69 print_scene_graph(scn->nodes, 0);
74 void MetaScene::update(float dt)
76 int nscn = scenes.size();
77 for(int i=0; i<nscn; i++) {
78 scenes[i]->update(dt);
82 void MetaScene::draw() const
84 int nscn = scenes.size();
85 for(int i=0; i<nscn; i++) {
90 SceneNode *MetaScene::find_node(const char *name) const
92 int num = scenes.size();
93 for(int i=0; i<num; i++) {
94 SceneNode *n = scenes[i]->find_node(name);
100 SceneNode *MetaScene::match_node(const char *qstr) const
102 int num = scenes.size();
103 for(int i=0; i<num; i++) {
104 SceneNode *n = scenes[i]->match_node(qstr);
110 std::list<SceneNode*> MetaScene::match_nodes(const char *qstr) const
112 std::list<SceneNode*> res;
113 int num = scenes.size();
114 for(int i=0; i<num; i++) {
115 std::list<SceneNode*> tmp = scenes[i]->match_nodes(qstr);
117 res.splice(res.end(), tmp);
120 return std::move(res);
123 Scene *MetaScene::extract_nodes(const char *qstr)
126 int nscn = scenes.size();
127 for(int i=0; i<nscn; i++) {
128 Scene *tmp = scenes[i]->extract_nodes(qstr);
141 static bool proc_node(MetaScene *mscn, struct ts_node *node)
143 struct ts_node *c = node->child_list;
145 if(!proc_node(mscn, c)) {
151 // do this last to allow other contents of the node to do their thing
152 if(strcmp(node->name, "scenefile") == 0) {
153 return proc_scenefile(mscn, node);
155 } else if(strcmp(node->name, "remap") == 0) {
156 const char *match = ts_get_attr_str(node, "match");
157 const char *replace = ts_get_attr_str(node, "replace");
158 if(match && replace) {
159 mscn->datamap.map(match, replace);
162 } else if(strcmp(node->name, "music") == 0) {
163 return proc_music(mscn, node);
173 std::string walkmesh_regexp, spawn_regexp;
174 std::vector<MaterialEdit> mtledit;
177 /*! Processes a `scenefile` node. And kicks off scene loading (if necessary) by
178 * calling `SceneSet::get`.
180 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node)
182 const char *fname = ts_get_attr_str(node, "file");
184 SceneData *sdat = new SceneData;
188 struct ts_attr *adpath = attr_inscope(node, "datapath");
189 if(adpath && adpath->val.type == TS_STRING) {
190 mscn->datamap.set_path(adpath->val.str);
194 struct ts_attr *aspath = attr_inscope(node, "strip_path");
195 if(aspath && aspath->val.type == TS_NUMBER) {
196 mscn->datamap.set_strip(aspath->val.inum);
200 struct ts_attr *awmesh = attr_inscope(node, "walkmesh");
201 if(awmesh && awmesh->val.type == TS_STRING) {
202 sdat->walkmesh_regexp = std::string(awmesh->val.str);
206 struct ts_attr *awspawn = attr_inscope(node, "spawn");
208 switch(awspawn->val.type) {
210 mscn->start_pos = Vec3(awspawn->val.vec[0], awspawn->val.vec[1],
211 awspawn->val.vec[2]);
216 sdat->spawn_regexp = std::string(awspawn->val.str);
219 if((awspawn = attr_inscope(node, "spawn_rot")) && awspawn->val.type == TS_VECTOR) {
221 rot.rotate(Vec3(1, 0, 0), deg_to_rad(awspawn->val.vec[0]));
222 rot.rotate(Vec3(0, 1, 0), deg_to_rad(awspawn->val.vec[1]));
223 rot.rotate(Vec3(0, 0, 1), deg_to_rad(awspawn->val.vec[2]));
224 mscn->start_rot = rot;
227 int namesz = mscn->datamap.lookup(fname, 0, 0);
228 char *namebuf = (char*)alloca(namesz + 1);
229 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
233 // material edits are kept in a list to be applied when the scene has been loaded
234 struct ts_node *child = node->child_list;
237 if(proc_mtledit(mscn, &medit, child)) {
238 sdat->mtledit.push_back(medit);
243 Scene *newscn = sceneman.get(fname);
244 /* NOTE: setting all these after get() is not a race condition, because
245 * scene_loaded() which uses this, will only run in our main loop during
246 * SceneSet::update() on the main thread.
248 newscn->datamap = mscn->datamap;
249 mscn->datamap.clear();
251 newscn->metascn = mscn;
252 mscn->scndata[newscn] = sdat;
257 bool MetaScene::scene_loaded(Scene *newscn)
259 SceneData *sdat = (SceneData*)scndata[newscn];
261 error_log("MetaScene::scene_loaded called, but corresponding SceneData not found\n");
265 // extract the walk mesh if necessary
267 if(!sdat->walkmesh_regexp.empty() && (wscn = newscn->extract_nodes(sdat->walkmesh_regexp.c_str()))) {
268 // apply all transformations to the meshes
271 int nmeshes = wscn->meshes.size();
272 for(int i=0; i<nmeshes; i++) {
273 Mesh *m = wscn->meshes[i];
276 walk_mesh->append(*m);
279 wscn->remove_mesh(m); // to save it from destruction
286 // extract the spawn node
287 if(!sdat->spawn_regexp.empty() && (wscn = newscn->extract_nodes(sdat->spawn_regexp.c_str()))) {
289 int nmeshes = wscn->meshes.size();
290 int nnodes = wscn->nodes ? wscn->nodes->get_num_children() : 0;
294 for(int i=0; i<nmeshes; i++) {
295 const Sphere &bsph = wscn->meshes[i]->get_bsphere();
298 pos /= (float)nmeshes;
299 sdat->meta->start_pos = pos;
302 // just use the first one
303 SceneNode *first = wscn->nodes->get_child(0);
304 sdat->meta->start_pos = first->get_position();
305 sdat->meta->start_rot = first->get_rotation();
310 int num_medits = sdat->mtledit.size();
311 for(int i=0; i<num_medits; i++) {
312 // perform material edits
313 apply_mtledit(newscn, sdat->mtledit[i]);
316 scenes.push_back(newscn);
320 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node)
322 if(strcmp(node->name, "mtledit") != 0) {
326 const char *restr = ".*";
327 struct ts_attr *amtl = ts_get_attr(node, "material");
328 if(amtl && amtl->val.type == TS_STRING) {
329 restr = amtl->val.str;
332 med->name_re = std::regex(restr);
334 node = node->child_list;
336 struct ts_node *cn = node;
339 if(strcmp(cn->name, "texture") == 0) {
340 // add/change/remove a texture
341 struct ts_attr *atype = ts_get_attr(cn, "type");
342 struct ts_attr *afile = ts_get_attr(cn, "file");
344 int textype = MTL_TEX_DIFFUSE;
346 if(atype->val.type == TS_STRING) {
347 textype = mtl_parse_type(atype->val.str);
348 } else if(atype->val.type == TS_NUMBER) {
349 textype = atype->val.inum;
350 if(textype < 0 || textype >= NUM_MTL_TEXTURES) {
351 error_log("invalid texture in mtledit: %d\n", textype);
355 error_log("unexpected texture type in mtledit: %s\n", atype->val.str);
362 if(!afile || !afile->val.str || !*afile->val.str) {
366 med->tex = texman.get_texture(afile->val.str, TEX_2D, &mscn->datamap);
369 // TODO add more edit modes
375 static void apply_mtledit(Scene *scn, const MaterialEdit &med)
377 // search all the objects to find matching material names
378 int nobj = scn->objects.size();
379 for(int i=0; i<nobj; i++) {
380 Object *obj = scn->objects[i];
381 if(std::regex_match(obj->mtl.name, med.name_re)) {
382 apply_mtledit(&obj->mtl, med);
387 static bool proc_music(MetaScene *mscn, struct ts_node *node)
389 const char *fname = ts_get_attr_str(node, "file");
391 SceneData *sdat = new SceneData;
395 struct ts_attr *adpath = attr_inscope(node, "datapath");
396 if(adpath && adpath->val.type == TS_STRING) {
397 mscn->datamap.set_path(adpath->val.str);
400 int namesz = mscn->datamap.lookup(fname, 0, 0);
401 char *namebuf = (char*)alloca(namesz + 1);
402 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
406 OggVorbisStream *ovstream = new OggVorbisStream;
407 if(!ovstream->open(fname)) {
413 mscn->music = ovstream;
418 static void apply_mtledit(Material *mtl, const MaterialEdit &med)
420 // TODO more edit modes...
422 mtl->add_texture(med.tex, med.attr);
424 Texture *tex = mtl->stdtex[med.attr];
426 mtl->remove_texture(tex);
431 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
433 struct ts_attr *attr = 0;
435 while(node && !(attr = ts_get_attr(node, name))) {
441 static void print_scene_graph(SceneNode *n, int level)
445 for(int i=0; i<level; i++) {
449 int nobj = n->get_num_objects();
451 info_log("%s - %d obj\n", n->get_name(), n->get_num_objects());
453 info_log("%s\n", n->get_name());
456 for(int i=0; i<n->get_num_children(); i++) {
457 print_scene_graph(n->get_child(i), level + 1);