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 // assume the object is actually flat, so grab the first triangle and make a plane
227 Triangle face = Triangle(0, (const Vec3*)mesh->get_attrib_data(MESH_ATTR_VERTEX),
228 mesh->get_index_data());
231 FlatMirror *mir = new FlatMirror;
232 mir->plane.pt = face.v[0];
233 mir->plane.normal = face.normal;
234 mir->reflect = obj->mtl.reflect;
236 // check to see if we have found this mirror plane already
238 FlatMirror *node = mirrors;
240 if(fabs(dot(mir->plane.normal, node->plane.normal)) < 1e-4 &&
241 fabs(dot(mir->plane.normal, normalize(mir->plane.pt - node->plane.pt))) < 1e-4) {
251 objmirror[obj] = mir; // associate with object
263 static bool proc_node(MetaScene *mscn, struct ts_node *node)
265 struct ts_node *c = node->child_list;
267 if(!proc_node(mscn, c)) {
273 // do this last to allow other contents of the node to do their thing
274 if(strcmp(node->name, "scenefile") == 0) {
275 return proc_scenefile(mscn, node);
277 } else if(strcmp(node->name, "remap") == 0) {
278 const char *match = ts_get_attr_str(node, "match");
279 const char *replace = ts_get_attr_str(node, "replace");
280 if(match && replace) {
281 mscn->datamap.map(match, replace);
284 } else if(strcmp(node->name, "music") == 0) {
285 return proc_music(mscn, node);
295 std::string walkmesh_regexp, spawn_regexp;
296 std::vector<MaterialEdit> mtledit;
299 /*! Processes a `scenefile` node. And kicks off scene loading (if necessary) by
300 * calling `SceneSet::get`.
302 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node)
304 const char *fname = ts_get_attr_str(node, "file");
306 SceneData *sdat = new SceneData;
310 struct ts_attr *adpath = attr_inscope(node, "datapath");
311 if(adpath && adpath->val.type == TS_STRING) {
312 mscn->datamap.set_path(adpath->val.str);
316 struct ts_attr *aspath = attr_inscope(node, "strip_path");
317 if(aspath && aspath->val.type == TS_NUMBER) {
318 mscn->datamap.set_strip(aspath->val.inum);
322 struct ts_attr *awmesh = attr_inscope(node, "walkmesh");
323 if(awmesh && awmesh->val.type == TS_STRING) {
324 sdat->walkmesh_regexp = std::string(awmesh->val.str);
328 struct ts_attr *awspawn = attr_inscope(node, "spawn");
330 switch(awspawn->val.type) {
332 mscn->start_pos = Vec3(awspawn->val.vec[0], awspawn->val.vec[1],
333 awspawn->val.vec[2]);
338 sdat->spawn_regexp = std::string(awspawn->val.str);
341 if((awspawn = attr_inscope(node, "spawn_rot")) && awspawn->val.type == TS_VECTOR) {
343 rot.rotate(Vec3(1, 0, 0), deg_to_rad(awspawn->val.vec[0]));
344 rot.rotate(Vec3(0, 1, 0), deg_to_rad(awspawn->val.vec[1]));
345 rot.rotate(Vec3(0, 0, 1), deg_to_rad(awspawn->val.vec[2]));
346 mscn->start_rot = rot;
349 int namesz = mscn->datamap.lookup(fname, 0, 0);
350 char *namebuf = (char*)alloca(namesz + 1);
351 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
355 // material edits are kept in a list to be applied when the scene has been loaded
356 struct ts_node *child = node->child_list;
359 if(proc_mtledit(mscn, &medit, child)) {
360 sdat->mtledit.push_back(medit);
365 Scene *newscn = sceneman.get(fname);
366 /* NOTE: setting all these after get() is not a race condition, because
367 * scene_loaded() which uses this, will only run in our main loop during
368 * SceneSet::update() on the main thread.
370 newscn->datamap = mscn->datamap;
371 mscn->datamap.clear();
373 newscn->metascn = mscn;
374 mscn->scndata[newscn] = sdat;
379 bool MetaScene::scene_loaded(Scene *newscn)
381 SceneData *sdat = (SceneData*)scndata[newscn];
383 error_log("MetaScene::scene_loaded called, but corresponding SceneData not found\n");
387 // extract the walk mesh if necessary
389 if(!sdat->walkmesh_regexp.empty() && (wscn = newscn->extract_nodes(sdat->walkmesh_regexp.c_str()))) {
390 // apply all transformations to the meshes
393 int nmeshes = wscn->meshes.size();
394 for(int i=0; i<nmeshes; i++) {
395 Mesh *m = wscn->meshes[i];
398 walk_mesh->append(*m);
401 wscn->remove_mesh(m); // to save it from destruction
408 // extract the spawn node
409 if(!sdat->spawn_regexp.empty() && (wscn = newscn->extract_nodes(sdat->spawn_regexp.c_str()))) {
411 int nmeshes = wscn->meshes.size();
412 int nnodes = wscn->nodes ? wscn->nodes->get_num_children() : 0;
416 for(int i=0; i<nmeshes; i++) {
417 const Sphere &bsph = wscn->meshes[i]->get_bsphere();
420 pos /= (float)nmeshes;
421 sdat->meta->start_pos = pos;
424 // just use the first one
425 SceneNode *first = wscn->nodes->get_child(0);
426 sdat->meta->start_pos = first->get_position();
427 sdat->meta->start_rot = first->get_rotation();
432 int num_medits = sdat->mtledit.size();
433 for(int i=0; i<num_medits; i++) {
434 // perform material edits
435 apply_mtledit(newscn, sdat->mtledit[i]);
438 scenes.push_back(newscn);
442 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node)
444 if(strcmp(node->name, "mtledit") != 0) {
448 const char *restr = ".*";
449 struct ts_attr *amtl = ts_get_attr(node, "material");
450 if(amtl && amtl->val.type == TS_STRING) {
451 restr = amtl->val.str;
453 med->name_re = std::regex(restr);
455 node = node->child_list;
457 struct ts_node *cn = node;
460 if(strcmp(cn->name, "texture") == 0) {
461 // add/change/remove a texture
462 struct ts_attr *atype = ts_get_attr(cn, "type");
463 struct ts_attr *afile = ts_get_attr(cn, "file");
465 int textype = MTL_TEX_DIFFUSE;
467 if(atype->val.type == TS_STRING) {
468 textype = mtl_parse_type(atype->val.str);
469 } else if(atype->val.type == TS_NUMBER) {
470 textype = atype->val.inum;
471 if(textype < 0 || textype >= NUM_MTL_TEXTURES) {
472 error_log("invalid texture in mtledit: %d\n", textype);
476 error_log("unexpected texture type in mtledit: %s\n", atype->val.str);
481 med->tex.attr = textype;
483 if(!afile || !afile->val.str || !*afile->val.str) {
487 med->tex.tex = texman.get_texture(afile->val.str, TEX_2D, &mscn->datamap);
490 med->type = MTL_EDIT_TEXTURE;
494 if(strcmp(cn->name, "mirror") == 0) {
495 // make this object a flat mirror (hopefully the object is flat otherwise this won't work)
498 struct ts_attr *arefl = ts_get_attr(cn, "reflect");
500 if(arefl->val.type == TS_NUMBER) {
501 refl = arefl->val.fnum;
503 error_log("invalid reflect attribute in mirror mtledit: %s\n", arefl->val.str);
508 med->type = MTL_EDIT_MIRROR;
509 med->mirror.reflectivity = refl;
517 static void apply_mtledit(Scene *scn, const MaterialEdit &med)
519 // search all the objects to find matching material names
520 int nobj = scn->objects.size();
521 for(int i=0; i<nobj; i++) {
522 Object *obj = scn->objects[i];
523 if(std::regex_match(obj->mtl.name, med.name_re)) {
524 apply_mtledit(&obj->mtl, med);
529 static bool proc_music(MetaScene *mscn, struct ts_node *node)
531 const char *fname = ts_get_attr_str(node, "file");
533 SceneData *sdat = new SceneData;
537 struct ts_attr *adpath = attr_inscope(node, "datapath");
538 if(adpath && adpath->val.type == TS_STRING) {
539 mscn->datamap.set_path(adpath->val.str);
542 int namesz = mscn->datamap.lookup(fname, 0, 0);
543 char *namebuf = (char*)alloca(namesz + 1);
544 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
548 OggVorbisStream *ovstream = new OggVorbisStream;
549 if(!ovstream->open(fname)) {
555 mscn->music = ovstream;
560 static void apply_mtledit(Material *mtl, const MaterialEdit &med)
562 // TODO more edit modes...
564 case MTL_EDIT_TEXTURE:
566 mtl->add_texture(med.tex.tex, med.tex.attr);
568 Texture *tex = mtl->stdtex[med.tex.attr];
570 mtl->remove_texture(tex);
575 case MTL_EDIT_MIRROR:
576 mtl->flat_mirror = med.mirror.reflectivity > 1e-6;
577 mtl->reflect = med.mirror.reflectivity;
582 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
584 struct ts_attr *attr = 0;
586 while(node && !(attr = ts_get_attr(node, name))) {
592 static void print_scene_graph(SceneNode *n, int level)
596 for(int i=0; i<level; i++) {
600 int nobj = n->get_num_objects();
602 info_log("%s - %d obj\n", n->get_name(), n->get_num_objects());
604 info_log("%s\n", n->get_name());
607 for(int i=0; i<n->get_num_children(); i++) {
608 print_scene_graph(n->get_child(i), level + 1);