Dynamic Index Buffers (IBOs) and glBufferSubData

Hello folks,

maybe I got it all wrong but is there anything wrong by dynamiclly changing my index buffer object using glBufferSubdata()?
It seems that my NVIDIA driver doesn’t copy my indices from client memory to GPU when I do this:

glGenBuffers(1, &id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)(indices * sizeof(uint32)), (GLvoid*)NULL, GL_STREAM_DRAW);

…and later in my game I try to create my scene using indices to tell OpenGL what to render and what not.

This approach WORKS - however its slow as hell on NV hardware:
{

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
uint32* idx = NULL;
myNewIndices = (uint32*)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
// copy indices into buffer “myNewIndices”
// …
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

And this approach messes up my geometry completely. It seems that the indices are not copied correctly to the GPU:
{

uint32 myNewIndices[1024];
// copy size indices into buffer “myNewIndices”
// …

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, size, myNewIndices);

}

Can anyone please give me a hint what I did wrong here?

Thanks a bunch in advance
Saski :slight_smile:

// copy size indices into buffer “myNewIndices”
// …

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, size, myNewIndices);

You’re trying to copy size indices into the buffer object. Therefore, you need to copy size * sizeof(uint32) bytes. Just like you allocated the buffer object with “indices * sizeof(uint32)” bytes.

eeehm…oh yeah that one slipped me totally :wink:

thx a lot for pointing that out! :slight_smile: