Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Removing items with glBufferSubData


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 29 April 2014 - 06:02 AM

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


Edited by tempvar, 29 April 2014 - 06:05 AM.


Sponsor:

#2 tempvar   Members   -  Reputation: 152

Like
0Likes
Like

Posted 29 April 2014 - 06:31 AM

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



#3 samoth   Crossbones+   -  Reputation: 5877

Like
0Likes
Like

Posted 29 April 2014 - 06:41 AM

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



#4 Olof Hedman   Crossbones+   -  Reputation: 3830

Like
0Likes
Like

Posted 29 April 2014 - 06:55 AM


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



#5 tempvar   Members   -  Reputation: 152

Like
0Likes
Like

Posted 29 April 2014 - 10:00 PM

 


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. 



#6 mhagain   Crossbones+   -  Reputation: 9625

Like
0Likes
Like

Posted 30 April 2014 - 02:05 AM

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.


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.





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