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(MetaScene *mscn, MaterialEdit *med, struct ts_node *node);
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()
36 MetaScene::~MetaScene()
42 bool MetaScene::load(const char *fname)
44 struct ts_node *root = ts_load(fname);
45 if(!root || strcmp(root->name, "scene") != 0) {
47 error_log("failed to load scene metadata: %s\n", fname);
51 bool res = proc_node(this, root);
54 /*info_log("loaded scene: %s\n", fname);
55 info_log("scene graph:\n");
56 print_scene_graph(scn->nodes, 0);
61 void MetaScene::update(float dt)
63 int nscn = scenes.size();
64 for(int i=0; i<nscn; i++) {
65 scenes[i]->update(dt);
69 void MetaScene::draw() const
71 int nscn = scenes.size();
72 for(int i=0; i<nscn; i++) {
77 static bool proc_node(MetaScene *mscn, struct ts_node *node)
79 struct ts_node *c = node->child_list;
81 if(!proc_node(mscn, c)) {
87 // do this last to allow other contents of the node to do their thing
88 if(strcmp(node->name, "scenefile") == 0) {
89 return proc_scenefile(mscn, node);
91 } else if(strcmp(node->name, "remap") == 0) {
92 const char *match = ts_get_attr_str(node, "match");
93 const char *replace = ts_get_attr_str(node, "replace");
94 if(match && replace) {
95 mscn->datamap.map(match, replace);
105 std::string walkmesh_regexp;
106 std::vector<MaterialEdit> mtledit;
109 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node)
111 const char *fname = ts_get_attr_str(node, "file");
113 SceneData *sdat = new SceneData;
116 struct ts_attr *adpath = attr_inscope(node, "datapath");
117 if(adpath && adpath->val.type == TS_STRING) {
118 info_log("adding data path: %s\n", adpath->val.str);
119 mscn->datamap.set_path(adpath->val.str);
123 struct ts_attr *awmesh = attr_inscope(node, "walkmesh");
124 if(awmesh && awmesh->val.type == TS_STRING) {
125 sdat->walkmesh_regexp = std::string(awmesh->val.str);
128 int namesz = mscn->datamap.lookup(fname, 0, 0);
129 char *namebuf = (char*)alloca(namesz + 1);
130 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
135 struct ts_node *child = node->child_list;
138 if(proc_mtledit(mscn, &medit, child)) {
139 sdat->mtledit.push_back(medit);
144 Scene *newscn = sceneman.get(fname);
145 /* NOTE: setting all these after get() is not a race condition, because
146 * scene_loaded() which uses this, will only run in our main loop during
147 * SceneSet::update() on the main thread.
149 newscn->datamap = mscn->datamap;
150 mscn->datamap.clear();
152 newscn->metascn = mscn;
153 mscn->scndata[newscn] = sdat;
158 bool MetaScene::scene_loaded(Scene *newscn)
160 SceneData *sdat = (SceneData*)scndata[newscn];
162 error_log("MetaScene::scene_loaded called, but corresponding SceneData not found\n");
166 // extract the walk mesh if necessary
168 if(!sdat->walkmesh_regexp.empty() && (wscn = newscn->extract_nodes(sdat->walkmesh_regexp.c_str()))) {
169 // apply all transformations to the meshes
172 int nmeshes = wscn->meshes.size();
173 for(int i=0; i<nmeshes; i++) {
174 Mesh *m = wscn->meshes[i];
177 walk_mesh->append(*m);
180 wscn->remove_mesh(m); // to save it from destruction
187 int num_medits = sdat->mtledit.size();
188 for(int i=0; i<num_medits; i++) {
189 // perform material edits
190 apply_mtledit(newscn, sdat->mtledit[i]);
193 scenes.push_back(newscn);
197 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node)
199 if(strcmp(node->name, "mtledit") != 0) {
203 const char *restr = ".*";
204 struct ts_attr *amtl = ts_get_attr(node, "material");
205 if(amtl && amtl->val.type == TS_STRING) {
206 restr = amtl->val.str;
209 med->name_re = std::regex(restr);
211 node = node->child_list;
213 struct ts_node *cn = node;
216 if(strcmp(cn->name, "texture") == 0) {
217 // add/change/remove a texture
218 struct ts_attr *atype = ts_get_attr(cn, "type");
219 struct ts_attr *afile = ts_get_attr(cn, "file");
221 int textype = MTL_TEX_DIFFUSE;
223 if(atype->val.type == TS_STRING) {
224 textype = mtl_parse_type(atype->val.str);
225 } else if(atype->val.type == TS_NUMBER) {
226 textype = atype->val.inum;
227 if(textype < 0 || textype >= NUM_MTL_TEXTURES) {
228 error_log("invalid texture in mtledit: %d\n", textype);
232 error_log("unexpected texture type in mtledit: %s\n", atype->val.str);
239 if(!afile || !afile->val.str || !*afile->val.str) {
243 med->tex = texman.get_texture(afile->val.str, TEX_2D, &mscn->datamap);
246 // TODO add more edit modes
252 static void apply_mtledit(Scene *scn, const MaterialEdit &med)
254 // search all the objects to find matching material names
255 int nobj = scn->objects.size();
256 for(int i=0; i<nobj; i++) {
257 Object *obj = scn->objects[i];
258 if(std::regex_match(obj->get_name(), med.name_re)) {
259 apply_mtledit(&obj->mtl, med);
264 static void apply_mtledit(Material *mtl, const MaterialEdit &med)
266 // TODO more edit modes...
268 mtl->add_texture(med.tex, med.attr);
270 Texture *tex = mtl->stdtex[med.attr];
272 mtl->remove_texture(tex);
277 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
279 struct ts_attr *attr = 0;
281 while(node && !(attr = ts_get_attr(node, name))) {
287 static void print_scene_graph(SceneNode *n, int level)
291 for(int i=0; i<level; i++) {
295 int nobj = n->get_num_objects();
297 info_log("%s - %d obj\n", n->get_name(), n->get_num_objects());
299 info_log("%s\n", n->get_name());
302 for(int i=0; i<n->get_num_children(); i++) {
303 print_scene_graph(n->get_child(i), level + 1);