Skip to main content
gamedev.net
🔒 Locked

glBufferData and glBufferSubData

Started by PlasmicSoup 5 replies 16.1k views
Original Post
PlasmicSoup
PlasmicSoup
Can anyone tell me how these two functions are different? glBufferSubDataARB(GL_ARRAY_BUFFER, 0, (end - start) * sizeof(vertexarray[0]) * 3, vertexarray); glBufferDataARB(GL_ARRAY_BUFFER, (end - start) * sizeof(vertexarray[0]) * 3, vertexarray, GL_DYNAMIC_DRAW_ARB); .... glBufferData works fine, while BufferSubData doesn't work at all... the *sub* function only displays a small portion of vertex data, and it doesn't even appear to resemble what is there, but I can't figure out how the functions are any different from each other. edit: screenshots here
-plasmicsoup
StealthNinja
StealthNinja
BufferSubData doesn't allocate memory, BufferData does.
PlasmicSoup
PlasmicSoup
I've already called BufferData to allocate the memory when I set up the buffer names and such.
-plasmicsoup
MARS_999
MARS_999
Well if you don't map the data then you should setup your VBO with glBufferData() and then when you want to update the VBO you could call glBufferSubData() instead of glBufferData which IIRC will reallocate memory for the VBO again, where with glBufferSubData will not and if you want to update the whole VBO just call

const unsigned int ARRAYSIZE = 100;struct Vertex{ float x,y,z;};Vertex *data = new Vertex[ARRAYSIZE];unsigned int size = sizeof(Vertex) / sizeof(float) * ARRAYSIZE;glBufferSubData(GL_ARRAY_BUFFER, 0, size, data);


or just a part of it

const unsigned int ARRAYSIZE = 100;struct Vertex{ float x,y,z;};Vertex *data = new Vertex[ARRAYSIZE];unsigned int startIndex = 10 * 3;// would be the 10th vertex due to 3 floats per vertexunsigned int size = 10 * 3;// replace the next 10 verticesglBufferSubData(GL_ARRAY_BUFFER, startIndex * sizeof(float), size * sizeof(float), data);
PlasmicSoup
PlasmicSoup
glGenBuffersARB(1, &vertexbuffername);		glBindBufferARB(GL_ARRAY_BUFFER, vertexbuffername);		glBufferDataARB(GL_ARRAY_BUFFER_ARB, maximumvertices * 3 * sizeof(float), 0, GL_DYNAMIC_DRAW_ARB);		glVertexPointer(3, GL_FLOAT, 0, 0);// i use drawelementsglGenBuffersARB(1, &indicesbuffername);		glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER,  indicesbuffername);		glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, maximumelements * sizeof(uint), 0, GL_DYNAMIC_DRAW_ARB);


this should be enough to set it up right?

my source for updating the buffer seems to be the same... except that for the sub routine right now i'm always starting at 0 but normally i have it at start (vertex # in the array) * 3 (for each component) * sizeof(float)

thanks for the replies so far!
-plasmicsoup
PlasmicSoup
PlasmicSoup
How does the allocation of BufferData work? I'm finding it matters where/when I first use it. I BufferData with NULL and just the size of my array to set it up... when I do that at program launch with the rest of my opengl initialization the program performs erratically as described above... but if I use that same BufferData call later on, right before I use BufferSubData to upload my new vertex data, then the program works normally.

Any info is appreciated... I've been reading a ton of docs and it's all starting to go over my head!
-plasmicsoup
PlasmicSoup
PlasmicSoup
Hmm. It appears to actually be something really really stupid where somehow my constants aren't being evaluated right... (they were used to set the size of the data buffered)... So anyway, thanks for your help.

The lesson to be learned is that when you know what your checking is right... check something else!

:-)
-plasmicsoup

Topic Locked

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

Sign in to reply to this topic.