Hey,
The past couple of days I've been running into some troubles attempting to update my VBO. I've taken the approach of allocating extra size at the start, so that when I need to add some more sprites in there is room.
Basically I'm doing things like this:
Create all the sprites I need at the start and put them in a VBO, but make sure the VBO has space for extra sprites
// all my sprite data currently resides here
std::vector<Vertex> m_vertices;
// create an opengl VBO
glGenVertexArrays(1, &m_vaoId);
glBindVertexArray(m_vaoId);
// times 6 because there are 6 verts for a quad of two triangles
int size = (m_vertices.size() + (numExtraSprites * 6)) * sizeof(Vertex);
glGenBuffers(1, &m_vboId);
glBindBuffer(GL_ARRAY_BUFFER, m_vboId);
glBufferData(GL_ARRAY_BUFFER, size , NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, m_vertices.size() * sizeof(Vertex), &m_vertices[0]);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(12));
Now this is fine, everything works perfectly and draws as expected.
I've got it setup so that when i click with the mouse, it will add another sprite of the same type at the mouse location.
This just involves me creating a temporary std::vector<Vertex> with 6 new vertices in it positioned at the mouse location.
Then I attempt to update my VBO.
void SpriteBatch::Update()
{
int offset = m_vertices.size() * sizeof(Vertex);
int size = sizeof(Vertex) * 6; // 6 because there is one more sprite to be updated
glBindBuffer(GL_ARRAY_BUFFER, m_vboId);
glBufferSubData(GL_ARRAY_BUFFER,
offset,
size,
&m_tempSpriteVec[0]); // m_tempSpriteVec holds the new 6 verts
m_vertices.insert(m_vertices.end(), m_tempSpriteVec.begin(), m_tempSpriteVec.end());
m_tempSpriteVec.clear();
}
This does not work, even though the vbo should have sufficient size? I've noticed if I just update one of the existing sprites (say the first 6 vertices of the vbo) then It works, but shouldn't it be big enough for the new data?
Thanks guys