Errors when loading Assimp meshes

| | August 5, 2015

I am experiencing some oddities when loading meshes in Assimp. Some models render perfectly, while others are complete jumbles of triangles.

Some Example Images:

Teapot Correct
Teapot Actual

Flower Correct
Flower Actual

OBJ download links:

Teapot.obj
Flower.obj

I’ve searched far and wide, and I cant seem to pin down the cause of this. I have noticed while debugging that the value of ai_mesh->mNumVertices differs from what Assimp Viewer or Blender display. ai_mesh->mNumVertices is smaller in the Teapot example by 156 vertices, and larger in the Flower example by 20 vertices. I’ve considered that Assimp post-processing may be causing the difference here. However, I’m using the EXACT same post-processing flags used in Assimp Viewer, copied directly from the source code. Here is my Mesh Loader:

mesh::mesh(aiMesh *ai_mesh, model* parent_model) : mesh()
{
    name_ = ai_mesh->mName.length != 0 ? ai_mesh->mName.C_Str() : "";

    // Get Vertices
    if (ai_mesh->mNumVertices > 0)
    {
        for (int ii = 0; ii < ai_mesh->mNumVertices; ii++)
        {
            aiVector3D ai = ai_mesh->mVertices[ii];
            glm::vec3 vec = glm::vec3(ai.x, ai.y, ai.z);
            vertices_.push_back(vec);
        }
    }

    // Get Normals
    if (ai_mesh->HasNormals())
    {
        for (int ii = 0; ii < ai_mesh->mNumVertices; ii++)
        {
            aiVector3D ai = ai_mesh->mNormals[ii];
            glm::vec3 vec = glm::vec3(ai.x, ai.y, ai.z);
            normals_.push_back(vec);
        };
    }

    // Get mesh indexes
    for (int t = 0; t < ai_mesh->mNumFaces; t++)
    {
        aiFace* face = &ai_mesh->mFaces[t];
        if (face->mNumIndices != 3)
        {
            std::cout << "[??] Mesh face with not exactly 3 indices, ignoring this primitive.n";
            continue;
        }

        indices_.push_back(face->mIndices[0]);
        indices_.push_back(face->mIndices[1]);
        indices_.push_back(face->mIndices[2]);
    }
    material_ = parent_model->getMaterial(ai_mesh->mMaterialIndex);
}

And render function, just in case.

void mesh::render()
{
    material_->bind();

    glVertexPointer(3, GL_FLOAT, 0, &vertices_[0]);
    glNormalPointer(GL_FLOAT, 0, &normals_[0]);

    glDrawElements(GL_TRIANGLES, indices_.size(), GL_UNSIGNED_BYTE, &indices_[0]);
}

Thanks ahead.

One Response to “Errors when loading Assimp meshes”

  1. Well, this is awkward. I overlooked the fact that the Teapot model has FAR more vertices than 255 (the size of GLubyte). I changed my call to glDrawElements and my indices_ vector to use unsigned integers, problem solved!

Leave a Reply