Hi,
I have been working on a project for school that is supposed to load in a .obj file exported from Blender3d (a modeling program) and display the model using OpenGL's VBOs. I have verified that my custom .obj file parser works correctly and the vertex and index information is being stored correctly in their respective arrays (using std::vector template). The problem is, it seems like the indice or vertex information is somehow off because what is displayed is sort of a twisted box. An image of this may be found at:
VBO Error 1 . The .obj file that was exported via Blender3D may be found
here. The unit cube (box) should look like what is shown in the middle of the 'VBO Error 1' screenshot. When I use glDrawArrays for the first 4 elements of the array (since they are in order), I get what is shown in this
picture. For proof that my file loader worked, check out
this screenshot of Visual Studios Debugger in action.
My VBO IDs and arrays are initialized as follows:
GLuint m_vertexBufferID;
GLuint m_vertexIndexBufferID;
vector<GLfloat> m_vertexBuffer;
vector<GLuint> m_vertexIndexBuffer;
The code for the initialization of the index and vertex VBOs as well as the rendering code is shown below:
Initialization:
glGenBuffers(1, &m_vertexBufferID); //Generate a buffer for the vertices
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferID); //Bind the vertex buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * m_vertexBuffer.size(),
&m_vertexBuffer[0], GL_STATIC_DRAW); //Send the data to OpenGL
glGenBuffers(1, &m_vertexIndexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vertexIndexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GL_UNSIGNED_INT) *
m_vertexIndexBuffer.size(), &m_vertexIndexBuffer[0], GL_STATIC_DRAW);
Rendering:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(-10.0, -6.0, 0.1, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferID);
glVertexPointer(3, GL_FLOAT, 0, (char *) NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vertexIndexBufferID); // for indices
glIndexPointer (GL_UNSIGNED_INT, 0, (char *) NULL );
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, static_cast< void * >(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
If anyone has any clue as to what might be happening, please post a reply. Thanks!