I'm sending a large number of data to the vertex shader. I use glBufferData to generate my VBO. Later on I have to replace the data in my VBO so I do the following:
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * models().size(), &models[0], GL_DYNAMIC_DRAW);
What I noticed is when I used the above code (I'm assuming) that some of the previous data is left in the VBO because I not only draw the new objects I want to but also some of the old ones.
Therefore I had to change my code to the following:
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * models().size(), &models[0], GL_DYNAMIC_DRAW);
The reason I don't like the above aproach is because according to Khronos
When replacing the entire data store, consider using glBufferSubData rather than completely recreating the data store with glBufferData. This avoids the cost of reallocating the data store.
How do I replace the whole VBO with my new data, but if my new data is smaller than the old data then how do I reallocated the VBO efficiently? Or is there something equivalent to a NULL terminating character where the graphics card can know not to read any further data?
The reason I ask this is because from my understanding glBufferStorage is like glBufferData except that the data store generated by glBufferStorage is immutable meaning that the size of the data store cannot be changed, as stated on Khronos and on Stackoverflow.
glBufferStorage and glNamedBufferStorage create a new immutable data store.
But if glBufferData creates a data store which cannot shrink in size then what's the point of it? (Unless I'm misunderstanding its intended use)
data storeof size 2MB then I useglBufferSubDatato add new data which is 1.5MB. Wouldn't there be an extra 0.5MB of data in thedata storewhich I don't potentially want to draw? – Archmede Jan 23 '18 at 00:25primitiveswithglDraw*. Is that correct? – Archmede Jan 23 '18 at 00:43data storewith new terrain but this new terrain might contain less data than the old terrain. So wouldn't there be some data from the old terrain left in thedata storeif the new terrain contained less data? Make sense? – Archmede Jan 23 '18 at 01:37glBufferStorageandglBufferData. I assumedglBufferSubDatacan reallocate thedata storebut I was mistaken. Thanks for your help though, it's greatly appreciated. – Archmede Jan 23 '18 at 02:12