0
votes

I am looking for a way to dynamically edit the data that displays for a vertex buffer object. I have tried glBufferSubData, glMapBuffer, glBufferData, and some others, however had no luck. I have found that the time consuming method is glBindBuffer. I think I am using VBOs right, but I am not completely sure. Heres some sample code of my problem:

    verticesId = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, verticesId);
    glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STREAM_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    normalsId  = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, normalsId);
    glBufferData(GL_ARRAY_BUFFER, normalsBuffer, GL_STREAM_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    texturesId  = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, texturesId);
    glBufferData(GL_ARRAY_BUFFER, texturesBuffer, GL_STREAM_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

the verticesBuffer and other variables are the FloatBuffers that have the data in them. Next, I render them this way:

    glEnableClientState(GL_VERTEX_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, verticesId);
    glVertexPointer(vertexSize, GL_FLOAT, 0, 0);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, texturesId);
    glTexCoordPointer(2, GL_FLOAT, 0, 0);

    glDrawArrays(GL_QUADS, 0, amountOfVertices);

    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);

Here is how I edit the VBOs:

    int position = 0;

    ///////////////////////////////////////////////////////////////////

    glBindBuffer(GL_ARRAY_BUFFER, verticesId);

    mapBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY, null);

    verticesBuffer = mapBuffer.order(ByteOrder.nativeOrder()).asFloatBuffer();

    verticesBuffer.position(position);

    // ... edit some values in the 'vertices' float array ...

    verticesBuffer.put(vertices);

    verticesBuffer.rewind();

    glUnmapBuffer(GL_ARRAY_BUFFER);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

Is there any way to speed up the glBindBuffer method, or am I doing this wrong? And also, how should I edit the data for the most efficiency.

1
can you provide some code where you actually change those VBOs? or maybe you recreate them every frame? - fen
Sorry its a little vague. I am trying to get only the parts I need out of some big code. - Braden Steffaniak

1 Answers

0
votes

You can speed up the drawing part by using Vertex Array Objects.

About the uploading part, your code is fine; what you can do is "double buffering": keep a copy of the data, process it using another thread (multiple processor system are frequent nowadays), and then upload with glBufferSubData.

Another option is the use of SIMD assembly, but this depends on your application.