2013-07-22 71 views

回答

2

下面是一個從assimp示例代碼中訪問網格索引的示例。

for (; n < nd->mNumMeshes; ++n) 
{ 
    const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]]; 

    apply_material(sc->mMaterials[mesh->mMaterialIndex]); 

    if(mesh->mNormals == NULL) { 
     glDisable(GL_LIGHTING); 
    } else { 
     glEnable(GL_LIGHTING); 
    } 

    for (t = 0; t < mesh->mNumFaces; ++t) { 
     const struct aiFace* face = &mesh->mFaces[t]; 
     GLenum face_mode; 

     switch(face->mNumIndices) { 
      case 1: face_mode = GL_POINTS; break; 
      case 2: face_mode = GL_LINES; break; 
      case 3: face_mode = GL_TRIANGLES; break; 
      default: face_mode = GL_POLYGON; break; 
     } 

     glBegin(face_mode); 

     for(i = 0; i < face->mNumIndices; i++) { 
      int index = face->mIndices[i]; 
      if(mesh->mColors[0] != NULL) 
       glColor4fv((GLfloat*)&mesh->mColors[0][index]); 
      if(mesh->mNormals != NULL) 
       glNormal3fv(&mesh->mNormals[index].x); 
      glVertex3fv(&mesh->mVertices[index].x); 
     } 

     glEnd(); 
    } 
} 
+0

謝謝你的代碼。但我的錯誤在其他地方。當我通過assimp讀取場景時,我只傳遞了aiProcess_Triangulate標誌,但爲了正確加載索引,還需要傳遞aiProcess_JoinIdenticalVertices。 – eugene