21 static bool proc_node(MetaScene *mscn, struct ts_node *node);
22 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node);
23 static bool proc_mtledit(MaterialEdit *med, struct ts_node *node, TextureSet *texset);
24 static void apply_mtledit(Scene *scn, const MaterialEdit &med);
25 static void apply_mtledit(Material *mtl, const MaterialEdit &med);
26 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name);
28 static void print_scene_graph(SceneNode *n, int level);
31 MetaScene::MetaScene(SceneSet *sman, TextureSet *tman)
38 MetaScene::~MetaScene()
44 bool MetaScene::load(const char *fname)
46 struct ts_node *root = ts_load(fname);
47 if(!root || strcmp(root->name, "scene") != 0) {
49 error_log("failed to load scene metadata: %s\n", fname);
53 bool res = proc_node(this, root);
56 /*info_log("loaded scene: %s\n", fname);
57 info_log("scene graph:\n");
58 print_scene_graph(scn->nodes, 0);
63 void MetaScene::update(float dt)
65 int nscn = scenes.size();
66 for(int i=0; i<nscn; i++) {
67 scenes[i]->update(dt);
71 void MetaScene::draw() const
73 int nscn = scenes.size();
74 for(int i=0; i<nscn; i++) {
79 static bool proc_node(MetaScene *mscn, struct ts_node *node)
81 struct ts_node *c = node->child_list;
83 if(!proc_node(mscn, c)) {
89 // do this last to allow other contents of the node to do their thing
90 if(strcmp(node->name, "scenefile") == 0) {
91 return proc_scenefile(mscn, node);
93 } else if(strcmp(node->name, "remap") == 0) {
94 const char *match = ts_get_attr_str(node, "match");
95 const char *replace = ts_get_attr_str(node, "replace");
96 if(match && replace) {
97 datamap_map(match, replace);
107 std::string walkmesh_regexp;
108 std::vector<MaterialEdit> mtledit;
111 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node)
113 const char *fname = ts_get_attr_str(node, "file");
115 SceneData *sdat = new SceneData;
118 struct ts_attr *adpath = attr_inscope(node, "datapath");
119 if(adpath && adpath->val.type == TS_STRING) {
120 info_log("adding data path: %s\n", adpath->val.str);
121 datamap_set_path(adpath->val.str);
125 struct ts_attr *awmesh = attr_inscope(node, "walkmesh");
126 if(awmesh && awmesh->val.type == TS_STRING) {
127 sdat->walkmesh_regexp = std::string(awmesh->val.str);
130 int namesz = datamap_lookup(fname, 0, 0);
131 char *namebuf = (char*)alloca(namesz + 1);
132 if(datamap_lookup(fname, namebuf, namesz + 1)) {
137 struct ts_node *child = node->child_list;
140 if(proc_mtledit(&medit, child, mscn->texman)) {
141 sdat->mtledit.push_back(medit);
146 Scene *newscn = mscn->sceneman->get(fname);
147 /* NOTE: setting this after get() is not a race condition, because
148 * scene_loaded() which uses this, will only run in our main loop during
149 * SceneSet::update() on the main thread.
151 newscn->metascn = mscn;
152 mscn->scndata[newscn] = sdat;
157 bool MetaScene::scene_loaded(Scene *newscn)
159 SceneData *sdat = (SceneData*)scndata[newscn];
161 error_log("MetaScene::scene_loaded called, but corresponding SceneData not found\n");
165 // extract the walk mesh if necessary
167 if(!sdat->walkmesh_regexp.empty() && (wscn = newscn->extract_nodes(sdat->walkmesh_regexp.c_str()))) {
168 // apply all transformations to the meshes
171 int nmeshes = wscn->meshes.size();
172 for(int i=0; i<nmeshes; i++) {
173 Mesh *m = wscn->meshes[i];
176 walk_mesh->append(*m);
179 wscn->remove_mesh(m); // to save it from destruction
186 int num_medits = sdat->mtledit.size();
187 for(int i=0; i<num_medits; i++) {
188 // perform material edits
189 apply_mtledit(newscn, sdat->mtledit[i]);
192 scenes.push_back(newscn);
196 static bool proc_mtledit(MaterialEdit *med, struct ts_node *node, TextureSet *texset)
198 if(strcmp(node->name, "mtledit") != 0) {
202 const char *restr = ".*";
203 struct ts_attr *amtl = ts_get_attr(node, "material");
204 if(amtl && amtl->val.type == TS_STRING) {
205 restr = amtl->val.str;
208 med->name_re = std::regex(restr);
210 node = node->child_list;
212 struct ts_node *cn = node;
215 if(strcmp(cn->name, "texture") == 0) {
216 // add/change/remove a texture
217 struct ts_attr *atype = ts_get_attr(cn, "type");
218 struct ts_attr *afile = ts_get_attr(cn, "file");
220 int textype = MTL_TEX_DIFFUSE;
222 if(atype->val.type == TS_STRING) {
223 textype = mtl_parse_type(atype->val.str);
224 } else if(atype->val.type == TS_NUMBER) {
225 textype = atype->val.inum;
226 if(textype < 0 || textype >= NUM_MTL_TEXTURES) {
227 error_log("invalid texture in mtledit: %d\n", textype);
231 error_log("unexpected texture type in mtledit: %s\n", atype->val.str);
238 if(!afile || !afile->val.str || !*afile->val.str) {
242 med->tex = texset->get_texture(afile->val.str, TEX_2D);
245 // TODO add more edit modes
251 static void apply_mtledit(Scene *scn, const MaterialEdit &med)
253 // search all the objects to find matching material names
254 int nobj = scn->objects.size();
255 for(int i=0; i<nobj; i++) {
256 Object *obj = scn->objects[i];
257 if(std::regex_match(obj->get_name(), med.name_re)) {
258 apply_mtledit(&obj->mtl, med);
263 static void apply_mtledit(Material *mtl, const MaterialEdit &med)
265 // TODO more edit modes...
267 mtl->add_texture(med.tex, med.attr);
269 Texture *tex = mtl->stdtex[med.attr];
271 mtl->remove_texture(tex);
276 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
278 struct ts_attr *attr = 0;
280 while(node && !(attr = ts_get_attr(node, name))) {
286 static void print_scene_graph(SceneNode *n, int level)
290 for(int i=0; i<level; i++) {
294 int nobj = n->get_num_objects();
296 info_log("%s - %d obj\n", n->get_name(), n->get_num_objects());
298 info_log("%s\n", n->get_name());
301 for(int i=0; i<n->get_num_children(); i++) {
302 print_scene_graph(n->get_child(i), level + 1);