c++openglglutwavefront

Drawing a model from obj file


I'm trying to write an obj viewer with openGl. This program has to draw only lines of model's faces, so i need to load:

Now i load only obj file, with 3 element per face, so i can draw the element in GL_TRIANGLE mode, but i'm getting somes trouble with some models:

http://people.sc.fsu.edu/~jburkardt/data/obj/icosahedron.obj

the loading phase seems working good, i think the problem is in the render() function:

static void render(void)
{
    glClearColor( 0.0f, 0.0f, 0.0, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glBindBuffer(GL_ARRAY_BUFFER, g_resources.vertex_buffer);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(
        3,                                /* size */
        GL_FLOAT,                         /* type */
        3*sizeof(GLfloat),                /* stride */
        (void*)0                          /* array buffer offset */
    );

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_resources.element_buffer);

    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glDrawElements(
        GL_TRIANGLES,           /* mode */
        theModel->face.size(),  /* count */
        GL_UNSIGNED_INT,        /* type */
        (void*)0                /* element array buffer offset */
    );

    glDisableClientState(GL_VERTEX_ARRAY);

    glutSwapBuffers();
}

I have also some questions:


Solution

  • if you are getting issue only with some models, try to disable GL_CULL_FACE to see the difference it can be the vertex order thing.

    Indices in OBJ are 1 based, GL ones are 0 based, so you need -1 when making a buffer.