Jump to content

  • Log In with Google      Sign In   
  • Create Account

Having problems updating vbo


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 tempvar   Members   -  Reputation: 152

Like
0Likes
Like

Posted 13 February 2013 - 08:20 AM

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



Sponsor:

#2 Sponji   Members   -  Reputation: 1256

Like
0Likes
Like

Posted 14 February 2013 - 12:22 AM

Hmm, shouldn't you use GL_DYNAMIC_DRAW instead? http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml says "The data store contents will be modified once and used many times" for static.


Derp

#3 tempvar   Members   -  Reputation: 152

Like
0Likes
Like

Posted 14 February 2013 - 05:46 AM

Tried GL_DYNAMIC_DRAW but no change. 



#4 Waterlimon   Crossbones+   -  Reputation: 2574

Like
0Likes
Like

Posted 14 February 2013 - 07:30 AM

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


o3o


#5 mhagain   Crossbones+   -  Reputation: 8000

Like
0Likes
Like

Posted 14 February 2013 - 11:30 AM

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.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#6 tempvar   Members   -  Reputation: 152

Like
0Likes
Like

Posted 14 February 2013 - 06:42 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS