【问题标题】:Assimp + OpenGL loads only partlyAssimp + OpenGL 仅部分加载
【发布时间】:2019-02-27 09:02:54
【问题描述】:

我正在尝试绘制铁人模型的线框。但是有些部分丢失了,我觉得画了额外的边缘。我哪里出错了?

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);

我得到如下图像。在搅拌机上,模型完美加载。

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

【问题讨论】:

  • 可能每个网格的索引从 0 开始。但是如果将所有网格的所有顶点添加到同一个容器 (std::vector),那么第二个网格的顶点在之后开始第一个网格的顶点等等。您必须为索引添加一个起始偏移量。起始偏移量是size_t offset = vertices.size(),位于外部for 循环的开头。请注意,这只是一个猜测,因为我不知道索引。
  • 是的,我查过了。每个网格的索引从 0 开始。我完全错过了。非常感谢。
  • 您应该使用节点树 (scene->mRoot) 来处理 assimp 场景,因为其中可能有实例化网格及其自己的实例相对世界矩阵。

标签: c++ opengl glm-math assimp


【解决方案1】:

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

unsigned int offet = 0;
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]+offet );
        indices.push_back(scene->mMeshes[num_meshes]->mFaces[num_faces].mIndices[1]+offset);
        indices.push_back(scene->mMeshes[num_meshes]->mFaces[num_faces].mIndices[2]+offset); 
    }
    offset += num_vertices_per_mesh;
}

【讨论】:

    猜你喜欢
    • 2018-07-23
    • 2012-01-13
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 2014-02-05
    • 1970-01-01
    • 2017-09-27
    相关资源
    最近更新 更多