Hello,
So I wrote a simple .obj models parser, and attempted to render a simple cube model with indices. Everything seems to be working fine, until I come to render the cube with a glDrawElements() call.. it crashes returning -1073741819. I've been struggling with this for a day now, so I'd really appreciate if you'd help me with this.
Here is my code:
Loading the stuff:
vector<Vertex> CubeModel, CubeNormals;
vector<GLuint> CubeIndices;
vector<TexCoord> CubeTexCoords;
LoadObjModel("simple cube.obj", CubeModel, CubeTexCoords, CubeNormals, CubeIndices);
GLuint CubeId = InitializeVBOs(&CubeModel, 8);
GLuint CubeIndicesId = InitializeIBOs( &CubeIndices, 36);
The InitializeVBOs/IBOs functions:
GLuint InitializeVBOs(std::vector<Vertex> * vertices, int number)
{
GLuint VBOid;
glGenBuffers(1, &VBOid);
glBindBuffer(GL_ARRAY_BUFFER, VBOid);
glBufferData(GL_ARRAY_BUFFER, number*sizeof(Vertex), vertices, GL_STATIC_DRAW);
return VBOid;
}
GLuint InitializeIBOs(vector<GLuint> * indices, int number)
{
GLuint IBOid;
glGenBuffers(1, &IBOid);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBOid);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, number*sizeof(GLuint), indices, GL_STATIC_DRAW);
return IBOid;
}
The drawing part:
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, CubeId);
glVertexPointer(3, GL_FLOAT, 0, NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, CubeIndicesId);
glDrawElements( GL_TRIANGLES, 36, GL_UNSIGNED_INT, NULL ); //there are 36 indices; 3 for each triangle making half a face
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Here is the .obj file content:
I am pretty sure the .obj parser works correctly. Feel free to compare the values with the watch values of the arrays (I am aware the indices values are subtracted by 1):
Content of "CubeModel" (vertices): http://prntscr.com/68emn7
Content of "CubeIndices": http://prntscr.com/68emcn
If I am missing any other info, let me know and I will provide them immediately. Thanks in advance!