+#include <regex>
#include "metascene.h"
#include "scene.h"
#include "datamap.h"
static bool proc_node(Scene *scn, struct ts_node *node);
static bool proc_scenefile(Scene *scn, struct ts_node *node);
+static bool proc_mtledit(Scene *scn, struct ts_node *node);
static struct ts_attr *attr_inscope(struct ts_node *node, const char *name);
static void print_scene_graph(SceneNode *n, int level);
fname = namebuf;
}
- if(!(scn->load(fname, SCNLOAD_FLIPTEX))) {
+ Scene *newscn = new Scene(scn->texset);
+ if(!(newscn->load(fname, SCNLOAD_FLIPTEX))) { // TODO unhardcode FLIPTEX
return false;
}
// extract the walk mesh if necessary
- // XXX as written, this will match even objects loaded by previous scenefile blocks
Scene *wscn;
- if(walkmesh_regexp && (wscn = scn->extract_nodes(walkmesh_regexp))) {
+ if(walkmesh_regexp && (wscn = newscn->extract_nodes(walkmesh_regexp))) {
// apply all transformations to the meshes
wscn->apply_xform();
for(int i=0; i<nmeshes; i++) {
Mesh *m = wscn->meshes[i];
- if(scn->walk_mesh) {
- scn->walk_mesh->append(*m);
+ if(newscn->walk_mesh) {
+ newscn->walk_mesh->append(*m);
} else {
- scn->walk_mesh = m;
+ newscn->walk_mesh = m;
wscn->remove_mesh(m); // to save it from destruction
}
}
delete wscn;
}
+
+ // perform material edits
+ struct ts_node *child = node->child_list;
+ while(child) {
+ proc_mtledit(newscn, child); // ignores any non-mtledit nodes, no need to check
+ child = child->next;
+ }
+
+ scn->merge(newscn);
+ delete newscn;
+ }
+ //datamap_reset(); // TODO this should be push/pop instead of hard reset
+
+ return true;
+}
+
+static void mtledit(Material *mtl, TextureSet *texset, struct ts_node *node)
+{
+ node = node->child_list;
+ while(node) {
+ struct ts_node *cn = node;
+ node = node->next;
+
+ if(strcmp(cn->name, "texture") == 0) {
+ // add/change/remove a texture
+ struct ts_attr *atype = ts_get_attr(cn, "type");
+ struct ts_attr *afile = ts_get_attr(cn, "file");
+
+ int textype = MTL_TEX_DIFFUSE;
+ if(atype) {
+ if(atype->val.type == TS_STRING) {
+ textype = mtl_parse_type(atype->val.str);
+ } else if(atype->val.type == TS_NUMBER) {
+ textype = atype->val.inum;
+ if(textype < 0 || textype >= NUM_MTL_TEXTURES) {
+ error_log("invalid texture in mtledit: %d\n", textype);
+ continue;
+ }
+ } else {
+ error_log("unexpected texture type in mtledit: %s\n", atype->val.str);
+ continue;
+ }
+ }
+
+ if(!afile || !afile->val.str || !*afile->val.str) {
+ // remove
+ mtl->textures[textype] = 0;
+ } else {
+ Texture *tex = texset->get_texture(afile->val.str, TEX_2D);
+ mtl->add_texture(tex, textype);
+ }
+ }
+ // TODO add more edit modes
+ }
+}
+
+static bool proc_mtledit(Scene *scn, struct ts_node *node)
+{
+ if(strcmp(node->name, "mtledit") != 0) {
+ return false;
+ }
+
+ const char *mtl_regexp = ".*";
+ struct ts_attr *amtl = ts_get_attr(node, "material");
+ if(amtl && amtl->val.type == TS_STRING) {
+ mtl_regexp = amtl->val.str;
+ }
+
+ // search all the objects to find matching material names
+ std::regex re{mtl_regexp};
+
+ int nobj = scn->objects.size();
+ for(int i=0; i<nobj; i++) {
+ Object *obj = scn->objects[i];
+ if(std::regex_match(obj->get_name(), re)) {
+ mtledit(&obj->mtl, scn->texset, node);
+ }
}
- datamap_reset();
return true;
}