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"
17 #include "treestore.h"
22 #if defined(WIN32) || defined(__WIN32__)
28 enum MaterialEditType {
33 struct MaterialEditTexture {
38 struct MaterialEditMirror {
44 MaterialEditType type;
46 MaterialEditTexture tex;
47 MaterialEditMirror mirror;
50 static bool proc_node(MetaScene *mscn, struct ts_node *node);
51 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node);
52 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node);
53 static bool proc_music(MetaScene *mscn, struct ts_node *node);
54 static void apply_mtledit(Scene *scn, const MaterialEdit &med);
55 static void apply_mtledit(Material *mtl, const MaterialEdit &med);
56 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name);
58 static void print_scene_graph(SceneNode *n, int level);
61 MetaScene::MetaScene()
68 MetaScene::~MetaScene()
74 FlatMirror *m = mirrors;
75 mirrors = mirrors->next;
80 bool MetaScene::load(const char *fname)
82 struct ts_node *root = ts_load(fname);
83 if(!root || strcmp(root->name, "scene") != 0) {
85 error_log("failed to load scene metadata: %s\n", fname);
89 bool res = proc_node(this, root);
92 /*info_log("loaded scene: %s\n", fname);
93 info_log("scene graph:\n");
94 print_scene_graph(scn->nodes, 0);
99 void MetaScene::update(float dt)
102 static char text[256];
104 ImGui::Begin("MetaScene nodes", 0, 0);
109 float x = ImGui::GetColumnOffset(1);
110 ImGui::SetColumnOffset(1, x * 1.7);
115 int nscn = scenes.size();
116 for(int i=0; i<nscn; i++) {
119 if(scenes[i]->name.empty()) {
120 sprintf(text, "scene %3d", i);
122 sprintf(text, "scene %3d: %s", i, scenes[i]->name.c_str());
124 expanded = parent_expanded = ImGui::TreeNode(text);
129 scenes[i]->update(dt);
131 if(debug_gui && expanded) {
142 // XXX not used, renderer draws
143 void MetaScene::draw() const
145 error_log("Don't call MetaScene::draw, use the Renderer\n");
146 int nscn = scenes.size();
147 for(int i=0; i<nscn; i++) {
152 SceneNode *MetaScene::find_node(const char *name) const
154 int num = scenes.size();
155 for(int i=0; i<num; i++) {
156 SceneNode *n = scenes[i]->find_node(name);
162 SceneNode *MetaScene::match_node(const char *qstr) const
164 int num = scenes.size();
165 for(int i=0; i<num; i++) {
166 SceneNode *n = scenes[i]->match_node(qstr);
172 std::list<SceneNode*> MetaScene::match_nodes(const char *qstr) const
174 std::list<SceneNode*> res;
175 int num = scenes.size();
176 for(int i=0; i<num; i++) {
177 std::list<SceneNode*> tmp = scenes[i]->match_nodes(qstr);
179 res.splice(res.end(), tmp);
182 return std::move(res);
185 Scene *MetaScene::extract_nodes(const char *qstr)
188 int nscn = scenes.size();
189 for(int i=0; i<nscn; i++) {
190 Scene *tmp = scenes[i]->extract_nodes(qstr);
203 int MetaScene::calc_mirror_planes()
207 FlatMirror *m = mirrors;
208 mirrors = mirrors->next;
214 int numscn = scenes.size();
215 for(int i=0; i<numscn; i++) {
216 Scene *scn = scenes[i];
218 int numobj = scn->objects.size();
219 for(int j=0; j<numobj; j++) {
220 Object *obj = scn->objects[j];
222 if(obj->mtl.flat_mirror && obj->get_type() == OBJ_MESH) {
223 const Mesh *mesh = ((ObjMesh*)obj)->mesh;
226 FlatMirror *mir = new FlatMirror;
227 mir->reflect = obj->mtl.reflect;
229 if(obj->mtl.flat_mirror == MTL_MIRROR_AUTO) {
230 // assume the object is actually flat, so grab the first triangle and make a plane
231 Triangle face = Triangle(0, (const Vec3*)mesh->get_attrib_data(MESH_ATTR_VERTEX),
232 mesh->get_index_data());
235 mir->plane.pt = face.v[0];
236 mir->plane.normal = face.normal;
240 // check to see if we have found this mirror plane already
242 FlatMirror *node = mirrors;
244 if(1.0f - dot(mir->plane.normal, node->plane.normal) < 1e-4f &&
245 fabs(dot(mir->plane.normal, normalize(mir->plane.pt - node->plane.pt))) < 1e-4) {
256 objmirror[obj] = mir; // associate with object
268 static bool proc_node(MetaScene *mscn, struct ts_node *node)
270 struct ts_node *c = node->child_list;
272 if(!proc_node(mscn, c)) {
278 // do this last to allow other contents of the node to do their thing
279 if(strcmp(node->name, "scenefile") == 0) {
280 return proc_scenefile(mscn, node);
282 } else if(strcmp(node->name, "remap") == 0) {
283 const char *match = ts_get_attr_str(node, "match");
284 const char *replace = ts_get_attr_str(node, "replace");
285 if(match && replace) {
286 mscn->datamap.map(match, replace);
289 } else if(strcmp(node->name, "music") == 0) {
290 return proc_music(mscn, node);
300 std::string walkmesh_regexp, spawn_regexp;
301 std::vector<MaterialEdit> mtledit;
304 /*! Processes a `scenefile` node. And kicks off scene loading (if necessary) by
305 * calling `SceneSet::get`.
307 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node)
309 const char *fname = ts_get_attr_str(node, "file");
311 SceneData *sdat = new SceneData;
315 struct ts_attr *adpath = attr_inscope(node, "datapath");
316 if(adpath && adpath->val.type == TS_STRING) {
317 mscn->datamap.set_path(adpath->val.str);
321 struct ts_attr *aspath = attr_inscope(node, "strip_path");
322 if(aspath && aspath->val.type == TS_NUMBER) {
323 mscn->datamap.set_strip(aspath->val.inum);
327 struct ts_attr *awmesh = attr_inscope(node, "walkmesh");
328 if(awmesh && awmesh->val.type == TS_STRING) {
329 sdat->walkmesh_regexp = std::string(awmesh->val.str);
333 struct ts_attr *awspawn = attr_inscope(node, "spawn");
335 switch(awspawn->val.type) {
337 mscn->start_pos = Vec3(awspawn->val.vec[0], awspawn->val.vec[1],
338 awspawn->val.vec[2]);
343 sdat->spawn_regexp = std::string(awspawn->val.str);
346 if((awspawn = attr_inscope(node, "spawn_rot")) && awspawn->val.type == TS_VECTOR) {
348 rot.rotate(Vec3(1, 0, 0), deg_to_rad(awspawn->val.vec[0]));
349 rot.rotate(Vec3(0, 1, 0), deg_to_rad(awspawn->val.vec[1]));
350 rot.rotate(Vec3(0, 0, 1), deg_to_rad(awspawn->val.vec[2]));
351 mscn->start_rot = rot;
354 int namesz = mscn->datamap.lookup(fname, 0, 0);
355 char *namebuf = (char*)alloca(namesz + 1);
356 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
360 // material edits are kept in a list to be applied when the scene has been loaded
361 struct ts_node *child = node->child_list;
364 if(proc_mtledit(mscn, &medit, child)) {
365 sdat->mtledit.push_back(medit);
370 Scene *newscn = sceneman.get(fname);
371 /* NOTE: setting all these after get() is not a race condition, because
372 * scene_loaded() which uses this, will only run in our main loop during
373 * SceneSet::update() on the main thread.
375 newscn->datamap = mscn->datamap;
376 mscn->datamap.clear();
378 newscn->metascn = mscn;
379 mscn->scndata[newscn] = sdat;
384 bool MetaScene::scene_loaded(Scene *newscn)
386 SceneData *sdat = (SceneData*)scndata[newscn];
388 error_log("MetaScene::scene_loaded called, but corresponding SceneData not found\n");
392 // extract the walk mesh if necessary
394 if(!sdat->walkmesh_regexp.empty() && (wscn = newscn->extract_nodes(sdat->walkmesh_regexp.c_str()))) {
395 // apply all transformations to the meshes
398 int nmeshes = wscn->meshes.size();
399 for(int i=0; i<nmeshes; i++) {
400 Mesh *m = wscn->meshes[i];
403 walk_mesh->append(*m);
406 wscn->remove_mesh(m); // to save it from destruction
413 // extract the spawn node
414 if(!sdat->spawn_regexp.empty() && (wscn = newscn->extract_nodes(sdat->spawn_regexp.c_str()))) {
416 int nmeshes = wscn->meshes.size();
417 int nnodes = wscn->nodes ? wscn->nodes->get_num_children() : 0;
421 for(int i=0; i<nmeshes; i++) {
422 const Sphere &bsph = wscn->meshes[i]->get_bsphere();
425 pos /= (float)nmeshes;
426 sdat->meta->start_pos = pos;
429 // just use the first one
430 SceneNode *first = wscn->nodes->get_child(0);
431 sdat->meta->start_pos = first->get_position();
432 sdat->meta->start_rot = first->get_rotation();
437 int num_medits = sdat->mtledit.size();
438 for(int i=0; i<num_medits; i++) {
439 // perform material edits
440 apply_mtledit(newscn, sdat->mtledit[i]);
443 scenes.push_back(newscn);
447 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node)
449 if(strcmp(node->name, "mtledit") != 0) {
453 const char *restr = ".*";
454 struct ts_attr *amtl = ts_get_attr(node, "material");
455 if(amtl && amtl->val.type == TS_STRING) {
456 restr = amtl->val.str;
458 med->name_re = std::regex(restr);
460 node = node->child_list;
462 struct ts_node *cn = node;
465 if(strcmp(cn->name, "texture") == 0) {
466 // add/change/remove a texture
467 struct ts_attr *atype = ts_get_attr(cn, "type");
468 struct ts_attr *afile = ts_get_attr(cn, "file");
470 int textype = MTL_TEX_DIFFUSE;
472 if(atype->val.type == TS_STRING) {
473 textype = mtl_parse_type(atype->val.str);
474 } else if(atype->val.type == TS_NUMBER) {
475 textype = atype->val.inum;
476 if(textype < 0 || textype >= NUM_MTL_TEXTURES) {
477 error_log("invalid texture in mtledit: %d\n", textype);
481 error_log("unexpected texture type in mtledit: %s\n", atype->val.str);
486 med->tex.attr = textype;
488 if(!afile || !afile->val.str || !*afile->val.str) {
492 med->tex.tex = texman.get_texture(afile->val.str, TEX_2D, &mscn->datamap);
495 med->type = MTL_EDIT_TEXTURE;
499 if(strcmp(cn->name, "mirror") == 0) {
500 // make this object a flat mirror (hopefully the object is flat otherwise this won't work)
503 struct ts_attr *arefl = ts_get_attr(cn, "reflect");
505 if(arefl->val.type == TS_NUMBER) {
506 refl = arefl->val.fnum;
508 error_log("invalid reflect attribute in mirror mtledit: %s\n", arefl->val.str);
513 med->type = MTL_EDIT_MIRROR;
514 med->mirror.reflectivity = refl;
522 static void apply_mtledit(Scene *scn, const MaterialEdit &med)
524 // search all the objects to find matching material names
525 int nobj = scn->objects.size();
526 for(int i=0; i<nobj; i++) {
527 Object *obj = scn->objects[i];
528 if(std::regex_match(obj->mtl.name, med.name_re)) {
529 apply_mtledit(&obj->mtl, med);
534 static bool proc_music(MetaScene *mscn, struct ts_node *node)
536 const char *fname = ts_get_attr_str(node, "file");
538 SceneData *sdat = new SceneData;
542 struct ts_attr *adpath = attr_inscope(node, "datapath");
543 if(adpath && adpath->val.type == TS_STRING) {
544 mscn->datamap.set_path(adpath->val.str);
547 int namesz = mscn->datamap.lookup(fname, 0, 0);
548 char *namebuf = (char*)alloca(namesz + 1);
549 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
553 OggVorbisStream *ovstream = new OggVorbisStream;
554 if(!ovstream->open(fname)) {
560 mscn->music = ovstream;
565 static void apply_mtledit(Material *mtl, const MaterialEdit &med)
567 // TODO more edit modes...
569 case MTL_EDIT_TEXTURE:
571 mtl->add_texture(med.tex.tex, med.tex.attr);
573 Texture *tex = mtl->stdtex[med.tex.attr];
575 mtl->remove_texture(tex);
580 case MTL_EDIT_MIRROR:
581 mtl->flat_mirror = med.mirror.reflectivity > 1e-6;
582 mtl->reflect = med.mirror.reflectivity;
587 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
589 struct ts_attr *attr = 0;
591 while(node && !(attr = ts_get_attr(node, name))) {
597 static void print_scene_graph(SceneNode *n, int level)
601 for(int i=0; i<level; i++) {
605 int nobj = n->get_num_objects();
607 info_log("%s - %d obj\n", n->get_name(), n->get_num_objects());
609 info_log("%s\n", n->get_name());
612 for(int i=0; i<n->get_num_children(); i++) {
613 print_scene_graph(n->get_child(i), level + 1);