qwertyu uytrewq
qwertyu uytrewq

Reputation: 317

Bugs with loading obj file with c++ and opengl

I wrote obj loader and got following: enter image description here

It is yellow eagle but as you see it has some additional triangles that go from its leg to wing. The code that I used:

{....
    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    glBufferData(GL_ARRAY_BUFFER,sizeof(data),data,GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,numOfIndices*sizeof(GLuint),indices,GL_STATIC_DRAW);
}

void Mesh::draw( )
{

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    glVertexAttribPointer(
            0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
            3,                  // size
            GL_FLOAT,           // type
            GL_FALSE,           // normalized?
            0,                  // stride
            (void*)0            // array buffer offset
        );
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ibo);
    glDrawElements(GL_TRIANGLES,numOfIndices,GL_UNSIGNED_INT,(void*)0  );
    glDisableVertexAttribArray(0);
}

Where data is array of vertices and indices is array of indices.

When I take and save data and indices in obj format and open resulting file in 3D editor eagle looks fine and doesn't have these additional triangles (that implies that both data and indices are fine).

I spent hours trying to to fix code and make eagle look normal but now I run out of ideas. So please if you have any ideas how to make eagle normal share them with me.

For those who think the problem is in loader here is screen of obj model that is made out of data from loader (from data[] and indices[]) enter image description here

Upvotes: 0

Views: 344

Answers (1)

qwertyu uytrewq
qwertyu uytrewq

Reputation: 317

Finally found solution.

Indexing in obj. format starts at 1 (not 0 ) and when you load vertices to GL_ARRAY_BUFFER vertex #1 becomes vertex#0 and whole indexing breaks.

Therefore it is necessary to decrease all values of indices by 1 and then index that pointed to vertex #1 will point to vertex #0 and indexing will become correct.

Upvotes: 1

Related Questions