10 #if defined(WIN32) || defined(__WIN32__)
22 static bool proc_node(MetaScene *mscn, struct ts_node *node);
23 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node);
24 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node);
25 static bool proc_music(MetaScene *mscn, struct ts_node *node);
26 static void apply_mtledit(Scene *scn, const MaterialEdit &med);
27 static void apply_mtledit(Material *mtl, const MaterialEdit &med);
28 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name);
30 static void print_scene_graph(SceneNode *n, int level);
33 MetaScene::MetaScene()
39 MetaScene::~MetaScene()
46 bool MetaScene::load(const char *fname)
48 struct ts_node *root = ts_load(fname);
49 if(!root || strcmp(root->name, "scene") != 0) {
51 error_log("failed to load scene metadata: %s\n", fname);
55 bool res = proc_node(this, root);
58 /*info_log("loaded scene: %s\n", fname);
59 info_log("scene graph:\n");
60 print_scene_graph(scn->nodes, 0);
65 void MetaScene::update(float dt)
67 int nscn = scenes.size();
68 for(int i=0; i<nscn; i++) {
69 scenes[i]->update(dt);
73 void MetaScene::draw() const
75 int nscn = scenes.size();
76 for(int i=0; i<nscn; i++) {
81 SceneNode *MetaScene::find_node(const char *name) const
83 int num = scenes.size();
84 for(int i=0; i<num; i++) {
85 SceneNode *n = scenes[i]->find_node(name);
91 SceneNode *MetaScene::match_node(const char *qstr) const
93 int num = scenes.size();
94 for(int i=0; i<num; i++) {
95 SceneNode *n = scenes[i]->match_node(qstr);
101 std::list<SceneNode*> MetaScene::match_nodes(const char *qstr) const
103 std::list<SceneNode*> res;
104 int num = scenes.size();
105 for(int i=0; i<num; i++) {
106 std::list<SceneNode*> tmp = scenes[i]->match_nodes(qstr);
108 res.splice(res.end(), tmp);
111 return std::move(res);
114 Scene *MetaScene::extract_nodes(const char *qstr)
117 int nscn = scenes.size();
118 for(int i=0; i<nscn; i++) {
119 Scene *tmp = scenes[i]->extract_nodes(qstr);
132 static bool proc_node(MetaScene *mscn, struct ts_node *node)
134 struct ts_node *c = node->child_list;
136 if(!proc_node(mscn, c)) {
142 // do this last to allow other contents of the node to do their thing
143 if(strcmp(node->name, "scenefile") == 0) {
144 return proc_scenefile(mscn, node);
146 } else if(strcmp(node->name, "remap") == 0) {
147 const char *match = ts_get_attr_str(node, "match");
148 const char *replace = ts_get_attr_str(node, "replace");
149 if(match && replace) {
150 mscn->datamap.map(match, replace);
153 } else if(strcmp(node->name, "music") == 0) {
154 return proc_music(mscn, node);
164 std::string walkmesh_regexp, spawn_regexp;
165 std::vector<MaterialEdit> mtledit;
168 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node)
170 const char *fname = ts_get_attr_str(node, "file");
172 SceneData *sdat = new SceneData;
176 struct ts_attr *adpath = attr_inscope(node, "datapath");
177 if(adpath && adpath->val.type == TS_STRING) {
178 mscn->datamap.set_path(adpath->val.str);
182 struct ts_attr *aspath = attr_inscope(node, "strip_path");
183 if(aspath && aspath->val.type == TS_NUMBER) {
184 mscn->datamap.set_strip(aspath->val.inum);
188 struct ts_attr *awmesh = attr_inscope(node, "walkmesh");
189 if(awmesh && awmesh->val.type == TS_STRING) {
190 sdat->walkmesh_regexp = std::string(awmesh->val.str);
194 struct ts_attr *awspawn = attr_inscope(node, "spawn");
196 switch(awspawn->val.type) {
198 mscn->start_pos = Vec3(awspawn->val.vec[0], awspawn->val.vec[1],
199 awspawn->val.vec[2]);
204 sdat->spawn_regexp = std::string(awspawn->val.str);
207 if((awspawn = attr_inscope(node, "spawn_rot")) && awspawn->val.type == TS_VECTOR) {
209 rot.rotate(Vec3(1, 0, 0), deg_to_rad(awspawn->val.vec[0]));
210 rot.rotate(Vec3(0, 1, 0), deg_to_rad(awspawn->val.vec[1]));
211 rot.rotate(Vec3(0, 0, 1), deg_to_rad(awspawn->val.vec[2]));
212 mscn->start_rot = rot;
215 int namesz = mscn->datamap.lookup(fname, 0, 0);
216 char *namebuf = (char*)alloca(namesz + 1);
217 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
222 struct ts_node *child = node->child_list;
225 if(proc_mtledit(mscn, &medit, child)) {
226 sdat->mtledit.push_back(medit);
231 Scene *newscn = sceneman.get(fname);
232 /* NOTE: setting all these after get() is not a race condition, because
233 * scene_loaded() which uses this, will only run in our main loop during
234 * SceneSet::update() on the main thread.
236 newscn->datamap = mscn->datamap;
237 mscn->datamap.clear();
239 newscn->metascn = mscn;
240 mscn->scndata[newscn] = sdat;
245 bool MetaScene::scene_loaded(Scene *newscn)
247 SceneData *sdat = (SceneData*)scndata[newscn];
249 error_log("MetaScene::scene_loaded called, but corresponding SceneData not found\n");
253 // extract the walk mesh if necessary
255 if(!sdat->walkmesh_regexp.empty() && (wscn = newscn->extract_nodes(sdat->walkmesh_regexp.c_str()))) {
256 // apply all transformations to the meshes
259 int nmeshes = wscn->meshes.size();
260 for(int i=0; i<nmeshes; i++) {
261 Mesh *m = wscn->meshes[i];
264 walk_mesh->append(*m);
267 wscn->remove_mesh(m); // to save it from destruction
274 // extract the spawn node
275 if(!sdat->spawn_regexp.empty() && (wscn = newscn->extract_nodes(sdat->spawn_regexp.c_str()))) {
277 int nmeshes = wscn->meshes.size();
278 int nnodes = wscn->nodes ? wscn->nodes->get_num_children() : 0;
282 for(int i=0; i<nmeshes; i++) {
283 const Sphere &bsph = wscn->meshes[i]->get_bsphere();
286 pos /= (float)nmeshes;
287 sdat->meta->start_pos = pos;
290 // just use the first one
291 SceneNode *first = wscn->nodes->get_child(0);
292 sdat->meta->start_pos = first->get_position();
293 sdat->meta->start_rot = first->get_rotation();
298 int num_medits = sdat->mtledit.size();
299 for(int i=0; i<num_medits; i++) {
300 // perform material edits
301 apply_mtledit(newscn, sdat->mtledit[i]);
304 scenes.push_back(newscn);
308 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node)
310 if(strcmp(node->name, "mtledit") != 0) {
314 const char *restr = ".*";
315 struct ts_attr *amtl = ts_get_attr(node, "material");
316 if(amtl && amtl->val.type == TS_STRING) {
317 restr = amtl->val.str;
320 med->name_re = std::regex(restr);
322 node = node->child_list;
324 struct ts_node *cn = node;
327 if(strcmp(cn->name, "texture") == 0) {
328 // add/change/remove a texture
329 struct ts_attr *atype = ts_get_attr(cn, "type");
330 struct ts_attr *afile = ts_get_attr(cn, "file");
332 int textype = MTL_TEX_DIFFUSE;
334 if(atype->val.type == TS_STRING) {
335 textype = mtl_parse_type(atype->val.str);
336 } else if(atype->val.type == TS_NUMBER) {
337 textype = atype->val.inum;
338 if(textype < 0 || textype >= NUM_MTL_TEXTURES) {
339 error_log("invalid texture in mtledit: %d\n", textype);
343 error_log("unexpected texture type in mtledit: %s\n", atype->val.str);
350 if(!afile || !afile->val.str || !*afile->val.str) {
354 med->tex = texman.get_texture(afile->val.str, TEX_2D, &mscn->datamap);
357 // TODO add more edit modes
363 static void apply_mtledit(Scene *scn, const MaterialEdit &med)
365 // search all the objects to find matching material names
366 int nobj = scn->objects.size();
367 for(int i=0; i<nobj; i++) {
368 Object *obj = scn->objects[i];
369 if(std::regex_match(obj->mtl.name, med.name_re)) {
370 apply_mtledit(&obj->mtl, med);
375 static bool proc_music(MetaScene *mscn, struct ts_node *node)
377 const char *fname = ts_get_attr_str(node, "file");
379 SceneData *sdat = new SceneData;
383 struct ts_attr *adpath = attr_inscope(node, "datapath");
384 if(adpath && adpath->val.type == TS_STRING) {
385 mscn->datamap.set_path(adpath->val.str);
388 int namesz = mscn->datamap.lookup(fname, 0, 0);
389 char *namebuf = (char*)alloca(namesz + 1);
390 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
394 OggVorbisStream *ovstream = new OggVorbisStream;
395 if(!ovstream->open(fname)) {
401 mscn->music = ovstream;
406 static void apply_mtledit(Material *mtl, const MaterialEdit &med)
408 // TODO more edit modes...
410 mtl->add_texture(med.tex, med.attr);
412 Texture *tex = mtl->stdtex[med.attr];
414 mtl->remove_texture(tex);
419 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
421 struct ts_attr *attr = 0;
423 while(node && !(attr = ts_get_attr(node, name))) {
429 static void print_scene_graph(SceneNode *n, int level)
433 for(int i=0; i<level; i++) {
437 int nobj = n->get_num_objects();
439 info_log("%s - %d obj\n", n->get_name(), n->get_num_objects());
441 info_log("%s\n", n->get_name());
444 for(int i=0; i<n->get_num_children(); i++) {
445 print_scene_graph(n->get_child(i), level + 1);