X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=laserbrain_demo;a=blobdiff_plain;f=src%2Fmetascene.cc;h=9c882495c25d282d163b9f3d5fd571c3c60e2ee1;hp=6059664a66b51b2b08844504665fda608986fee9;hb=d47a663825bd358d2165c1a4b040cc828aeb4991;hpb=5d44043a65106ff411abeaaaa80c732a47284024 diff --git a/src/metascene.cc b/src/metascene.cc index 6059664..9c88249 100644 --- a/src/metascene.cc +++ b/src/metascene.cc @@ -1,3 +1,13 @@ +/*! \file + * \brief Metascene implementation + * + * Loading starts at `MetaScene::load`, which calls `ts_load` (libtreestore) + * to load the metascene description tree into memory. Then `proc_node` is + * called at the root to recursively process the tree. `scenefile` nodes are + * handed over to `proc_scenefile`, which will trigger scene loading for any + * nodes with a `file` attribute. Scene loading is handled by requesting the + * filename from the scene resource manager, which is of type [SceneSet](\ref SceneSet). + */ #include #include #include @@ -6,22 +16,40 @@ #include "treestore.h" #include "logger.h" #include "app.h" +#include "dbg_gui.h" -#ifdef WIN32 +#if defined(WIN32) || defined(__WIN32__) #include #else #include #endif -struct MaterialEdit { - std::regex name_re; +enum MaterialEditType { + MTL_EDIT_TEXTURE, + MTL_EDIT_MIRROR +}; + +struct MaterialEditTexture { int attr; Texture *tex; }; +struct MaterialEditMirror { + float reflectivity; +}; + +struct MaterialEdit { + std::regex name_re; + MaterialEditType type; + + MaterialEditTexture tex; + MaterialEditMirror mirror; +}; + static bool proc_node(MetaScene *mscn, struct ts_node *node); static bool proc_scenefile(MetaScene *mscn, struct ts_node *node); static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node); +static bool proc_music(MetaScene *mscn, struct ts_node *node); static void apply_mtledit(Scene *scn, const MaterialEdit &med); static void apply_mtledit(Material *mtl, const MaterialEdit &med); static struct ts_attr *attr_inscope(struct ts_node *node, const char *name); @@ -32,14 +60,15 @@ static void print_scene_graph(SceneNode *n, int level); MetaScene::MetaScene() { walk_mesh = 0; + music = 0; } MetaScene::~MetaScene() { delete walk_mesh; + delete music; } - bool MetaScene::load(const char *fname) { struct ts_node *root = ts_load(fname); @@ -61,12 +90,48 @@ bool MetaScene::load(const char *fname) void MetaScene::update(float dt) { + bool expanded; + static char text[256]; + if(debug_gui) { + ImGui::Begin("MetaScene nodes", 0, 0); + ImGui::Columns(2); + + static bool once; + if(!once) { + float x = ImGui::GetColumnOffset(1); + ImGui::SetColumnOffset(1, x * 1.7); + once = true; + } + } + int nscn = scenes.size(); for(int i=0; iname.empty()) { + sprintf(text, "scene %3d", i); + } else { + sprintf(text, "scene %3d: %s", i, scenes[i]->name.c_str()); + } + expanded = parent_expanded = ImGui::TreeNode(text); + ImGui::NextColumn(); + ImGui::NextColumn(); + } + scenes[i]->update(dt); + + if(debug_gui && expanded) { + ImGui::TreePop(); + } + } + + if(debug_gui) { + ImGui::Columns(1); + ImGui::End(); } } +// XXX not used, renderer draws void MetaScene::draw() const { int nscn = scenes.size(); @@ -75,6 +140,57 @@ void MetaScene::draw() const } } +SceneNode *MetaScene::find_node(const char *name) const +{ + int num = scenes.size(); + for(int i=0; ifind_node(name); + if(n) return n; + } + return 0; +} + +SceneNode *MetaScene::match_node(const char *qstr) const +{ + int num = scenes.size(); + for(int i=0; imatch_node(qstr); + if(n) return n; + } + return 0; +} + +std::list MetaScene::match_nodes(const char *qstr) const +{ + std::list res; + int num = scenes.size(); + for(int i=0; i tmp = scenes[i]->match_nodes(qstr); + if(!tmp.empty()) { + res.splice(res.end(), tmp); + } + } + return std::move(res); +} + +Scene *MetaScene::extract_nodes(const char *qstr) +{ + Scene *scn = 0; + int nscn = scenes.size(); + for(int i=0; iextract_nodes(qstr); + if(tmp) { + if(!scn) { + scn = tmp; + } else { + scn->merge(tmp); + delete tmp; + } + } + } + return scn; +} + static bool proc_node(MetaScene *mscn, struct ts_node *node) { struct ts_node *c = node->child_list; @@ -95,6 +211,9 @@ static bool proc_node(MetaScene *mscn, struct ts_node *node) if(match && replace) { mscn->datamap.map(match, replace); } + + } else if(strcmp(node->name, "music") == 0) { + return proc_music(mscn, node); } return true; @@ -108,6 +227,9 @@ struct SceneData { std::vector mtledit; }; +/*! Processes a `scenefile` node. And kicks off scene loading (if necessary) by + * calling `SceneSet::get`. + */ static bool proc_scenefile(MetaScene *mscn, struct ts_node *node) { const char *fname = ts_get_attr_str(node, "file"); @@ -161,7 +283,7 @@ static bool proc_scenefile(MetaScene *mscn, struct ts_node *node) fname = namebuf; } - // material edits + // material edits are kept in a list to be applied when the scene has been loaded struct ts_node *child = node->child_list; while(child) { MaterialEdit medit; @@ -259,7 +381,6 @@ static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *nod if(amtl && amtl->val.type == TS_STRING) { restr = amtl->val.str; } - med->name_re = std::regex(restr); node = node->child_list; @@ -288,16 +409,37 @@ static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *nod } } - med->attr = textype; + med->tex.attr = textype; if(!afile || !afile->val.str || !*afile->val.str) { // remove - med->tex = 0; + med->tex.tex = 0; } else { - med->tex = texman.get_texture(afile->val.str, TEX_2D, &mscn->datamap); + med->tex.tex = texman.get_texture(afile->val.str, TEX_2D, &mscn->datamap); } + + med->type = MTL_EDIT_TEXTURE; + break; + } + + if(strcmp(cn->name, "mirror") == 0) { + // make this object a flat mirror (hopefully the object is flat otherwise this won't work) + float refl = 1.0f; + + struct ts_attr *arefl = ts_get_attr(cn, "reflect"); + if(arefl) { + if(arefl->val.type == TS_NUMBER) { + refl = arefl->val.fnum; + } else { + error_log("invalid reflect attribute in mirror mtledit: %s\n", arefl->val.str); + continue; + } + } + + med->type = MTL_EDIT_MIRROR; + med->mirror.reflectivity = refl; + break; } - // TODO add more edit modes } return true; @@ -315,16 +457,56 @@ static void apply_mtledit(Scene *scn, const MaterialEdit &med) } } +static bool proc_music(MetaScene *mscn, struct ts_node *node) +{ + const char *fname = ts_get_attr_str(node, "file"); + if(fname) { + SceneData *sdat = new SceneData; + sdat->meta = mscn; + + // datapath + struct ts_attr *adpath = attr_inscope(node, "datapath"); + if(adpath && adpath->val.type == TS_STRING) { + mscn->datamap.set_path(adpath->val.str); + } + + int namesz = mscn->datamap.lookup(fname, 0, 0); + char *namebuf = (char*)alloca(namesz + 1); + if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) { + fname = namebuf; + } + + OggVorbisStream *ovstream = new OggVorbisStream; + if(!ovstream->open(fname)) { + delete ovstream; + return false; + } + + delete mscn->music; + mscn->music = ovstream; + } + return true; +} + static void apply_mtledit(Material *mtl, const MaterialEdit &med) { // TODO more edit modes... - if(med.tex) { - mtl->add_texture(med.tex, med.attr); - } else { - Texture *tex = mtl->stdtex[med.attr]; - if(tex) { - mtl->remove_texture(tex); + switch(med.type) { + case MTL_EDIT_TEXTURE: + if(med.tex.tex) { + mtl->add_texture(med.tex.tex, med.tex.attr); + } else { + Texture *tex = mtl->stdtex[med.tex.attr]; + if(tex) { + mtl->remove_texture(tex); + } } + break; + + case MTL_EDIT_MIRROR: + mtl->flat_mirror = med.mirror.reflectivity > 1e-6; + mtl->reflect = med.mirror.reflectivity; + break; } }