[4.1] VAO glBindVertexArray - Invalid Operation??

Hi
I have been trying to get VAO’s to work with my engine using 4.1 compatability context on nvidia 270.61 drivers.

It seems to run ok except that I get 1282 invalid operation
inside my draw method when I try to bind the VAO.

Other than that it renders it ok. Could someone please have a quick look at my code to see if I got something wrong?


void Terrain::GenVAO(std::shared_ptr<TILE>tile)
{
//VBOs//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//Generate VBOs
	glGenBuffers(2, tile->VBOIBO);

	//Vertex buffer
    glBindBuffer(GL_ARRAY_BUFFER, tile->VBOIBO[0]);
	glBufferData(GL_ARRAY_BUFFER, tile->Vertices.size() * sizeof(VortexVertex), &tile->Vertices[0].pos.x, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);

	//Index buffer
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tile->VBOIBO[1]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, tile->Indices.size() * sizeof(unsigned int), &tile->Indices[0], GL_STATIC_DRAW);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	//tile->Vertices.clear(); //Why cant I uncommend this? I dont understand why the buffer needs to store its own memory + point to my own memory
	//tile->Indices.clear();

//VAOs//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//Generate VAOs
	glGenVertexArrays(1, &tile->VAO);
    glBindVertexArray(tile->VAO);
	glBindBuffer(GL_ARRAY_BUFFER, tile->VBOIBO[0]);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VortexVertex), BUFFER_OFFSET(0));
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tile->VBOIBO[1]);
	glBindVertexArray(0);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

void Terrain::Draw(std::shared_ptr<TILE>tile)
{
	glBindVertexArray(tile->VAO);
	glDrawElements(GL_TRIANGLES, tile->Indices.size(), GL_UNSIGNED_INT, BUFFER_OFFSET(0));
	glBindVertexArray(0);
}

Where do you check your GL errors ? I don’t see any glGetError in your posted code.

Please, be more consistent.

I removed them in order to display the code more clearly here.
I had one after each statement.

I enable attrib arrays before binding to the vbo. I don’t unbind the vbo inside the vao. Try with this.

According to http://www.opengl.org/sdk/docs/man4/xhtml/glBindVertexArray.xml

GL_INVALID_OPERATION is generated if array is not zero or the name of a vertex array object previously returned from a call to glGenVertexArrays.

Do you get a problem even with binding the VAO directly after generating it, checking that an error doesn’t exist from a previous OpenGL call? That’s fairly strange if you do.

glGenVertexArrays(1, &tile->VAO);
glBindVertexArray(tile->VAO);

If you were using the core profile, you’re calling glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tile->VBOIBO[1]); the first time without a VAO bound, but this shouldn’t matter in compatibility profile.

As for why the two commented lines don’t work:

//tile->Vertices.clear();
//tile->Indices.clear();

Does calling tile->Indices.Clear() set the value that will be returned by tile->Indices.size() to zero? Because you use tile->Indices.size() in your glDrawElements call.

@Dan Bartlett: Thanks so much man, I was stupid with that tile->Indices.size() thing which I completely forgot about. I am now using much less memory thanks to you.

As for the buffer problem, it has magicaly resolved itself when I shrunk down the triangle counts per VBO to around 3333. Weird I know but no more error.