converted scene loader to use assman
authorJohn Tsiombikas <nuclear@member.fsf.org>
Sat, 29 Sep 2018 02:51:32 +0000 (05:51 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Sat, 29 Sep 2018 02:51:32 +0000 (05:51 +0300)
src/imgui/imgui.cc
src/sceneload.cc
src/vrinput.cc

index 211bd29..303b780 100644 (file)
@@ -1541,7 +1541,7 @@ ImGuiTextFilter::ImGuiTextFilter(const char* default_filter)
 {
     if (default_filter)
     {
-        ImStrncpy(InputBuf, default_filter, IM_ARRAYSIZE(InputBuf));
+        ImStrncpy(InputBuf, default_filter, IM_ARRAYSIZE(InputBuf) - 1);
         Build();
     }
     else
index 28f5877..7459c1d 100644 (file)
@@ -5,6 +5,7 @@
 #include <map>
 #include <gmath/gmath.h>
 #include <assimp/cimport.h>
+#include <assimp/cfileio.h>
 #include <assimp/postprocess.h>
 #include <assimp/scene.h>
 #include <assimp/mesh.h>
@@ -13,6 +14,7 @@
 #include <assimp/vector3.h>
 #include <assimp/matrix4x4.h>
 #include <assimp/quaternion.h>
+#include "assman.h"
 #include "app.h"
 #include "scene.h"
 #include "objmesh.h"
@@ -36,6 +38,13 @@ static long assimp_time(const aiAnimation *anim, double aitime);
 static void print_hierarchy(const aiNode *node);
 */
 
+static aiFile *io_open(aiFileIO *io, const char *fname, const char *mode);
+static void io_close(aiFileIO *io, aiFile *aifp);
+static size_t io_read(aiFile *aifp, char *buf, size_t size, size_t count);
+static size_t io_tell(aiFile *aifp);
+static size_t io_filesize(aiFile *aifp);
+static aiReturn io_seek(aiFile *aifp, size_t offs, aiOrigin whence);
+
 struct LoaderData {
        const aiScene *aiscn;
        std::string fname;
@@ -72,7 +81,11 @@ bool Scene::load(const char *fname, unsigned int flags)
                        this->name = std::string(fname);
                }
 
-               const aiScene *aiscn = aiImportFile(fname, ppflags);
+               aiFileIO io;
+               io.OpenProc = io_open;
+               io.CloseProc = io_close;
+
+               const aiScene *aiscn = aiImportFileEx(fname, ppflags, &io);
                if(!aiscn) {
                        error_log("failed to load scene file: %s\n", fname);
                        return false;
@@ -400,3 +413,59 @@ void SceneSet::free_scene(Scene *scn)
 {
        delete scn;
 }
+
+
+// ------ custom file I/O for assimp -------
+
+static aiFile *io_open(aiFileIO *io, const char *fname, const char *mode)
+{
+       ass_file *fp;
+       if(!(fp = ass_fopen(fname, mode))) {
+               error_log("failed to open scene file: %s: %s\n", fname, strerror(ass_errno));
+               return 0;
+       }
+
+       aiFile *aifp = new aiFile;
+       aifp->ReadProc = io_read;
+       aifp->WriteProc = 0;
+       aifp->TellProc = io_tell;
+       aifp->FileSizeProc = io_filesize;
+       aifp->SeekProc = io_seek;
+       aifp->FlushProc = 0;
+       aifp->UserData = (aiUserData)fp;
+       return aifp;
+}
+
+static void io_close(aiFileIO *io, aiFile *aifp)
+{
+       ass_fclose(aifp->UserData);
+       delete aifp;
+}
+
+static size_t io_read(aiFile *aifp, char *buf, size_t size, size_t count)
+{
+       return ass_fread(buf, size, count, aifp->UserData);
+}
+
+static size_t io_tell(aiFile *aifp)
+{
+       return ass_ftell(aifp->UserData);
+}
+
+static size_t io_filesize(aiFile *aifp)
+{
+       ass_file *fp = aifp->UserData;
+       long cur = ass_ftell(fp);
+       ass_fseek(fp, 0, SEEK_END);
+       long off = ass_ftell(fp);
+       ass_fseek(fp, cur, SEEK_SET);
+       return off;
+}
+
+static aiReturn io_seek(aiFile *aifp, size_t offs, aiOrigin whence)
+{
+       if(ass_fseek(aifp->UserData, offs, (int)whence) == -1) {
+               return aiReturn_FAILURE;
+       }
+       return aiReturn_SUCCESS;
+}
index 6bb2aaa..2d0646a 100644 (file)
@@ -6,24 +6,25 @@
 
 VRHand vrhand[2];
 
-static Scene *scn;
+//static Scene *scn;
 
 bool init_vrhands()
 {
-       scn = new Scene;
+       /*scn = new Scene;
        if(!(scn->load("data/vrhands.obj"))) {
                return false;
        }
        scn->objects[0]->node->set_position(Vec3(0, 150, 0));
        scn->objects[1]->node->set_position(Vec3(0, 250, 0));
        scn->update(0);
+       */
        return true;
 }
 
 void destroy_vrhands()
 {
-       delete scn;
-       scn = 0;
+       //delete scn;
+       //scn = 0;
 }
 
 void update_vrhands(const Avatar *avatar)
@@ -46,11 +47,11 @@ void update_vrhands(const Avatar *avatar)
                }
        }
 
-       scn->update(0);
+       //scn->update(0);
 }
 
 void draw_vrhands()
 {
-       bind_shader(0);
-       scn->draw();
+       //bind_shader(0);
+       //scn->draw();
 }