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