Reputation: 7594
Is it possible to add more data to a VBO without blowing away the old contents. Basically is there a way to resize the VBO while keeping the old contents?
For example, lets say I interleaved some color(CrCgCb) and position(PxPy) data in a VBO to represent a square using two triangles, such that a abstract representation of memory in the VBO looked like:
[P1x,P1y,C1r,C1g,C1b,P2x,P2y,C2r,C2g,C2b,P3x,P3y,C3r,C3g,C3b,P4x,P4y,C4r,C4g,C4b,P5x,P5y,C5r,C5g,C5b,P6x,P6y,C6r,C6g,C6b]
Now some event transpires and all of a sudden another square is born and I want to place it in the same VBO such that the data now looks like:
[P1x,P1y,C1r,C1g,C1b,P2x,P2y,C2r,C2g,C2b,P3x,P3y,C3r,C3g,C3b,P4x,P4y,C4r,C4g,C4b,P5x,P5y,C5r,C5g,C5b,P6x,P6y,C6r,C6g,C6b,P7x,P7y,C7r,C7g,C7b,P8x,P8y,C8r,C8g,C8b,P9x,P9y,C9r,C9g,C9b,P10x,P10y,C10r,C10g,C10b,P11x,P11y,C11r,C11g,C11b,P12x,P12y,C12r,C12g,C12b]
To place the first batch of data I would do this:
GLuint vbo;
glGenBuffers(1, &vbo);
float verticies[6 * 2 + 6 * 3] = ...;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
Now to get the second square in there I normally would do something as follows:
glDeleteBuffers(1, &vbo);
glGenBuffers(1, &vbo);
float verticies[12 * 2 + 12 * 3] = ...;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
How do you do this without having to call glDeleteBuffers()
?
Upvotes: 0
Views: 2583
Reputation: 5068
You are looking for glBufferSubData. This can not resize the buffer, but can change data in an existing buffer. Just make sure your buffer is big enough from the beginning. A common pattern is to initialize the buffer with BufferData and a null pointer to a certain size and then change only parts of it with BufferSubData.
Upvotes: 1