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 static bool proc_node(MetaScene *mscn, struct ts_node *node)
80 struct ts_node *c = node->child_list;
82 if(!proc_node(mscn, c)) {
88 // do this last to allow other contents of the node to do their thing
89 if(strcmp(node->name, "scenefile") == 0) {
90 return proc_scenefile(mscn, node);
92 } else if(strcmp(node->name, "remap") == 0) {
93 const char *match = ts_get_attr_str(node, "match");
94 const char *replace = ts_get_attr_str(node, "replace");
95 if(match && replace) {
96 mscn->datamap.map(match, replace);
107 std::string walkmesh_regexp, spawn_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;
119 struct ts_attr *adpath = attr_inscope(node, "datapath");
120 if(adpath && adpath->val.type == TS_STRING) {
121 info_log("adding data path: %s\n", adpath->val.str);
122 mscn->datamap.set_path(adpath->val.str);
126 struct ts_attr *awmesh = attr_inscope(node, "walkmesh");
127 if(awmesh && awmesh->val.type == TS_STRING) {
128 sdat->walkmesh_regexp = std::string(awmesh->val.str);
132 struct ts_attr *awspawn = attr_inscope(node, "spawn");
134 switch(awspawn->val.type) {
136 mscn->start_pos = Vec3(awspawn->val.vec[0], awspawn->val.vec[1],
137 awspawn->val.vec[2]);
142 sdat->spawn_regexp = std::string(awspawn->val.str);
145 if((awspawn = attr_inscope(node, "spawn_rot")) && awspawn->val.type == TS_VECTOR) {
147 rot.rotate(Vec3(1, 0, 0), deg_to_rad(awspawn->val.vec[0]));
148 rot.rotate(Vec3(0, 1, 0), deg_to_rad(awspawn->val.vec[1]));
149 rot.rotate(Vec3(0, 0, 1), deg_to_rad(awspawn->val.vec[2]));
150 mscn->start_rot = rot;
153 int namesz = mscn->datamap.lookup(fname, 0, 0);
154 char *namebuf = (char*)alloca(namesz + 1);
155 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
160 struct ts_node *child = node->child_list;
163 if(proc_mtledit(mscn, &medit, child)) {
164 sdat->mtledit.push_back(medit);
169 Scene *newscn = sceneman.get(fname);
170 /* NOTE: setting all these after get() is not a race condition, because
171 * scene_loaded() which uses this, will only run in our main loop during
172 * SceneSet::update() on the main thread.
174 newscn->datamap = mscn->datamap;
175 mscn->datamap.clear();
177 newscn->metascn = mscn;
178 mscn->scndata[newscn] = sdat;
183 bool MetaScene::scene_loaded(Scene *newscn)
185 SceneData *sdat = (SceneData*)scndata[newscn];
187 error_log("MetaScene::scene_loaded called, but corresponding SceneData not found\n");
191 // extract the walk mesh if necessary
193 if(!sdat->walkmesh_regexp.empty() && (wscn = newscn->extract_nodes(sdat->walkmesh_regexp.c_str()))) {
194 // apply all transformations to the meshes
197 int nmeshes = wscn->meshes.size();
198 for(int i=0; i<nmeshes; i++) {
199 Mesh *m = wscn->meshes[i];
202 walk_mesh->append(*m);
205 wscn->remove_mesh(m); // to save it from destruction
212 // extract the spawn node
213 if(!sdat->spawn_regexp.empty() && (wscn = newscn->extract_nodes(sdat->spawn_regexp.c_str()))) {
215 int nmeshes = wscn->meshes.size();
216 int nnodes = wscn->nodes ? wscn->nodes->get_num_children() : 0;
220 for(int i=0; i<nmeshes; i++) {
221 const Sphere &bsph = wscn->meshes[i]->get_bsphere();
224 pos /= (float)nmeshes;
225 sdat->meta->start_pos = pos;
228 // just use the first one
229 SceneNode *first = wscn->nodes->get_child(0);
230 sdat->meta->start_pos = first->get_position();
231 sdat->meta->start_rot = first->get_rotation();
236 int num_medits = sdat->mtledit.size();
237 for(int i=0; i<num_medits; i++) {
238 // perform material edits
239 apply_mtledit(newscn, sdat->mtledit[i]);
242 scenes.push_back(newscn);
246 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node)
248 if(strcmp(node->name, "mtledit") != 0) {
252 const char *restr = ".*";
253 struct ts_attr *amtl = ts_get_attr(node, "material");
254 if(amtl && amtl->val.type == TS_STRING) {
255 restr = amtl->val.str;
258 med->name_re = std::regex(restr);
260 node = node->child_list;
262 struct ts_node *cn = node;
265 if(strcmp(cn->name, "texture") == 0) {
266 // add/change/remove a texture
267 struct ts_attr *atype = ts_get_attr(cn, "type");
268 struct ts_attr *afile = ts_get_attr(cn, "file");
270 int textype = MTL_TEX_DIFFUSE;
272 if(atype->val.type == TS_STRING) {
273 textype = mtl_parse_type(atype->val.str);
274 } else if(atype->val.type == TS_NUMBER) {
275 textype = atype->val.inum;
276 if(textype < 0 || textype >= NUM_MTL_TEXTURES) {
277 error_log("invalid texture in mtledit: %d\n", textype);
281 error_log("unexpected texture type in mtledit: %s\n", atype->val.str);
288 if(!afile || !afile->val.str || !*afile->val.str) {
292 med->tex = texman.get_texture(afile->val.str, TEX_2D, &mscn->datamap);
295 // TODO add more edit modes
301 static void apply_mtledit(Scene *scn, const MaterialEdit &med)
303 // search all the objects to find matching material names
304 int nobj = scn->objects.size();
305 for(int i=0; i<nobj; i++) {
306 Object *obj = scn->objects[i];
307 if(std::regex_match(obj->mtl.name, med.name_re)) {
308 apply_mtledit(&obj->mtl, med);
313 static void apply_mtledit(Material *mtl, const MaterialEdit &med)
315 // TODO more edit modes...
317 mtl->add_texture(med.tex, med.attr);
319 Texture *tex = mtl->stdtex[med.attr];
321 mtl->remove_texture(tex);
326 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
328 struct ts_attr *attr = 0;
330 while(node && !(attr = ts_get_attr(node, name))) {
336 static void print_scene_graph(SceneNode *n, int level)
340 for(int i=0; i<level; i++) {
344 int nobj = n->get_num_objects();
346 info_log("%s - %d obj\n", n->get_name(), n->get_num_objects());
348 info_log("%s\n", n->get_name());
351 for(int i=0; i<n->get_num_children(); i++) {
352 print_scene_graph(n->get_child(i), level + 1);