Assimp + OpenGL仅部分加载

时间:2018-09-23 19:06:54

标签: c++ opengl glm-math assimp

我正在尝试绘制Ironman模型的线框。但是我感觉有些部分丢失了,并且绘制了额外的边缘。我在哪里弄错了?

std::vector<glm::vec3> vertices;
std::vector<GLuint> indices; 
for(unsigned int num_meshes=0; num_meshes<scene->mNumMeshes; num_meshes++)
{
    // lets store all the vertices.
    for(unsigned int num_vertices_per_mesh=0; num_vertices_per_mesh<scene->mMeshes[num_meshes]->mNumVertices; num_vertices_per_mesh++)
    {
        glm::vec3 vertex(scene->mMeshes[num_meshes]->mVertices[num_vertices_per_mesh].x, scene->mMeshes[num_meshes]->mVertices[num_vertices_per_mesh].y, scene->mMeshes[num_meshes]->mVertices[num_vertices_per_mesh].z);
        vertices.push_back(vertex);
    }
    // lets store all the indices or faces.
    for(unsigned int num_faces=0; num_faces<scene->mMeshes[num_meshes]->mNumFaces; num_faces++){
        indices.push_back(scene->mMeshes[num_meshes]->mFaces[num_faces].mIndices[0]);
        indices.push_back(scene->mMeshes[num_meshes]->mFaces[num_faces].mIndices[1]);
        indices.push_back(scene->mMeshes[num_meshes]->mFaces[num_faces].mIndices[2]); 
    }
}

GLfloat *vertices_array = &vertices[0].x;
GLuint size_of_vertices_array = vertices.size() * 3 * sizeof(GLfloat); // vertices data size * x,y,z values per vertex * sizeof(GLfloat)

GLuint *indices_array = indices.data();
GLuint size_of_indices_array = indices.size()*sizeof(GLuint);
// ===========================================================================================
GLuint VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);

glBindVertexArray(VAO); // Bind vertex array objects first before VBOs
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, size_of_vertices_array, vertices_array, GL_STATIC_DRAW);

// attribute 0 vertex positions
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GL_FLOAT), (GLvoid*)0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size_of_indices_array, indices_array, GL_STATIC_DRAW);

glBindVertexArray(0);               // unbinding VAO

/****** other lines of code ***********/
main_loop()
{
    /***** other lines of code *********/
    glBindVertexArray(VAO);
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);

我得到的图像如下。在Blender上,模型可以完美加载。 current output

我哪里出错了?我能够只用一个网格完美地加载对象!

1 个答案:

答案 0 :(得分:0)

正如Rabbid76之前所说:当所有顶点都存储在一个顶点缓冲区中时,您需要重新编号索引。只需将当前索引增加顶点数量的偏移量即可:

2018-09-29 11:21:00.000  INFO [leaderboard,c2f241d8a806fd26,c2f241d8a806fd26,false] 7937 --- [   scheduling-1] i.s.l.task.CronClass    : Cron-Job Notification....
2018-09-29 11:21:00.000  INFO [leaderboard,c2f241d8a806fd26,c2f241d8a806fd26,false] 7937 --- [   scheduling-1] i.s.l.task.CronClass    : Cron-Job executed at: 2018-09-29 11:21:00.0