1
\$\begingroup\$

I am writing a little game, in C++, using OpenGL. I have a varying number of enemies that are identical objects; each one uses the same animated mesh and shader, with skinning performed by GLSL-shader. I need to draw all of these objects at once, and I am confused about the fastest and most convenient way to do it.

I see several ways to solve my problem:

  1. Creating a single Vertex Buffer Object (VBO) for the vertices and indices, and binding it to a Vertex Array Object (VAO) to draw it ten times by ten draw calls. In this way, after creating the VBOs we, can free the memory that stores the mesh vertices in the RAM, because we don't need it anymore.

  2. Upload all of the meshes into a single VBO for the vertices and indices, and draw this VAO at once. In this case, there is a need to add a lot of additional info, such as the attributes such as position of the mesh, bone matrices, etc. In this case, we continue to store vertex info in the RAM, in order to add vertex data into that global VBO.

  3. Upload all of the meshes into a single VBO for the vertices and indices, and bind it to a VAO. This time, we draw models one by one, sending uniforms for uniform skinning and animation for each model before the draw call. Like in the previous case, we store mesh vertices in the RAM.

  4. Using instancing - passing a uniform array of bone transform matrices and accessing it within the shader by instance id. In this case, we might get rid of the CPU-side mesh data, as we don't need it anymore; much like with the first option. I'm not sure about this option, because instancing is usually used for drawing a large number of mesh instances, rather than drawing 10 instances.

I'm currently not sure which method is faster, and just overall convenient. What is the fastest and most convenient way to draw a number of identical objects at once in OpenGL?

\$\endgroup\$
1
\$\begingroup\$

Obviously instancing is the fastest solution. But the question is: Do you really need it?

I'd say that generally for anything under 1000 instances you don't really need instancing. You seem to be writing your own engine. I'd say you write it in such a way, that if at a later time you need instanced rendering, you can implement it without changing your game logic code.

Also please note that as with any performance-related question: Profile it. How much does it cost for you to do that many calls? What platforms are you planning to support? etc...

TL;DR: Do it the simplest way(to me that is VAO), and come back and optimize it, when you need it(after profiling)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.