openglvertex-buffervertex-array-object

Draw multiple buffers with the same vertex layout


I have multiple objects, each one of them has its own index buffer, vertex buffer, some have a different shader or texture, and all of them have the same vertex format (x, y, z, u, v, nx, ny, nz). I don't want to batch my objects together, but render them in separate draw calls. Lets say that I want to render 5 different objects (cubes, spheres, etc.), do I have to create a new vao for each one of them, or is there a way to tell OpenGL that I want to render 5 different buffers with the same layout/format?


Solution

  • You don't need to tell OpenGL this; you simply modify the VAO and change the buffers without changing the vertex format.

    Now granted, you can't do that without providing the vertex format parameters to glVertexAttribPointer. So at the very least, you have to remember what those parameters still are.

    Fortunately, separate attribute format exists (in GL 4.3+), which allows you to change the buffer bindings (which are still stored in a VAO) separately from the functions that change the vertex format. So you should just be able to call glBindVertexBuffer and glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ...) to change the buffers.