datamap object passed around while loading
[laserbrain_demo] / src / metascene.cc
1 #include <string>
2 #include <regex>
3 #include "metascene.h"
4 #include "scene.h"
5 #include "treestore.h"
6 #include "logger.h"
7
8 #ifdef WIN32
9 #include <malloc.h>
10 #else
11 #include <alloca.h>
12 #endif
13
14 struct MaterialEdit {
15         std::regex name_re;
16         int attr;
17         Texture *tex;
18 };
19
20 static bool proc_node(MetaScene *mscn, struct ts_node *node);
21 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node);
22 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node, TextureSet *texset);
23 static void apply_mtledit(Scene *scn, const MaterialEdit &med);
24 static void apply_mtledit(Material *mtl, const MaterialEdit &med);
25 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name);
26
27 static void print_scene_graph(SceneNode *n, int level);
28
29
30 MetaScene::MetaScene(SceneSet *sman, TextureSet *tman)
31 {
32         sceneman = sman;
33         texman = tman;
34         walk_mesh = 0;
35 }
36
37 MetaScene::~MetaScene()
38 {
39         delete walk_mesh;
40 }
41
42
43 bool MetaScene::load(const char *fname)
44 {
45         struct ts_node *root = ts_load(fname);
46         if(!root || strcmp(root->name, "scene") != 0) {
47                 ts_free_tree(root);
48                 error_log("failed to load scene metadata: %s\n", fname);
49                 return false;
50         }
51
52         bool res = proc_node(this, root);
53         ts_free_tree(root);
54
55         /*info_log("loaded scene: %s\n", fname);
56         info_log("scene graph:\n");
57         print_scene_graph(scn->nodes, 0);
58         */
59         return res;
60 }
61
62 void MetaScene::update(float dt)
63 {
64         int nscn = scenes.size();
65         for(int i=0; i<nscn; i++) {
66                 scenes[i]->update(dt);
67         }
68 }
69
70 void MetaScene::draw() const
71 {
72         int nscn = scenes.size();
73         for(int i=0; i<nscn; i++) {
74                 scenes[i]->draw();
75         }
76 }
77
78 static bool proc_node(MetaScene *mscn, struct ts_node *node)
79 {
80         struct ts_node *c = node->child_list;
81         while(c) {
82                 if(!proc_node(mscn, c)) {
83                         return false;
84                 }
85                 c = c->next;
86         }
87
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);
91
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);
97                 }
98         }
99
100         return true;
101 }
102
103
104
105 struct SceneData {
106         std::string walkmesh_regexp;
107         std::vector<MaterialEdit> mtledit;
108 };
109
110 static bool proc_scenefile(MetaScene *mscn, struct ts_node *node)
111 {
112         const char *fname = ts_get_attr_str(node, "file");
113         if(fname) {
114                 SceneData *sdat = new SceneData;
115
116                 // datapath
117                 struct ts_attr *adpath = attr_inscope(node, "datapath");
118                 if(adpath && adpath->val.type == TS_STRING) {
119                         info_log("adding data path: %s\n", adpath->val.str);
120                         mscn->datamap.set_path(adpath->val.str);
121                 }
122
123                 // walkmesh
124                 struct ts_attr *awmesh = attr_inscope(node, "walkmesh");
125                 if(awmesh && awmesh->val.type == TS_STRING) {
126                         sdat->walkmesh_regexp = std::string(awmesh->val.str);
127                 }
128
129                 int namesz = mscn->datamap.lookup(fname, 0, 0);
130                 char *namebuf = (char*)alloca(namesz + 1);
131                 if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
132                         fname = namebuf;
133                 }
134
135                 // material edits
136                 struct ts_node *child = node->child_list;
137                 while(child) {
138                         MaterialEdit medit;
139                         if(proc_mtledit(mscn, &medit, child, mscn->texman)) {
140                                 sdat->mtledit.push_back(medit);
141                         }
142                         child = child->next;
143                 }
144
145                 Scene *newscn = mscn->sceneman->get(fname);
146                 /* NOTE: setting all these after get() is not a race condition, because
147                  * scene_loaded() which uses this, will only run in our main loop during
148                  * SceneSet::update() on the main thread.
149                  */
150                 newscn->datamap = mscn->datamap;
151                 mscn->datamap.clear();
152
153                 newscn->metascn = mscn;
154                 mscn->scndata[newscn] = sdat;
155         }
156         return true;
157 }
158
159 bool MetaScene::scene_loaded(Scene *newscn)
160 {
161         SceneData *sdat = (SceneData*)scndata[newscn];
162         if(!sdat) {
163                 error_log("MetaScene::scene_loaded called, but corresponding SceneData not found\n");
164                 return false;
165         }
166
167         // extract the walk mesh if necessary
168         Scene *wscn;
169         if(!sdat->walkmesh_regexp.empty() && (wscn = newscn->extract_nodes(sdat->walkmesh_regexp.c_str()))) {
170                 // apply all transformations to the meshes
171                 wscn->apply_xform();
172
173                 int nmeshes = wscn->meshes.size();
174                 for(int i=0; i<nmeshes; i++) {
175                         Mesh *m = wscn->meshes[i];
176
177                         if(walk_mesh) {
178                                 walk_mesh->append(*m);
179                         } else {
180                                 walk_mesh = m;
181                                 wscn->remove_mesh(m);   // to save it from destruction
182                         }
183                 }
184
185                 delete wscn;
186         }
187
188         int num_medits = sdat->mtledit.size();
189         for(int i=0; i<num_medits; i++) {
190                 // perform material edits
191                 apply_mtledit(newscn, sdat->mtledit[i]);
192         }
193
194         scenes.push_back(newscn);
195         return true;
196 }
197
198 static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node, TextureSet *texset)
199 {
200         if(strcmp(node->name, "mtledit") != 0) {
201                 return false;
202         }
203
204         const char *restr = ".*";
205         struct ts_attr *amtl = ts_get_attr(node, "material");
206         if(amtl && amtl->val.type == TS_STRING) {
207                 restr = amtl->val.str;
208         }
209
210         med->name_re = std::regex(restr);
211
212         node = node->child_list;
213         while(node) {
214                 struct ts_node *cn = node;
215                 node = node->next;
216
217                 if(strcmp(cn->name, "texture") == 0) {
218                         // add/change/remove a texture
219                         struct ts_attr *atype = ts_get_attr(cn, "type");
220                         struct ts_attr *afile = ts_get_attr(cn, "file");
221
222                         int textype = MTL_TEX_DIFFUSE;
223                         if(atype) {
224                                 if(atype->val.type == TS_STRING) {
225                                         textype = mtl_parse_type(atype->val.str);
226                                 } else if(atype->val.type == TS_NUMBER) {
227                                         textype = atype->val.inum;
228                                         if(textype < 0 || textype >= NUM_MTL_TEXTURES) {
229                                                 error_log("invalid texture in mtledit: %d\n", textype);
230                                                 continue;
231                                         }
232                                 } else {
233                                         error_log("unexpected texture type in mtledit: %s\n", atype->val.str);
234                                         continue;
235                                 }
236                         }
237
238                         med->attr = textype;
239
240                         if(!afile || !afile->val.str || !*afile->val.str) {
241                                 // remove
242                                 med->tex = 0;
243                         } else {
244                                 med->tex = texset->get_texture(afile->val.str, TEX_2D, &mscn->datamap);
245                         }
246                 }
247                 // TODO add more edit modes
248         }
249
250         return true;
251 }
252
253 static void apply_mtledit(Scene *scn, const MaterialEdit &med)
254 {
255         // search all the objects to find matching material names
256         int nobj = scn->objects.size();
257         for(int i=0; i<nobj; i++) {
258                 Object *obj = scn->objects[i];
259                 if(std::regex_match(obj->get_name(), med.name_re)) {
260                         apply_mtledit(&obj->mtl, med);
261                 }
262         }
263 }
264
265 static void apply_mtledit(Material *mtl, const MaterialEdit &med)
266 {
267         // TODO more edit modes...
268         if(med.tex) {
269                 mtl->add_texture(med.tex, med.attr);
270         } else {
271                 Texture *tex = mtl->stdtex[med.attr];
272                 if(tex) {
273                         mtl->remove_texture(tex);
274                 }
275         }
276 }
277
278 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
279 {
280         struct ts_attr *attr = 0;
281
282         while(node && !(attr = ts_get_attr(node, name))) {
283                 node = node->parent;
284         }
285         return attr;
286 }
287
288 static void print_scene_graph(SceneNode *n, int level)
289 {
290         if(!n) return;
291
292         for(int i=0; i<level; i++) {
293                 info_log("  ");
294         }
295
296         int nobj = n->get_num_objects();
297         if(nobj) {
298                 info_log("%s - %d obj\n", n->get_name(), n->get_num_objects());
299         } else {
300                 info_log("%s\n", n->get_name());
301         }
302
303         for(int i=0; i<n->get_num_children(); i++) {
304                 print_scene_graph(n->get_child(i), level + 1);
305         }
306 }