[OpenGL] DrawElements - element array buffer object disabled? [SOLVED]

Hi guys!

Yet again opengl is giving me a lot of attitude and I can’t figure out why…

I am trying to draw a rectangle using 2 triangles drawn with glDrawElements.
No textures just color.
I am in the same program drawing a lot of rectangles with texturing and that is still working but a simple rectangle does not work…

When I use the code below I get the error below the code, saying that “element array buffer object is disabled”.
If I try to use glDrawElements with a shortbuffer directly I get the exact opposite error: “Element array buffer object is enabled”.

I have made sure that all the vbo-IDs are not -1. So they are there…

Also it worked when I used glDrawArrays, but it stopped working when I converted to glDrawElements.

This truly confuse me so i hope that someone else can spot the problem :slight_smile:


public void render()
{	
	//If there is nothing to render return
	if(numberOfVerticies == 0)
		return;
	
	glUseProgram(shaderProgramID);		
	
	//Send the data to the vertex-vbo
	buffer.flip();
	glBindBuffer(GL_ARRAY_BUFFER, vboID);
	glBufferData(GL_ARRAY_BUFFER, buffer, GL_DYNAMIC_DRAW);
	
	//Send the indecies to the index-vbo
	indexBuffer.flip();
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ivboID);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL_DYNAMIC_DRAW);
	
	//Bind the vao
	glBindVertexArray(vaoID);
	
	//Draw the triangles
	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
	
	//Reset the batcher
	buffer.clear();
	indexBuffer.clear();
	numberOfVerticies = 0;
}


org.lwjgl.opengl.OpenGLException: Cannot use offsets when Element Array Buffer Object is disabled
	at org.lwjgl.opengl.GLChecks.ensureElementVBOenabled(GLChecks.java:105)
	at org.lwjgl.opengl.GL11.glDrawElements(GL11.java:1117)
	at graphics.ShapeBatcher.render(ShapeBatcher.java:325)
	at graphics.SpriteBatcher.checkMode(SpriteBatcher.java:471)
	at graphics.SpriteBatcher.render(SpriteBatcher.java:428)
	at startMenu.StartMenu.render(StartMenu.java:106)
	at stateBasedGame.StateBasedGame.start(StateBasedGame.java:49)
	at Main.main(Main.java:53)

How is your VAO set up?

Like this:


private void setupBuffers()
{
	//Setup VAO and activate the first 2 attributes
	vaoID = glGenVertexArrays();
	glBindVertexArray(vaoID);
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);
			
	//Setup vertex-VBO
	vboID = glGenBuffers();
	glBindBuffer(GL_ARRAY_BUFFER, vboID);
	
	//Setup VAO pointers
	int bytesPerFloat = 4;
	int numberOfVertecies = 2;
	int numberOfColors = 4;
			
	int bytesPerVertex = (numberOfVertecies + numberOfColors) * bytesPerFloat;
	
	//Giv VAO'en vertex-VBO'en på index 0
	glVertexAttribPointer(0, 2, GL_FLOAT, false, bytesPerVertex, 0);
			
	//Giv VAO'en color-VBO'en på index 1
	int colorOffset = numberOfVertecies * bytesPerFloat;
	glVertexAttribPointer(1, 4, GL_FLOAT, false, bytesPerVertex, colorOffset);
	
	//Setup index-VBO
	ivboID = glGenBuffers();
}

But as I said it worked fine with glDrawArrays ???

You need to bind the index buffer when creating your VAO. The index buffer binding is stored in the VAO.

Hey that worked! :smiley:

Also I had to bind the vao before the index-buffer when rendering.
I did not do this when rendering with my spritebatcher, but I guess it becomes an issue when you have multiple vaos :stuck_out_tongue:

Thank you very much!
I would never have figured that out :slight_smile:

Lots of good info here: http://en.wikipedia.org/wiki/Vertex_Buffer_Object