I am currently trying to draw a isometric map in batches, these batches are meshes put together from map data. For each wall and floor I am creating a quad and add that to the chunks mesh. Because I am using alpha blending for a lot of objects I generate these meshes in the order they should be drawn and draw the combined chunk meshes back to front and bottom to top for multiple height levels. This works all great and I can draw a huge part of my map while remaining my target FPS of 60.The meshes and objects go allong the normal axis and I just rotate a Orthographic camera to get it's isometric projection matrix.
Now comes the tricky part, the dynamic objects have transparency as well, they are also just quads with a transparent texture. If I draw a mesh later in sequence but behind a transparent object in the world that mesh won't be visible trough the transparency of the object in front of it. I guess this is because the object behind does not exist at the moment of drawing the front object and so it is not being rendered on it's transparent pixels. If there is an obvious not too expensive solution for this issue I am saved. I need to draw moving transparent meshes in between these walls and objects belonging to the chunk mesh and I do not know OpenGL good enough to know if there is a trick for this. I can think of two unwanted options:
- Adding these dynamic objects to the chunks in the right draw does not seem like a proper solution since that means rearranging the whole mesh each time something moves.
- Dump the whole chunk idea and just draw each object individually and deal with the loss in frames in other area's.
- Making dynamic objects full 3D instead of just a quad with a texture. Now I can just draw it before the chunk and depth sorting should sort it. However, I cannot use any transparency on these objects which is a sever limitation and I wanted to avoid going "full 3D". Besides that, I might want to add 2D particle effects at a much later stage so I am really a much happier man if I can sort the drawing out.
- Don't combine the transparent objects in the chunks mesh and draw all these later, together with the dynamic meshes and properly sorted.
The latter seems like the best option now but it feels hacky and error prone. This is a whole other question but if this is a viable solution are there good and proven ways to add and remove meshes/vertex data/indices from a mesh and keeping vertex data and indices properly sorted, I also need to add meshes in the proper draw order as I explained earlier. I guess I need to keep this data somewhere when I create my chunk meshes and look it up later. Anyway, a proper solution (magic trick) to get the draw order and transparency correct in the first place would be awesome. I am using LibGDX btw and here is some code I use for drawing.
Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
Gdx.gl.glEnable(GL20.GL_BLEND);
// I tried a lot of different combinations of blend functions, but as always this comes closest.
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
// Very basic shader just taking in position and UV coordinates.
// If the shader can do anything for me in regard of my problem I'd really like to know.
shader.begin();
shader.setUniformMatrix("u_worldView", cam.combined);
// If I draw the player before the chunk the transparent pixels of the player would not render the chunk.
player.getMesh().render(shader, GL20.GL_TRIANGLES);
// Drawing the mesh chunk combines of floors, walls and objects of each tile within the chunk.
chunk.getCombinedMesh().render(shader, GL20.GL_TRIANGLES);
// If I draw the player after the chunk the player would not be drawn if it is behind a transparent mesh like a window or a tree.
player.getMesh().render(shader, GL20.GL_TRIANGLES);
shader.end();
So is this drawing order problem really that complicated or expensive to easily solve by OpenGL or have I been looking in the wrong places for a solution?