fatarms
fatarms

Reputation: 543

Incorrect vertex & normal access in 3D Model loading using ASSIMP & OpenGL

I use textured 3d model loading sample provided in ASSIMP's sample code. However, for some models, it doesn't seem to access the model's vertex and normal correctly. Here is a screenshot of the model loaded incorrectly:

enter image description here

While the model should be like the following screenshot:

enter image description here

In the first image, the roof of the building placed at the center-top of the front view. The floor is missing. I assume that this problem is caused by incorrect access to vertex positions and normals (CMIIW). The following is the snippet used to access both vertex position and normal:

        glBegin(face_mode);
        for(i = 0; i < face->mNumIndices; i++){
            int vertexIndex = face->mIndices[i];    // get group index for current index
            if(mesh->mColors[0] != NULL)
                Color4f(&mesh->mColors[0][vertexIndex]); 
            if(mesh->mNormals != NULL)
                if(mesh->HasTextureCoords(0)){  
                    glTexCoord2f(mesh->mTextureCoords[0][vertexIndex].x,  mesh->mTextureCoords[0][vertexIndex].y);                  
                }
                glNormal3fv(&mesh->mNormals[vertexIndex].x);
                glVertex3fv(&mesh->mVertices[vertexIndex].x);
        }
        glEnd();

How we can access the model's vertex positions and normals correctly?

Upvotes: 2

Views: 1789

Answers (1)

datenwolf
datenwolf

Reputation: 162164

What happens there is, that vertices with the same position are "shared". This is problematic, because (in OpenGL terms), a vertex is the whole combination of [position, normal, texture coordinates, other attributes…].

Somewhere in the exporter or loader this information is lost.

You can fix this in the 3D modeler, by splitting the mesh at hard edges, i.e. select contiguous patches of smoothly lit surfaces and turn them into individual submeshes (e.g. in Blender by the "Split Mesh" function, Hotkey 'Y').

Upvotes: 2

Related Questions