Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Having problems updating vbo

Started by tempvar Feb 13, 2013 at 2:20 PM 4 replies 2.2k views
Original Post
tempvar
tempvar

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 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

tempvar
tempvar

Tried GL_DYNAMIC_DRAW but no change.

Waterlimon
Waterlimon

Are you using the right amount of vertices/triangles/whatever when doing the draw call?

o3o
21st Century Moose
21st Century Moose

Set a breakpoint, run in your debugger, and double-check that the value of "offset" is what you expect it to be in your Update function.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls. 
tempvar
tempvar

Are you using the right amount of vertices/triangles/whatever when doing the draw call?

THAT WAS IT! Thank you very much, can't believe I didn't consider that.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.