20 static bool proc_node(MetaScene *mscn, struct ts_node *node);
21 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node);
22 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node, TextureSet *texset);
23 static void apply_mtledit(Scene *scn, const MaterialEdit &med);
24 static void apply_mtledit(Material *mtl, const MaterialEdit &med);
25 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name);
27 static void print_scene_graph(SceneNode *n, int level);
30 MetaScene::MetaScene(SceneSet *sman, TextureSet *tman)
37 MetaScene::~MetaScene()
43 bool MetaScene::load(const char *fname)
45 struct ts_node *root = ts_load(fname);
46 if(!root || strcmp(root->name, "scene") != 0) {
48 error_log("failed to load scene metadata: %s\n", fname);
52 bool res = proc_node(this, root);
55 /*info_log("loaded scene: %s\n", fname);
56 info_log("scene graph:\n");
57 print_scene_graph(scn->nodes, 0);
62 void MetaScene::update(float dt)
64 int nscn = scenes.size();
65 for(int i=0; i<nscn; i++) {
66 scenes[i]->update(dt);
70 void MetaScene::draw() const
72 int nscn = scenes.size();
73 for(int i=0; i<nscn; i++) {
78 static bool proc_node(MetaScene *mscn, struct ts_node *node)
80 struct ts_node *c = node->child_list;
82 if(!proc_node(mscn, c)) {
88 // do this last to allow other contents of the node to do their thing
89 if(strcmp(node->name, "scenefile") == 0) {
90 return proc_scenefile(mscn, node);
92 } else if(strcmp(node->name, "remap") == 0) {
93 const char *match = ts_get_attr_str(node, "match");
94 const char *replace = ts_get_attr_str(node, "replace");
95 if(match && replace) {
96 mscn->datamap.map(match, replace);
106 std::string walkmesh_regexp;
107 std::vector<MaterialEdit> mtledit;
110 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node)
112 const char *fname = ts_get_attr_str(node, "file");
114 SceneData *sdat = new SceneData;
117 struct ts_attr *adpath = attr_inscope(node, "datapath");
118 if(adpath && adpath->val.type == TS_STRING) {
119 info_log("adding data path: %s\n", adpath->val.str);
120 mscn->datamap.set_path(adpath->val.str);
124 struct ts_attr *awmesh = attr_inscope(node, "walkmesh");
125 if(awmesh && awmesh->val.type == TS_STRING) {
126 sdat->walkmesh_regexp = std::string(awmesh->val.str);
129 int namesz = mscn->datamap.lookup(fname, 0, 0);
130 char *namebuf = (char*)alloca(namesz + 1);
131 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
136 struct ts_node *child = node->child_list;
139 if(proc_mtledit(mscn, &medit, child, mscn->texman)) {
140 sdat->mtledit.push_back(medit);
145 Scene *newscn = mscn->sceneman->get(fname);
146 /* NOTE: setting all these after get() is not a race condition, because
147 * scene_loaded() which uses this, will only run in our main loop during
148 * SceneSet::update() on the main thread.
150 newscn->datamap = mscn->datamap;
151 mscn->datamap.clear();
153 newscn->metascn = mscn;
154 mscn->scndata[newscn] = sdat;
159 bool MetaScene::scene_loaded(Scene *newscn)
161 SceneData *sdat = (SceneData*)scndata[newscn];
163 error_log("MetaScene::scene_loaded called, but corresponding SceneData not found\n");
167 // extract the walk mesh if necessary
169 if(!sdat->walkmesh_regexp.empty() && (wscn = newscn->extract_nodes(sdat->walkmesh_regexp.c_str()))) {
170 // apply all transformations to the meshes
173 int nmeshes = wscn->meshes.size();
174 for(int i=0; i<nmeshes; i++) {
175 Mesh *m = wscn->meshes[i];
178 walk_mesh->append(*m);
181 wscn->remove_mesh(m); // to save it from destruction
188 int num_medits = sdat->mtledit.size();
189 for(int i=0; i<num_medits; i++) {
190 // perform material edits
191 apply_mtledit(newscn, sdat->mtledit[i]);
194 scenes.push_back(newscn);
198 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node, TextureSet *texset)
200 if(strcmp(node->name, "mtledit") != 0) {
204 const char *restr = ".*";
205 struct ts_attr *amtl = ts_get_attr(node, "material");
206 if(amtl && amtl->val.type == TS_STRING) {
207 restr = amtl->val.str;
210 med->name_re = std::regex(restr);
212 node = node->child_list;
214 struct ts_node *cn = node;
217 if(strcmp(cn->name, "texture") == 0) {
218 // add/change/remove a texture
219 struct ts_attr *atype = ts_get_attr(cn, "type");
220 struct ts_attr *afile = ts_get_attr(cn, "file");
222 int textype = MTL_TEX_DIFFUSE;
224 if(atype->val.type == TS_STRING) {
225 textype = mtl_parse_type(atype->val.str);
226 } else if(atype->val.type == TS_NUMBER) {
227 textype = atype->val.inum;
228 if(textype < 0 || textype >= NUM_MTL_TEXTURES) {
229 error_log("invalid texture in mtledit: %d\n", textype);
233 error_log("unexpected texture type in mtledit: %s\n", atype->val.str);
240 if(!afile || !afile->val.str || !*afile->val.str) {
244 med->tex = texset->get_texture(afile->val.str, TEX_2D, &mscn->datamap);
247 // TODO add more edit modes
253 static void apply_mtledit(Scene *scn, const MaterialEdit &med)
255 // search all the objects to find matching material names
256 int nobj = scn->objects.size();
257 for(int i=0; i<nobj; i++) {
258 Object *obj = scn->objects[i];
259 if(std::regex_match(obj->get_name(), med.name_re)) {
260 apply_mtledit(&obj->mtl, med);
265 static void apply_mtledit(Material *mtl, const MaterialEdit &med)
267 // TODO more edit modes...
269 mtl->add_texture(med.tex, med.attr);
271 Texture *tex = mtl->stdtex[med.attr];
273 mtl->remove_texture(tex);
278 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
280 struct ts_attr *attr = 0;
282 while(node && !(attr = ts_get_attr(node, name))) {
288 static void print_scene_graph(SceneNode *n, int level)
292 for(int i=0; i<level; i++) {
296 int nobj = n->get_num_objects();
298 info_log("%s - %d obj\n", n->get_name(), n->get_num_objects());
300 info_log("%s\n", n->get_name());
303 for(int i=0; i<n->get_num_children(); i++) {
304 print_scene_graph(n->get_child(i), level + 1);