Changing data in Buffer Objects

My current code structure generates a maze using GL_LINES. I’m putting the vertex and index data into vertex buffer objects (GL_ARRAY_BUFFER and GL_ELEMENT_ARRAY_BUFFER) during the initialization stage of my code.

my glBufferData commands are currently set to GL_STATIC_DRAW.

What I want to do is to have a keypress that lets me go back and reinitialize the entire maze. I have the code now that calls the initialize maze command on keypress, and it appears to work fine. I’m clearing the maze array, clearing the vertex array, and clearing the index array, and then regenerating all of that data (m_vertices.push_back, m_indices.push_back) and running the following lines again:


	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
	glEnableClientState(GL_VERTEX_ARRAY);

	glGenBuffers(MAX_BUFFERS, &m_vbos[0]);
	glBindBuffer(GL_ARRAY_BUFFER, m_vbos[VERTEX_BUFFER]); //Bind the vertex buffer
	glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * m_vertices.size(), &m_vertices[0], GL_STATIC_DRAW); //Send the data to OpenGL

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vbos[INDEX_BUFFER]); //Bind the index buffer
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * m_indices.size(), &m_indices[0], GL_STATIC_DRAW); //Send the data to OpenGL

	glBindBuffer(GL_ARRAY_BUFFER, m_vbos[VERTEX_BUFFER]);
	glVertexPointer(3, GL_FLOAT, 0, 0);

And then it renders the new data properly

My question is…is it ok to just keep calling that block of code every time i want to regenerate? Or should I be destroying the buffers first and then recreating them with the above code? if I should be destroying them, what is the proper code and order to destroy or unallocate the buffers? I’m pretty new to this, so go easy :wink:

Thanks!

is it ok to just keep calling that block of code every time i want to regenerate?

There’s no need to keep calling glGenBuffers.
You can call glSubBufferData and fill the same buffer with new data, or if the buffer has changed in size, just use glbufferData instead.

Or should I be destroying the buffers first

You mean with glDeleteBuffers? No, no need to do that. Keep the same BufferID.

Thanks!

so I’ll need to separate all the buffer setup code from my maze generation/vertex/index code and on first run, call the bindbuffers and bufferdata…and then on all subsequent calls, only call the glbufferdata commands? Do the subsequent calls to glBufferData look the same or will the parameters need to change?

Edit: To be more specific, will I also need to call the glEnableClientState and glVertexPointer commands also on the subsequent runs?

Yes you’ll need to call glEnableClientState and glVertexPointer unless you are using VAOs.
You’ll need to separate the buffer creation from the buffer filling. The code for filling is exactly like you currently have.

Excellent…I think…Here’s what I have that works:

when I generate the maze and populate the m_vertices vector array, and then create the indices and populate the m_indices array…I call the above code, exactly as it is written.

I created a new routine that ONLY calls the two glBufferData lines out of that block (lines 6 & 9 if you include spaces) the same way they’re written. It appears to work, but I don’t want to be creating any memory leaks or overflows after many different maze generations. It appears that I’m just re-buffering new data right?

Given the code above, that’s VAO’s right?

Thank you so much for the help…I’m learning a WHOLE lot here.

Given the code above, that’s VAO’s right?

No, but that’s what the VAO object encapsulates. Vertex Array Objects have to generated and bound, just like the buffer object.