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

Removing items with glBufferSubData

Started by tempvar Apr 29, 2014 at 12:02 PM 3 replies 2.8k views
Original Post
tempvar
tempvar

Hey guys,

I'm currently working on making a text drawing module in my project. It's working for the most part but i'm having some trouble when trying to 'backspace' characters (removing a quad from the back of the buffer).

So at the moment this is how I'm doing things

1. Create a buffer object with a size enough to hold 120 or so characters (a character is a quad made up of 6 vertices)

2. When the user presses a key, add 6 new vertices to the end of the vbo (with correct tex coords for the character) using glBufferSubData

This works fine and I can add those 120 characters by pressing letters on the keyboard (It re-builds the buffer by another 120 characters when you reach the limit)

Now the problem is I want to erase a character when the backspace (39) key is pressed It doesnt visually appear to have an effect until you add another character.

Example use case:

- Press 'a'

- Press 'b'

- Press 'c'

- Press 'd'

My buffer contains 4 quads (each quad is 6 vertices) and on screen it prints "abcd" fine.

Now If i press backspace once it should read - "abc"

but it still reads - "abcd"

However if I add a new character say 'e' (after backspace) the text becomes - "abce". Which leads me to believe it did in fact remove the last quad from the buffer BUT was not updated till more data was written to that area of memory.

So it really did erase that character but it doesnt get updated till you add another quad

This is how I'm updating my buffer after a backspace:


void Text2D::PopLetter()
{
glBindBuffer(GL_ARRAY_BUFFER, m_vbo->vData->m_vboId);

// remove 6 vertices from the end of the vector
m_vbo->vData->vertices.pop_back();
m_vbo->vData->vertices.pop_back();
m_vbo->vData->vertices.pop_back();
m_vbo->vData->vertices.pop_back();
m_vbo->vData->vertices.pop_back();
m_vbo->vData->vertices.pop_back();

int currSize = m_vbo->vData->vertices.size() * sizeof(Vertex);
int amountToOverwrite = sizeof(Vertex) * 6;

glBufferSubData(GL_ARRAY_BUFFER, currSize, amountToOverwrite, nullptr);
}

So after this function call, the last quad is still visible, yet when I add another character the old one is just replaced with the new one if that makes sense.

Is there a better way to do this?

Thanks guys

tempvar
tempvar

Hmmm well I'm an idiot.

I fixed it. I was not updating the size in the call to glDrawArrays(...), so it was still drawing the size of the vertices before I had typed a backspace...

Time for sleep :)

samoth
samoth

Changing your call to glDrawArrays alone is sufficient in that case, too.

There is no way you can remove data from a buffer object. You can only allocate a buffer object and throw it away, or allocate a new one and implicitly throw the old one away ("orphaning"), or you can overwrite the contents without changing the size.

Simply calling glDrawArrays with 4 fewer vertices will do the trick for "removing" a char, no need to modify buffer data (which requires a PCIe transfer and is a possible pipeline stall). Only if you remove a character in the middle, you may have to modify the buffer, obviously (or you need to draw elements rather than arrays and change the indices, or you must use two draw calls which is not very practical).

alh420
alh420




int currSize = m_vbo->vData->vertices.size() * sizeof(Vertex);
int amountToOverwrite = sizeof(Vertex) * 6;

glBufferSubData(GL_ARRAY_BUFFER, currSize, amountToOverwrite, nullptr);

Not only is glBufferSubData unnecessary in this case (as samoth explains), but I don't think this line does what you think it does...

Wouldn't this line risk a seg. fault for trying to read from a nullptr? Maybe the driver checks it so its ok, but I see no mention in the OpenGL docs of this method supporting giving it a nullptr for data...

tempvar
tempvar

int currSize = m_vbo->vData->vertices.size() * sizeof(Vertex);
int amountToOverwrite = sizeof(Vertex) * 6;

glBufferSubData(GL_ARRAY_BUFFER, currSize, amountToOverwrite, nullptr);

Not only is glBufferSubData unnecessary in this case (as samoth explains), but I don't think this line does what you think it does...

Wouldn't this line risk a seg. fault for trying to read from a nullptr? Maybe the driver checks it so its ok, but I see no mention in the OpenGL docs of this method supporting giving it a nullptr for data...

Yeah I don't need to call it anymore for removing characters, if I was removing from the middle or something I'd probably need to call it to shift the rest of the data back. If editing rather than removing I can simply just update the texture coords of those verts.

21st Century Moose
21st Century Moose

This depends on what you want to achieve.

If your objective is to simulate the effects of a backspace key at the end of a line of text, then you don't even need to go near the buffer: chaging the parameter to glDrawArrays so that you're drawing one less quad is enough. Then when the user types the next character you can just issue a glBufferSubData to overwrite the previous last character.

But the point is that "removing" the last character from the buffer is not actually needed to meet this objective. There's no need to "remove" it if all you want to do is just not draw it: that can be achieved by leaving it there but just not drawing it.

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

Topic Locked

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

Sign in to reply to this topic.