fixed the mesh loader
authorJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 25 Dec 2019 21:06:40 +0000 (23:06 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 25 Dec 2019 21:06:40 +0000 (23:06 +0200)
tools/ropesim/src/main.c
tools/ropesim/src/meshload.c

index bb5e5c5..bfd6424 100644 (file)
@@ -108,7 +108,7 @@ void display(void)
                {0.5, 0.3, 0.2, 1},
                {0.2, 0.3, 0.2, 1}
        };
                {0.5, 0.3, 0.2, 1},
                {0.2, 0.3, 0.2, 1}
        };
-       int i;
+       int i, count;
 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
@@ -123,7 +123,13 @@ void display(void)
                glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
        }
 
                glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
        }
 
-       cmesh_draw(scn);
+       count = cmesh_submesh_count(scn);
+       for(i=0; i<count; i++) {
+               cmesh_draw_submesh(scn, i);
+       }
+       cmesh_draw(mesh_gout);
+       cmesh_draw(mesh_gin);
+       cmesh_draw(mesh_suz);
 
        glutSwapBuffers();
 }
 
        glutSwapBuffers();
 }
index 3a1fc70..3522932 100644 (file)
 
 #ifdef USE_ASSIMP
 
 
 #ifdef USE_ASSIMP
 
-static int add_mesh(struct cmesh *mesh, struct aiMesh *aimesh);
+static int add_mesh(struct cmesh *mesh, struct aiMesh *aimesh, const struct aiNode *ainode);
+static struct aiNode *find_node(struct aiNode *root, unsigned int midx);
 
 #define AIPPFLAGS \
 
 #define AIPPFLAGS \
-       (aiProcess_JoinIdenticalVertices | aiProcess_PreTransformVertices | \
+       (aiProcess_JoinIdenticalVertices | \
         aiProcess_Triangulate | aiProcess_SortByPType | aiProcess_FlipUVs)
 
 int cmesh_load(struct cmesh *mesh, const char *fname)
 {
        int i;
        const struct aiScene *aiscn;
         aiProcess_Triangulate | aiProcess_SortByPType | aiProcess_FlipUVs)
 
 int cmesh_load(struct cmesh *mesh, const char *fname)
 {
        int i;
        const struct aiScene *aiscn;
+       const struct aiNode *ainode;
 
        if(!(aiscn = aiImportFile(fname, AIPPFLAGS))) {
                fprintf(stderr, "failed to open mesh file: %s\n", fname);
                return -1;
        }
 
 
        if(!(aiscn = aiImportFile(fname, AIPPFLAGS))) {
                fprintf(stderr, "failed to open mesh file: %s\n", fname);
                return -1;
        }
 
+       printf("scene contains %d meshes\n", (int)aiscn->mNumMeshes);
        for(i=0; i<(int)aiscn->mNumMeshes; i++) {
        for(i=0; i<(int)aiscn->mNumMeshes; i++) {
-               add_mesh(mesh, aiscn->mMeshes[i]);
+               if(aiscn->mRootNode->mNumChildren) {
+                       ainode = find_node(aiscn->mRootNode, i);
+               } else {
+                       ainode = 0;
+               }
+               add_mesh(mesh, aiscn->mMeshes[i], ainode);
        }
 
        aiReleaseImport(aiscn);
        return 0;
 }
 
        }
 
        aiReleaseImport(aiscn);
        return 0;
 }
 
-static int add_mesh(struct cmesh *mesh, struct aiMesh *aim)
+static int add_mesh(struct cmesh *mesh, struct aiMesh *aim, const struct aiNode *ainode)
 {
        int i, j, voffs, foffs;
 {
        int i, j, voffs, foffs;
+       const char *name;
+
+       if(ainode && ainode->mName.length > 0) {
+               name = ainode->mName.data;
+       } else {
+               name = aim->mName.data;
+       }
+       printf("adding mesh: %s\n", name);
 
        voffs = cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX);
        foffs = cmesh_poly_count(mesh);
 
        voffs = cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX);
        foffs = cmesh_poly_count(mesh);
@@ -82,7 +98,26 @@ static int add_mesh(struct cmesh *mesh, struct aiMesh *aim)
                                cmesh_push_index(mesh, aim->mFaces[i].mIndices[j] + voffs);
                        }
                }
                                cmesh_push_index(mesh, aim->mFaces[i].mIndices[j] + voffs);
                        }
                }
-               cmesh_submesh(mesh, aim->mName.data, foffs, aim->mNumFaces);
+               cmesh_submesh(mesh, name, foffs, aim->mNumFaces);
+       }
+       return 0;
+}
+
+static struct aiNode *find_node(struct aiNode *node, unsigned int midx)
+{
+       unsigned int i;
+       struct aiNode *n;
+
+       for(i=0; i<node->mNumMeshes; i++) {
+               if(node->mMeshes[i] == midx) {
+                       return node;
+               }
+       }
+
+       for(i=0; i<node->mNumChildren; i++) {
+               if((n = find_node(node->mChildren[i], midx))) {
+                       return n;
+               }
        }
        return 0;
 }
        }
        return 0;
 }