From: John Tsiombikas Date: Tue, 13 Mar 2018 02:52:03 +0000 (+0200) Subject: mirror mtledit X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=laserbrain_demo;a=commitdiff_plain;h=d47a663825bd358d2165c1a4b040cc828aeb4991;hp=d0e426c6c0856487332e67c787c35fd0e97c90ce mirror mtledit --- diff --git a/src/material.cc b/src/material.cc index 6288ef4..26ab63d 100644 --- a/src/material.cc +++ b/src/material.cc @@ -11,6 +11,9 @@ Material::Material() shininess = 0.0f; alpha = 1.0f; memset(stdtex, 0, sizeof stdtex); + + reflect = 0.0f; + flat_mirror = false; } void Material::setup() const diff --git a/src/material.h b/src/material.h index c46a21d..3d0b566 100644 --- a/src/material.h +++ b/src/material.h @@ -25,6 +25,9 @@ public: float shininess; float alpha; + float reflect; + bool flat_mirror; + Texture *stdtex[NUM_MTL_TEXTURES]; std::vector textures; diff --git a/src/metascene.cc b/src/metascene.cc index d9f96fb..9c88249 100644 --- a/src/metascene.cc +++ b/src/metascene.cc @@ -24,12 +24,28 @@ #include #endif -struct MaterialEdit { - std::regex name_re; +enum MaterialEditType { + MTL_EDIT_TEXTURE, + MTL_EDIT_MIRROR +}; + +struct MaterialEditTexture { int attr; Texture *tex; }; +struct MaterialEditMirror { + float reflectivity; +}; + +struct MaterialEdit { + std::regex name_re; + MaterialEditType type; + + MaterialEditTexture tex; + MaterialEditMirror mirror; +}; + static bool proc_node(MetaScene *mscn, struct ts_node *node); static bool proc_scenefile(MetaScene *mscn, struct ts_node *node); static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node); @@ -365,7 +381,6 @@ static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *nod if(amtl && amtl->val.type == TS_STRING) { restr = amtl->val.str; } - med->name_re = std::regex(restr); node = node->child_list; @@ -394,16 +409,37 @@ static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *nod } } - med->attr = textype; + med->tex.attr = textype; if(!afile || !afile->val.str || !*afile->val.str) { // remove - med->tex = 0; + med->tex.tex = 0; } else { - med->tex = texman.get_texture(afile->val.str, TEX_2D, &mscn->datamap); + med->tex.tex = texman.get_texture(afile->val.str, TEX_2D, &mscn->datamap); } + + med->type = MTL_EDIT_TEXTURE; + break; + } + + if(strcmp(cn->name, "mirror") == 0) { + // make this object a flat mirror (hopefully the object is flat otherwise this won't work) + float refl = 1.0f; + + struct ts_attr *arefl = ts_get_attr(cn, "reflect"); + if(arefl) { + if(arefl->val.type == TS_NUMBER) { + refl = arefl->val.fnum; + } else { + error_log("invalid reflect attribute in mirror mtledit: %s\n", arefl->val.str); + continue; + } + } + + med->type = MTL_EDIT_MIRROR; + med->mirror.reflectivity = refl; + break; } - // TODO add more edit modes } return true; @@ -455,13 +491,22 @@ static bool proc_music(MetaScene *mscn, struct ts_node *node) static void apply_mtledit(Material *mtl, const MaterialEdit &med) { // TODO more edit modes... - if(med.tex) { - mtl->add_texture(med.tex, med.attr); - } else { - Texture *tex = mtl->stdtex[med.attr]; - if(tex) { - mtl->remove_texture(tex); + switch(med.type) { + case MTL_EDIT_TEXTURE: + if(med.tex.tex) { + mtl->add_texture(med.tex.tex, med.tex.attr); + } else { + Texture *tex = mtl->stdtex[med.tex.attr]; + if(tex) { + mtl->remove_texture(tex); + } } + break; + + case MTL_EDIT_MIRROR: + mtl->flat_mirror = med.mirror.reflectivity > 1e-6; + mtl->reflect = med.mirror.reflectivity; + break; } }