Good, so we need to minimize the number of state changes and attract challenges. I assume that you are using modern OpenGL, including vertex buffer objects and shaders.
One approach is to ensure that all vertex data has the same format. For example, each vertex has a coordinate of position, color and texture (xyz, rgba, uv). If we alternate our vertex data in VBO, we only need one call to glVertexAttribPointer and glEnableVertexAttribArray before rendering.
This means some redundant data for non-textured objects, but we get everything to get everything in one batch, which is nice.
To process non-textured objects, you can snap an empty white texture and treat it as a textured object. Or you can have a single variable (floating between 0 and 1) in your fragment shader and mix the texture color and the vertex color using the mix function.
In order for packet sprites and forms, we must first process the transformations on the CPU, so that we always load the "world" coordinates into the GPU. This eliminates the need to establish a uniform transformation for each sprite, each of which requires an individual call call.
In addition, we need to sort by texture whenever possible, since texture bindings are among the more expensive operations you can do.
Our approach basically boils down to the following:
- Support one Vertex- and Index Buffer object for data storage
- Store all vertex data in one format and interleave data in VBO
- Texture Sort
- Discard data (draw elements / arrays) in buffers whenever we change the texture (or set a uniform texture-mixture if we go with this option)
Getting data from the CPU to the GPU memory can be done in different ways. For example, first allocating a large enough empty memory buffer on the GPU and using glBufferSubData to load a subset of vertex / index data each time you make one of your Render calls.
Do not forget the profile!
It is very important to perform profiling when performing this kind of work. For example, to compare performance between batch and individual drawing calls or glDrawArrays vs glDrawElements. I recommend using gDebugger , which is a free and very good OpenGL profiler.
Also note that too much VBO can damage your performance . So keep it in reasonable size and be with him with an appeal to draw whenever it is filled.