Fabrizio
Fabrizio

Reputation: 1074

Is it possible render two different buffers with different indices starting point in the same call in OpenGL?

I have these two objects, defined like so: Object 1:

vertices = {...}
indices = {1, 2, 3, 4, 5, ...} 

And Object 2:

vertices = {...}
indices = {1, 2, ...}

Where vertices is a vector of floats that gets pushed to the GL_ARRAY_BUFFER and indices is a vector of unsigned that gets pushed to the GL_ELEMENT_ARRAY_BUFFER.

These objects do not have transformations that need to be applied to them and are relatively static but can be modified by the user.

I didn't want to create a single object that contains all the vertices of the two objects because I wanted the modifications to these objects to be as fast as possible, so the smaller the object the faster it is to rebuild the mesh. I know that two draw calls on two small objects are slower than one draw call on a single, even if the object is a bit bigger, so I wanted to batch them together.

My problem is that I can push or remove vertices from the buffer with glBufferSubData, but for the indices, I would need to loop through every value of the second object and offset it to account for the indices of the first object, so it would end up being slower probably.

The objects can have very different sizes, so I don't think fixed offset could be a viable solution either.

Is it possible to tell OpenGL that the indices of the first object are referred to the vertices of the first object and the indices of the second object are referred to the vertices of the second object? In this case, I would be able to modify only a part of the buffer object, without the need to offset the indices for each object every time.

Upvotes: 1

Views: 67

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473322

The baseVertex parameter of the various BaseVertex rendering commands are meant to do essentially what you're asking for. The baseVertex parameter is an integer that gets added to each index in the rendering command before fetching that any vertex attributes from the vertex buffers.

Let's say this is your index buffer:

0 1 2 1 3 2 0 1 2 1 3 2
|  Mesh 0  |  Mesh 1  |

If mesh 0 has 4 vertices, then you pass 4 as the baseVertex to glDrawElementsBaseVertex or a similar command. This effectively makes the indices in mesh 1 4 5 6 5 7 6.

Upvotes: 1

Related Questions