Model not drawing when using Index Buffer Object

When I pass my index data directly in glDrawElements() everything works fine. But when I use vao and vbo the model isn’t drawn at all.

If you remove vao and vbo code and uncomment the commented it works fine.

Below is the code


GLfloat *vertexData;
GLuint* indices;
int size;
ObjParser* parser = ObjParser::getObjParser();

void display() {
	glTranslatef(0, 0, -8);
	glDrawElements(GL_TRIANGLES,size,GL_UNSIGNED_INT, (void*)0);

	//This works fine
	//glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT, indices);

void init() {
	glClearColor(0, 0, 0, 0);
	gluPerspective(50, 1, 5, 20);

	//Just parsing the obj file
	vertexData = parser->getVertexData();
	indices = parser->getIndices();
	size = parser->getIndicesSize();

	GLuint vaoId;
	GLuint vboId[2];

	glGenVertexArrays(1, &vaoId);
	glGenBuffers(2, vboId);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId[0]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, vboId[1]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
	glVertexPointer(3, GL_FLOAT, 0, 0);

	//This works fine
	/*glVertexPointer(3, GL_FLOAT, 0, vertexData);

int main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(10, 10);
	glutCreateWindow("My Window");
	return 0;

I didn’t look closely, but isn’t sizeof(vertexData) tiny, like 4 bytes? Don’t you need something like (vertex_count * sizeof(vertexData)) ??? And similar for the IBO?

Also, my vague recollection is, for some odd reason you need to create VAOs before the IBO and VBO associated with the VAO. Looks like you do that.

I can’t imagine why setting the last argument of glDrawElements() to “indices” makes any sense.

Always beware of the possibility it is drawing, but is not visible for some reason (outside of frustum or black color or something).

Where are vertex attributes specified? You need to breakpoint and look at the arguments to all the data copy functions and the draw function.

I’m probably not giving good advice here… too long since I wrote and tested my VAO/IBO/VBO code.

You can’t use [var]sizeof[/var] to obtain the size of a dynamically-allocated array; it will return the size of the pointer (typically 4 bytes or 8 bytes).

So the version using buffers is copying hardly any data to the buffers.

Hopefully the ObjParser class returns the size of the vertex array (it wouldn’t be much use if it didn’t). It appears to return the size of the index array, but you aren’t using it when copying data to the index buffer, only when calling glDrawElements().

Ohh!! Thanks that works.