developer
developer

Reputation: 678

Visual artifacts when loading OBJ model with Assimp

I am trying to integrate the Assimp loader to my framework. Everything is rendered fine, but in this spider model I'm rendering, its fangs are not being drawn as expected (see following picture).

spider

Below is the relevant code snippet:

//Storing the Indices
for (unsigned int t = 0; t < mesh->mNumFaces; ++t) {
    aiFace* face = &mesh->mFaces[t];
    memcpy(&faceArray[index], face->mIndices, 3*sizeof(unsigned int));
    index += 3;
}

//Storing the Vertices
for (unsigned int t = 0; t < mesh->mNumVertices; ++t) {
    aiVector3D vertex ;
    if (mesh->HasPositions()) {
        vertex = mesh->mVertices[t];
        memcpy(&vertexArray[index], &vertex,3*sizeof(float));
    }
    index += 3;          
}

//Render module
void model::helperDraw(GLuint vertexBufferID, GLuint indexBufferID, GLuint textureID)
{
    GLint indexSize;
    glBindBuffer(GL_ARRAY_BUFFER,vertexBufferID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexBufferID);
    glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &indexSize);
    glBindTexture( GL_TEXTURE_2D, textureID);
    glDrawElements(GL_TRIANGLES, indexSize/sizeof(GLuint), GL_UNSIGNED_INT, 0);
}

What could be wrong with my code?

Upvotes: 3

Views: 1307

Answers (1)

glampert
glampert

Reputation: 4411

There is nothing obviously wrong with your code. One possible cause for these rendering artefacts is that the OBJ model you load has some faces that are triangles an some faces that are not. You are rendering everything as GL_TRIANGLES, but the OBJ format can specify faces as quads, triangle-strips, triangles and even other more exotic things like patches.

Assimp has a mesh triangulation facility that can make your life a lot easier when dealing with these multi-format mesh files, such as the OBJ. Try passing the flag aiProcess_Triangulate to the load method of the importer or even to the post-processing method if you do post-processing as a separate step. This is likely to fix the issue.

Upvotes: 2

Related Questions