added delayed init call after scenegraph/meshes are done loading
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 13 Mar 2018 08:57:06 +0000 (10:57 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 13 Mar 2018 08:57:06 +0000 (10:57 +0200)
src/app.cc
src/dataset.h
src/dataset.inl
src/exman.cc
src/metascene.cc
src/metascene.h
src/scene.h
src/texture.cc

index 63e7cd7..7b4280d 100644 (file)
@@ -84,6 +84,8 @@ static Renderer *rend;
 
 static Ray last_pick_ray;
 
+static bool post_scene_init_pending = true;
+
 
 bool app_init(int argc, char **argv)
 {
@@ -206,6 +208,11 @@ bool app_init(int argc, char **argv)
        return true;
 }
 
+static void post_scene_init()
+{
+       exman->load(mscn, "data/exhibits");
+}
+
 void app_cleanup()
 {
        if(mscn->music) {
@@ -257,6 +264,11 @@ static void update(float dt)
        texman.update();
        sceneman.update();
 
+       if(post_scene_init_pending && !sceneman.pending()) {
+               post_scene_init_pending = false;
+               post_scene_init();
+       }
+
        mscn->update(dt);
        exman->update(dt);
        exui_update(dt);
index 5c4ca51..38fec6b 100644 (file)
@@ -53,6 +53,9 @@ public:
 
        T get(const char *name) const;
 
+       int pending() const;
+       void wait() const;
+
        void add_loaded_callback(LoadedCallbackFunc func, void *cls = 0);
        bool remove_loaded_callback(LoadedCallbackFunc func);
 };
index 954081e..41277a1 100644 (file)
@@ -47,11 +47,23 @@ T DataSet<T>::get(const char *name) const
 
        T res = create();
        data[name] = res;
-       resman_lookup(rman, name, res);
+       resman_add(rman, name, res);
        return res;
 }
 
 template <typename T>
+int DataSet<T>::pending() const
+{
+       return resman_pending(rman);
+}
+
+template <typename T>
+void DataSet<T>::wait() const
+{
+       resman_waitall(rman);
+}
+
+template <typename T>
 void DataSet<T>::add_loaded_callback(LoadedCallbackFunc func, void *cls)
 {
        std::pair<LoadedCallbackFunc, void*> cb;
index eea24a6..00f7e39 100644 (file)
@@ -153,6 +153,8 @@ bool ExhibitManager::remove(Exhibit *ex)
 
 bool ExhibitManager::load(MetaScene *mscn, const char *fname)
 {
+       info_log("ExhibitManager::load(%s)\n", fname);
+
        struct ts_node *root = ts_load(fname);
        if(!root || strcmp(root->name, "exhibits") != 0) {
                ts_free_tree(root);
@@ -243,6 +245,7 @@ bool ExhibitManager::load(MetaScene *mscn, const char *fname)
                        const char *voice = ts_get_attr_str(node, "voiceover");
                        if(desc || voice) {
                                ExData exd;
+                               exd.type = EXDATA_INFO;
 
                                if(desc) {
                                        char *fixed_desc = new char[strlen(desc) + 1];
index 9c88249..c467420 100644 (file)
@@ -13,6 +13,7 @@
 #include <regex>
 #include "metascene.h"
 #include "scene.h"
+#include "objmesh.h"
 #include "treestore.h"
 #include "logger.h"
 #include "app.h"
@@ -61,12 +62,19 @@ MetaScene::MetaScene()
 {
        walk_mesh = 0;
        music = 0;
+       mirrors = 0;
 }
 
 MetaScene::~MetaScene()
 {
        delete walk_mesh;
        delete music;
+
+       while(mirrors) {
+               FlatMirror *m = mirrors;
+               mirrors = mirrors->next;
+               delete m;
+       }
 }
 
 bool MetaScene::load(const char *fname)
@@ -134,6 +142,7 @@ void MetaScene::update(float dt)
 // XXX not used, renderer draws
 void MetaScene::draw() const
 {
+       error_log("Don't call MetaScene::draw, use the Renderer\n");
        int nscn = scenes.size();
        for(int i=0; i<nscn; i++) {
                scenes[i]->draw();
@@ -191,6 +200,66 @@ Scene *MetaScene::extract_nodes(const char *qstr)
        return scn;
 }
 
+int MetaScene::calc_mirror_planes()
+{
+       int num_mirrors = 0;
+       while(mirrors) {
+               FlatMirror *m = mirrors;
+               mirrors = mirrors->next;
+               delete m;
+       }
+       mirrors = 0;
+       objmirror.clear();
+
+       int numscn = scenes.size();
+       for(int i=0; i<numscn; i++) {
+               Scene *scn = scenes[i];
+
+               int numobj = scn->objects.size();
+               for(int j=0; j<numobj; j++) {
+                       Object *obj = scn->objects[j];
+
+                       if(obj->mtl.flat_mirror && obj->get_type() == OBJ_MESH) {
+                               const Mesh *mesh = ((ObjMesh*)obj)->mesh;
+                               if(!mesh) continue;
+
+                               // assume the object is actually flat, so grab the first triangle and make a plane
+                               Triangle face = Triangle(0, (const Vec3*)mesh->get_attrib_data(MESH_ATTR_VERTEX),
+                                               mesh->get_index_data());
+                               face.calc_normal();
+
+                               FlatMirror *mir = new FlatMirror;
+                               mir->plane.pt = face.v[0];
+                               mir->plane.normal = face.normal;
+                               mir->reflect = obj->mtl.reflect;
+
+                               // check to see if we have found this mirror plane already
+                               bool found = false;
+                               FlatMirror *node = mirrors;
+                               while(node) {
+                                       if(fabs(dot(mir->plane.normal, node->plane.normal)) < 1e-4 &&
+                                                       fabs(dot(mir->plane.normal, normalize(mir->plane.pt - node->plane.pt))) < 1e-4) {
+                                               found = true;
+                                               break;
+                                       }
+                               }
+
+                               if(!found) {
+                                       mir->next = mirrors;
+                                       mirrors = mir;
+
+                                       objmirror[obj] = mir;   // associate with object
+                                       ++num_mirrors;
+                               } else {
+                                       delete mir;
+                               }
+                       }
+               }
+       }
+
+       return num_mirrors;
+}
+
 static bool proc_node(MetaScene *mscn, struct ts_node *node)
 {
        struct ts_node *c = node->child_list;
index c363901..0417c00 100644 (file)
@@ -1,12 +1,24 @@
 #ifndef METASCENE_H_
 #define METASCENE_H_
 
+#include <vector>
 #include <map>
 #include "scene.h"
 #include "mesh.h"
+#include "geom.h"
 #include "audio/ovstream.h"
 #include "datamap.h"
 
+
+struct FlatMirror {
+       Plane plane;
+       float reflect;
+       //std::vector<Object*> objects;
+
+       FlatMirror *next;
+};
+
+
 class MetaScene {
 public:
        DataMap datamap;
@@ -19,6 +31,10 @@ public:
 
        std::map<Scene*, void*> scndata;
 
+       FlatMirror *mirrors;
+       int num_mirrors;
+       std::map<Object*, FlatMirror*> objmirror;
+
        AudioStream *music;
 
        MetaScene();
@@ -38,6 +54,8 @@ public:
        std::list<SceneNode*> match_nodes(const char *qstr) const;
 
        Scene *extract_nodes(const char *qstr);
+
+       int calc_mirror_planes();
 };
 
 #endif // METASCENE_H_
index dd421d9..65e1a55 100644 (file)
@@ -37,7 +37,7 @@ public:
        TextureSet *texset;
        void *loader_data;
 
-       explicit Scene();
+       Scene();
        ~Scene();
 
        Scene(const Scene &rhs) = delete;
index 3af8116..ca1a6db 100644 (file)
@@ -516,7 +516,7 @@ Texture *TextureSet::get_texture(const char *name, TextureType type, const DataM
        Texture *res = create();
        data[fname] = res;
        res->create_default(type);
-       resman_lookup(rman, fname, res);
+       resman_add(rman, fname, res);
        return res;
 }