I am in the process of migrating away from legacy OpenGL calls in my 2D LWJGL game. Previously I would render each sprite using immediate mode, which was dead easy - bind the texture, add the vertices, and it's done.
Now I want to use vertex arrays. Ideally I would put the vertices for ALL my entities in one big array and render it in a single draw call. The problem? Each entity type uses a different texture, and
glDrawArrays uses just one texure at a time.
Here are some possible solutions I came up with:
1) Create a separate vertex array for each entity
This is by far the easiest solution, but it somehow seems wrong to me, because it's so similar to using immediate mode. Doesn't this defeat the benefit of using vertex arrays?
2) Create a separate vertex array for each entity type
In other words, all entities that have the same texture will be rendered together. This isn't ideal as it would involve a good chunk of extra work and some extra effort on the CPU to separate the entities by type, and more importantly, would affect the z-ordering of the entities.
For example, it would be impossible to have a Tree that appears in front of one Squirrel but behind another Squirrel.
That is, unless I introduced a depth buffer, but I was trying to avoid this as it adds another level of complexity and, as I understand it, more work on the GPU (although that probably doesn't matter too much).
3) Combine all textures using a texture atlas
If I was able to use a single texture for all entities, I could use a single vertex array. However, there are 2 problems with this:
I have not been able to find a good library for creating a texture atlas at runtime, and writing this code myself would be non-trivial.
I am worried that some GPUs may have a prohibitively small texture size limit.
- Would the first solution be viable / advisable?
- Am I being foolish for trying to avoid using the depth buffer?
- Am I missing something here? Is there a better solution?