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);
106 std::string walkmesh_regexp, spawn_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;
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 mscn->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);
131 struct ts_attr *awspawn = attr_inscope(node, "spawn");
133 switch(awspawn->val.type) {
135 mscn->start_pos = Vec3(awspawn->val.vec[0], awspawn->val.vec[1],
136 awspawn->val.vec[2]);
141 sdat->spawn_regexp = std::string(awspawn->val.str);
144 if((awspawn = attr_inscope(node, "spawn_rot")) && awspawn->val.type == TS_VECTOR) {
146 rot.rotate(Vec3(1, 0, 0), deg_to_rad(awspawn->val.vec[0]));
147 rot.rotate(Vec3(0, 1, 0), deg_to_rad(awspawn->val.vec[1]));
148 rot.rotate(Vec3(0, 0, 1), deg_to_rad(awspawn->val.vec[2]));
149 mscn->start_rot = rot;
152 int namesz = mscn->datamap.lookup(fname, 0, 0);
153 char *namebuf = (char*)alloca(namesz + 1);
154 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
159 struct ts_node *child = node->child_list;
162 if(proc_mtledit(mscn, &medit, child)) {
163 sdat->mtledit.push_back(medit);
168 Scene *newscn = sceneman.get(fname);
169 /* NOTE: setting all these after get() is not a race condition, because
170 * scene_loaded() which uses this, will only run in our main loop during
171 * SceneSet::update() on the main thread.
173 newscn->datamap = mscn->datamap;
174 mscn->datamap.clear();
176 newscn->metascn = mscn;
177 mscn->scndata[newscn] = sdat;
182 bool MetaScene::scene_loaded(Scene *newscn)
184 SceneData *sdat = (SceneData*)scndata[newscn];
186 error_log("MetaScene::scene_loaded called, but corresponding SceneData not found\n");
190 // extract the walk mesh if necessary
192 if(!sdat->walkmesh_regexp.empty() && (wscn = newscn->extract_nodes(sdat->walkmesh_regexp.c_str()))) {
193 // apply all transformations to the meshes
196 int nmeshes = wscn->meshes.size();
197 for(int i=0; i<nmeshes; i++) {
198 Mesh *m = wscn->meshes[i];
201 walk_mesh->append(*m);
204 wscn->remove_mesh(m); // to save it from destruction
211 // extract the spawn node
212 if(!sdat->spawn_regexp.empty() && (wscn = newscn->extract_nodes(sdat->spawn_regexp.c_str()))) {
214 int nmeshes = wscn->meshes.size();
215 int nnodes = wscn->nodes ? wscn->nodes->get_num_children() : 0;
219 for(int i=0; i<nmeshes; i++) {
220 const Sphere &bsph = wscn->meshes[i]->get_bsphere();
223 pos /= (float)nmeshes;
224 sdat->meta->start_pos = pos;
227 // just use the first one
228 SceneNode *first = wscn->nodes->get_child(0);
229 sdat->meta->start_pos = first->get_position();
230 sdat->meta->start_rot = first->get_rotation();
235 int num_medits = sdat->mtledit.size();
236 for(int i=0; i<num_medits; i++) {
237 // perform material edits
238 apply_mtledit(newscn, sdat->mtledit[i]);
241 scenes.push_back(newscn);
245 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node)
247 if(strcmp(node->name, "mtledit") != 0) {
251 const char *restr = ".*";
252 struct ts_attr *amtl = ts_get_attr(node, "material");
253 if(amtl && amtl->val.type == TS_STRING) {
254 restr = amtl->val.str;
257 med->name_re = std::regex(restr);
259 node = node->child_list;
261 struct ts_node *cn = node;
264 if(strcmp(cn->name, "texture") == 0) {
265 // add/change/remove a texture
266 struct ts_attr *atype = ts_get_attr(cn, "type");
267 struct ts_attr *afile = ts_get_attr(cn, "file");
269 int textype = MTL_TEX_DIFFUSE;
271 if(atype->val.type == TS_STRING) {
272 textype = mtl_parse_type(atype->val.str);
273 } else if(atype->val.type == TS_NUMBER) {
274 textype = atype->val.inum;
275 if(textype < 0 || textype >= NUM_MTL_TEXTURES) {
276 error_log("invalid texture in mtledit: %d\n", textype);
280 error_log("unexpected texture type in mtledit: %s\n", atype->val.str);
287 if(!afile || !afile->val.str || !*afile->val.str) {
291 med->tex = texman.get_texture(afile->val.str, TEX_2D, &mscn->datamap);
294 // TODO add more edit modes
300 static void apply_mtledit(Scene *scn, const MaterialEdit &med)
302 // search all the objects to find matching material names
303 int nobj = scn->objects.size();
304 for(int i=0; i<nobj; i++) {
305 Object *obj = scn->objects[i];
306 if(std::regex_match(obj->get_name(), med.name_re)) {
307 apply_mtledit(&obj->mtl, med);
312 static void apply_mtledit(Material *mtl, const MaterialEdit &med)
314 // TODO more edit modes...
316 mtl->add_texture(med.tex, med.attr);
318 Texture *tex = mtl->stdtex[med.attr];
320 mtl->remove_texture(tex);
325 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
327 struct ts_attr *attr = 0;
329 while(node && !(attr = ts_get_attr(node, name))) {
335 static void print_scene_graph(SceneNode *n, int level)
339 for(int i=0; i<level; i++) {
343 int nobj = n->get_num_objects();
345 info_log("%s - %d obj\n", n->get_name(), n->get_num_objects());
347 info_log("%s\n", n->get_name());
350 for(int i=0; i<n->get_num_children(); i++) {
351 print_scene_graph(n->get_child(i), level + 1);