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"
21 #if defined(WIN32) || defined(__WIN32__)
33 static bool proc_node(MetaScene *mscn, struct ts_node *node);
34 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node);
35 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node);
36 static bool proc_music(MetaScene *mscn, struct ts_node *node);
37 static void apply_mtledit(Scene *scn, const MaterialEdit &med);
38 static void apply_mtledit(Material *mtl, const MaterialEdit &med);
39 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name);
41 static void print_scene_graph(SceneNode *n, int level);
44 MetaScene::MetaScene()
50 MetaScene::~MetaScene()
56 bool MetaScene::load(const char *fname)
58 struct ts_node *root = ts_load(fname);
59 if(!root || strcmp(root->name, "scene") != 0) {
61 error_log("failed to load scene metadata: %s\n", fname);
65 bool res = proc_node(this, root);
68 /*info_log("loaded scene: %s\n", fname);
69 info_log("scene graph:\n");
70 print_scene_graph(scn->nodes, 0);
75 void MetaScene::update(float dt)
78 static char text[256];
80 ImGui::Begin("MetaScene nodes", 0, 0);
85 float x = ImGui::GetColumnOffset(1);
86 ImGui::SetColumnOffset(1, x * 1.7);
91 int nscn = scenes.size();
92 for(int i=0; i<nscn; i++) {
95 if(scenes[i]->name.empty()) {
96 sprintf(text, "scene %3d", i);
98 sprintf(text, "scene %3d: %s", i, scenes[i]->name.c_str());
100 expanded = parent_expanded = ImGui::TreeNode(text);
105 scenes[i]->update(dt);
107 if(debug_gui && expanded) {
118 // XXX not used, renderer draws
119 void MetaScene::draw() const
121 int nscn = scenes.size();
122 for(int i=0; i<nscn; i++) {
127 SceneNode *MetaScene::find_node(const char *name) const
129 int num = scenes.size();
130 for(int i=0; i<num; i++) {
131 SceneNode *n = scenes[i]->find_node(name);
137 SceneNode *MetaScene::match_node(const char *qstr) const
139 int num = scenes.size();
140 for(int i=0; i<num; i++) {
141 SceneNode *n = scenes[i]->match_node(qstr);
147 std::list<SceneNode*> MetaScene::match_nodes(const char *qstr) const
149 std::list<SceneNode*> res;
150 int num = scenes.size();
151 for(int i=0; i<num; i++) {
152 std::list<SceneNode*> tmp = scenes[i]->match_nodes(qstr);
154 res.splice(res.end(), tmp);
157 return std::move(res);
160 Scene *MetaScene::extract_nodes(const char *qstr)
163 int nscn = scenes.size();
164 for(int i=0; i<nscn; i++) {
165 Scene *tmp = scenes[i]->extract_nodes(qstr);
178 static bool proc_node(MetaScene *mscn, struct ts_node *node)
180 struct ts_node *c = node->child_list;
182 if(!proc_node(mscn, c)) {
188 // do this last to allow other contents of the node to do their thing
189 if(strcmp(node->name, "scenefile") == 0) {
190 return proc_scenefile(mscn, node);
192 } else if(strcmp(node->name, "remap") == 0) {
193 const char *match = ts_get_attr_str(node, "match");
194 const char *replace = ts_get_attr_str(node, "replace");
195 if(match && replace) {
196 mscn->datamap.map(match, replace);
199 } else if(strcmp(node->name, "music") == 0) {
200 return proc_music(mscn, node);
210 std::string walkmesh_regexp, spawn_regexp;
211 std::vector<MaterialEdit> mtledit;
214 /*! Processes a `scenefile` node. And kicks off scene loading (if necessary) by
215 * calling `SceneSet::get`.
217 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node)
219 const char *fname = ts_get_attr_str(node, "file");
221 SceneData *sdat = new SceneData;
225 struct ts_attr *adpath = attr_inscope(node, "datapath");
226 if(adpath && adpath->val.type == TS_STRING) {
227 mscn->datamap.set_path(adpath->val.str);
231 struct ts_attr *aspath = attr_inscope(node, "strip_path");
232 if(aspath && aspath->val.type == TS_NUMBER) {
233 mscn->datamap.set_strip(aspath->val.inum);
237 struct ts_attr *awmesh = attr_inscope(node, "walkmesh");
238 if(awmesh && awmesh->val.type == TS_STRING) {
239 sdat->walkmesh_regexp = std::string(awmesh->val.str);
243 struct ts_attr *awspawn = attr_inscope(node, "spawn");
245 switch(awspawn->val.type) {
247 mscn->start_pos = Vec3(awspawn->val.vec[0], awspawn->val.vec[1],
248 awspawn->val.vec[2]);
253 sdat->spawn_regexp = std::string(awspawn->val.str);
256 if((awspawn = attr_inscope(node, "spawn_rot")) && awspawn->val.type == TS_VECTOR) {
258 rot.rotate(Vec3(1, 0, 0), deg_to_rad(awspawn->val.vec[0]));
259 rot.rotate(Vec3(0, 1, 0), deg_to_rad(awspawn->val.vec[1]));
260 rot.rotate(Vec3(0, 0, 1), deg_to_rad(awspawn->val.vec[2]));
261 mscn->start_rot = rot;
264 int namesz = mscn->datamap.lookup(fname, 0, 0);
265 char *namebuf = (char*)alloca(namesz + 1);
266 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
270 // material edits are kept in a list to be applied when the scene has been loaded
271 struct ts_node *child = node->child_list;
274 if(proc_mtledit(mscn, &medit, child)) {
275 sdat->mtledit.push_back(medit);
280 Scene *newscn = sceneman.get(fname);
281 /* NOTE: setting all these after get() is not a race condition, because
282 * scene_loaded() which uses this, will only run in our main loop during
283 * SceneSet::update() on the main thread.
285 newscn->datamap = mscn->datamap;
286 mscn->datamap.clear();
288 newscn->metascn = mscn;
289 mscn->scndata[newscn] = sdat;
294 bool MetaScene::scene_loaded(Scene *newscn)
296 SceneData *sdat = (SceneData*)scndata[newscn];
298 error_log("MetaScene::scene_loaded called, but corresponding SceneData not found\n");
302 // extract the walk mesh if necessary
304 if(!sdat->walkmesh_regexp.empty() && (wscn = newscn->extract_nodes(sdat->walkmesh_regexp.c_str()))) {
305 // apply all transformations to the meshes
308 int nmeshes = wscn->meshes.size();
309 for(int i=0; i<nmeshes; i++) {
310 Mesh *m = wscn->meshes[i];
313 walk_mesh->append(*m);
316 wscn->remove_mesh(m); // to save it from destruction
323 // extract the spawn node
324 if(!sdat->spawn_regexp.empty() && (wscn = newscn->extract_nodes(sdat->spawn_regexp.c_str()))) {
326 int nmeshes = wscn->meshes.size();
327 int nnodes = wscn->nodes ? wscn->nodes->get_num_children() : 0;
331 for(int i=0; i<nmeshes; i++) {
332 const Sphere &bsph = wscn->meshes[i]->get_bsphere();
335 pos /= (float)nmeshes;
336 sdat->meta->start_pos = pos;
339 // just use the first one
340 SceneNode *first = wscn->nodes->get_child(0);
341 sdat->meta->start_pos = first->get_position();
342 sdat->meta->start_rot = first->get_rotation();
347 int num_medits = sdat->mtledit.size();
348 for(int i=0; i<num_medits; i++) {
349 // perform material edits
350 apply_mtledit(newscn, sdat->mtledit[i]);
353 scenes.push_back(newscn);
357 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node)
359 if(strcmp(node->name, "mtledit") != 0) {
363 const char *restr = ".*";
364 struct ts_attr *amtl = ts_get_attr(node, "material");
365 if(amtl && amtl->val.type == TS_STRING) {
366 restr = amtl->val.str;
369 med->name_re = std::regex(restr);
371 node = node->child_list;
373 struct ts_node *cn = node;
376 if(strcmp(cn->name, "texture") == 0) {
377 // add/change/remove a texture
378 struct ts_attr *atype = ts_get_attr(cn, "type");
379 struct ts_attr *afile = ts_get_attr(cn, "file");
381 int textype = MTL_TEX_DIFFUSE;
383 if(atype->val.type == TS_STRING) {
384 textype = mtl_parse_type(atype->val.str);
385 } else if(atype->val.type == TS_NUMBER) {
386 textype = atype->val.inum;
387 if(textype < 0 || textype >= NUM_MTL_TEXTURES) {
388 error_log("invalid texture in mtledit: %d\n", textype);
392 error_log("unexpected texture type in mtledit: %s\n", atype->val.str);
399 if(!afile || !afile->val.str || !*afile->val.str) {
403 med->tex = texman.get_texture(afile->val.str, TEX_2D, &mscn->datamap);
406 // TODO add more edit modes
412 static void apply_mtledit(Scene *scn, const MaterialEdit &med)
414 // search all the objects to find matching material names
415 int nobj = scn->objects.size();
416 for(int i=0; i<nobj; i++) {
417 Object *obj = scn->objects[i];
418 if(std::regex_match(obj->mtl.name, med.name_re)) {
419 apply_mtledit(&obj->mtl, med);
424 static bool proc_music(MetaScene *mscn, struct ts_node *node)
426 const char *fname = ts_get_attr_str(node, "file");
428 SceneData *sdat = new SceneData;
432 struct ts_attr *adpath = attr_inscope(node, "datapath");
433 if(adpath && adpath->val.type == TS_STRING) {
434 mscn->datamap.set_path(adpath->val.str);
437 int namesz = mscn->datamap.lookup(fname, 0, 0);
438 char *namebuf = (char*)alloca(namesz + 1);
439 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
443 OggVorbisStream *ovstream = new OggVorbisStream;
444 if(!ovstream->open(fname)) {
450 mscn->music = ovstream;
455 static void apply_mtledit(Material *mtl, const MaterialEdit &med)
457 // TODO more edit modes...
459 mtl->add_texture(med.tex, med.attr);
461 Texture *tex = mtl->stdtex[med.attr];
463 mtl->remove_texture(tex);
468 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
470 struct ts_attr *attr = 0;
472 while(node && !(attr = ts_get_attr(node, name))) {
478 static void print_scene_graph(SceneNode *n, int level)
482 for(int i=0; i<level; i++) {
486 int nobj = n->get_num_objects();
488 info_log("%s - %d obj\n", n->get_name(), n->get_num_objects());
490 info_log("%s\n", n->get_name());
493 for(int i=0; i<n->get_num_children(); i++) {
494 print_scene_graph(n->get_child(i), level + 1);