Search code examples
arrayspointersopenglvertexvertex-buffer

Adding up buffers into one


I have a mesh consisting of several entries. Every entry contains it's own list of faces, vertices, normals, colors and texture coordinates.

Can I loop though all of my entries and use glVertexAttribPointer to cummulate data of an attribute in a single buffer object, like this?:

glBindBuffer(vbo);
for(Entry* e : entries) {
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, e->vertices);
    ...
}

In other words, will repeated calls on glVertexAttribPointer for attribute 0 of buffer vbo rewrite the data pointed on before or not?

If yes, is there any effective solution out of copying all vertices into one consecutive memory block before calling glVertexAttribPointer only once for the whole buffer?


Solution

  • glVertexAttribPointer does only store (for each attribute) the last information you supplied to it. So appending buffers is not possible by this method.

    You have two options when you have a situation like yours:

    1. Issue for each buffer a separate draw-call
    2. Copy the data off all buffers into a single buffer and issue one draw-call for it. Note that in this case the indices might have to be adjusted to point to the correct positions in the combined buffer.