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 void apply_mtledit(Scene *scn, const MaterialEdit &med);
26 static void apply_mtledit(Material *mtl, const MaterialEdit &med);
27 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name);
29 static void print_scene_graph(SceneNode *n, int level);
32 MetaScene::MetaScene()
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 SceneNode *MetaScene::find_node(const char *name) const
80 int num = scenes.size();
81 for(int i=0; i<num; i++) {
82 SceneNode *n = scenes[i]->find_node(name);
88 SceneNode *MetaScene::match_node(const char *qstr) const
90 int num = scenes.size();
91 for(int i=0; i<num; i++) {
92 SceneNode *n = scenes[i]->match_node(qstr);
98 std::list<SceneNode*> MetaScene::match_nodes(const char *qstr) const
100 std::list<SceneNode*> res;
101 int num = scenes.size();
102 for(int i=0; i<num; i++) {
103 std::list<SceneNode*> tmp = scenes[i]->match_nodes(qstr);
105 res.splice(res.end(), tmp);
108 return std::move(res);
111 Scene *MetaScene::extract_nodes(const char *qstr)
114 int nscn = scenes.size();
115 for(int i=0; i<nscn; i++) {
116 Scene *tmp = scenes[i]->extract_nodes(qstr);
129 static bool proc_node(MetaScene *mscn, struct ts_node *node)
131 struct ts_node *c = node->child_list;
133 if(!proc_node(mscn, c)) {
139 // do this last to allow other contents of the node to do their thing
140 if(strcmp(node->name, "scenefile") == 0) {
141 return proc_scenefile(mscn, node);
143 } else if(strcmp(node->name, "remap") == 0) {
144 const char *match = ts_get_attr_str(node, "match");
145 const char *replace = ts_get_attr_str(node, "replace");
146 if(match && replace) {
147 mscn->datamap.map(match, replace);
158 std::string walkmesh_regexp, spawn_regexp;
159 std::vector<MaterialEdit> mtledit;
162 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node)
164 const char *fname = ts_get_attr_str(node, "file");
166 SceneData *sdat = new SceneData;
170 struct ts_attr *adpath = attr_inscope(node, "datapath");
171 if(adpath && adpath->val.type == TS_STRING) {
172 mscn->datamap.set_path(adpath->val.str);
176 struct ts_attr *aspath = attr_inscope(node, "strip_path");
177 if(aspath && aspath->val.type == TS_NUMBER) {
178 mscn->datamap.set_strip(aspath->val.inum);
182 struct ts_attr *awmesh = attr_inscope(node, "walkmesh");
183 if(awmesh && awmesh->val.type == TS_STRING) {
184 sdat->walkmesh_regexp = std::string(awmesh->val.str);
188 struct ts_attr *awspawn = attr_inscope(node, "spawn");
190 switch(awspawn->val.type) {
192 mscn->start_pos = Vec3(awspawn->val.vec[0], awspawn->val.vec[1],
193 awspawn->val.vec[2]);
198 sdat->spawn_regexp = std::string(awspawn->val.str);
201 if((awspawn = attr_inscope(node, "spawn_rot")) && awspawn->val.type == TS_VECTOR) {
203 rot.rotate(Vec3(1, 0, 0), deg_to_rad(awspawn->val.vec[0]));
204 rot.rotate(Vec3(0, 1, 0), deg_to_rad(awspawn->val.vec[1]));
205 rot.rotate(Vec3(0, 0, 1), deg_to_rad(awspawn->val.vec[2]));
206 mscn->start_rot = rot;
209 int namesz = mscn->datamap.lookup(fname, 0, 0);
210 char *namebuf = (char*)alloca(namesz + 1);
211 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
216 struct ts_node *child = node->child_list;
219 if(proc_mtledit(mscn, &medit, child)) {
220 sdat->mtledit.push_back(medit);
225 Scene *newscn = sceneman.get(fname);
226 /* NOTE: setting all these after get() is not a race condition, because
227 * scene_loaded() which uses this, will only run in our main loop during
228 * SceneSet::update() on the main thread.
230 newscn->datamap = mscn->datamap;
231 mscn->datamap.clear();
233 newscn->metascn = mscn;
234 mscn->scndata[newscn] = sdat;
239 bool MetaScene::scene_loaded(Scene *newscn)
241 SceneData *sdat = (SceneData*)scndata[newscn];
243 error_log("MetaScene::scene_loaded called, but corresponding SceneData not found\n");
247 // extract the walk mesh if necessary
249 if(!sdat->walkmesh_regexp.empty() && (wscn = newscn->extract_nodes(sdat->walkmesh_regexp.c_str()))) {
250 // apply all transformations to the meshes
253 int nmeshes = wscn->meshes.size();
254 for(int i=0; i<nmeshes; i++) {
255 Mesh *m = wscn->meshes[i];
258 walk_mesh->append(*m);
261 wscn->remove_mesh(m); // to save it from destruction
268 // extract the spawn node
269 if(!sdat->spawn_regexp.empty() && (wscn = newscn->extract_nodes(sdat->spawn_regexp.c_str()))) {
271 int nmeshes = wscn->meshes.size();
272 int nnodes = wscn->nodes ? wscn->nodes->get_num_children() : 0;
276 for(int i=0; i<nmeshes; i++) {
277 const Sphere &bsph = wscn->meshes[i]->get_bsphere();
280 pos /= (float)nmeshes;
281 sdat->meta->start_pos = pos;
284 // just use the first one
285 SceneNode *first = wscn->nodes->get_child(0);
286 sdat->meta->start_pos = first->get_position();
287 sdat->meta->start_rot = first->get_rotation();
292 int num_medits = sdat->mtledit.size();
293 for(int i=0; i<num_medits; i++) {
294 // perform material edits
295 apply_mtledit(newscn, sdat->mtledit[i]);
298 scenes.push_back(newscn);
302 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node)
304 if(strcmp(node->name, "mtledit") != 0) {
308 const char *restr = ".*";
309 struct ts_attr *amtl = ts_get_attr(node, "material");
310 if(amtl && amtl->val.type == TS_STRING) {
311 restr = amtl->val.str;
314 med->name_re = std::regex(restr);
316 node = node->child_list;
318 struct ts_node *cn = node;
321 if(strcmp(cn->name, "texture") == 0) {
322 // add/change/remove a texture
323 struct ts_attr *atype = ts_get_attr(cn, "type");
324 struct ts_attr *afile = ts_get_attr(cn, "file");
326 int textype = MTL_TEX_DIFFUSE;
328 if(atype->val.type == TS_STRING) {
329 textype = mtl_parse_type(atype->val.str);
330 } else if(atype->val.type == TS_NUMBER) {
331 textype = atype->val.inum;
332 if(textype < 0 || textype >= NUM_MTL_TEXTURES) {
333 error_log("invalid texture in mtledit: %d\n", textype);
337 error_log("unexpected texture type in mtledit: %s\n", atype->val.str);
344 if(!afile || !afile->val.str || !*afile->val.str) {
348 med->tex = texman.get_texture(afile->val.str, TEX_2D, &mscn->datamap);
351 // TODO add more edit modes
357 static void apply_mtledit(Scene *scn, const MaterialEdit &med)
359 // search all the objects to find matching material names
360 int nobj = scn->objects.size();
361 for(int i=0; i<nobj; i++) {
362 Object *obj = scn->objects[i];
363 if(std::regex_match(obj->mtl.name, med.name_re)) {
364 apply_mtledit(&obj->mtl, med);
369 static void apply_mtledit(Material *mtl, const MaterialEdit &med)
371 // TODO more edit modes...
373 mtl->add_texture(med.tex, med.attr);
375 Texture *tex = mtl->stdtex[med.attr];
377 mtl->remove_texture(tex);
382 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
384 struct ts_attr *attr = 0;
386 while(node && !(attr = ts_get_attr(node, name))) {
392 static void print_scene_graph(SceneNode *n, int level)
396 for(int i=0; i<level; i++) {
400 int nobj = n->get_num_objects();
402 info_log("%s - %d obj\n", n->get_name(), n->get_num_objects());
404 info_log("%s\n", n->get_name());
407 for(int i=0; i<n->get_num_children(); i++) {
408 print_scene_graph(n->get_child(i), level + 1);