0
votes

If I have multiple meshes in a single VBO, is there a way to apply transformations to those meshes independently?

If I call glDrawElements while a VBO with two meshes is bound, it's going to draw those two meshes with whatever the current MVP matrix is. So if I have upwards of 20 independently moving meshes on the screen at a time, would I need 20 VBOs? Or is there some way of specifying an offset and a count to glDrawElements so that it only renders the first mesh stored in the VBO? This way I could change the MVP Matrix and then render the second mesh in the VBO.

1

1 Answers

1
votes

Say you have three meshes with 5, 11 and 13 triangles respectively packed into one index buffer, you can draw the two separate meshes as:

// assumes index buffer has been bound
// set modelview
glDrawElements(GL_TRIANGLES, 5 * 3, GL_UNSIGNED_SHORT, 0);
// set modelview
glDrawElements(GL_TRIANGLES, 11 * 3, GL_UNSIGNED_SHORT, 5 * 3 * sizeof(unsigned short));
// set modelview
glDrawElements(GL_TRIANGLES, 13 * 3, GL_UNSIGNED_SHORT, (5 + 11) * 3 * sizeof(unsigned short));

You specify the offset by offsetting the pointer in the last argument.