Android - Efficient way to draw tiles in OpenGL ES
- by Maecky
Hi,
I am trying to write efficient code to render a tile based map in android. I load for each tile the corresponding bitmap (just one time) and then create the according tiles. I have designed a class to do this:
public class VertexQuad {
private float[] mCoordArr;
private float[] mColArr;
private float[] mTexCoordArr;
private int mTextureName;
private static short mCounter = 0;
private short mIndex;
As you can see, each tile has it's x,y location, a color array, texture coordinates and a texture name.
Now, I want to render all my created tiles.
To reduce the openGL api calls (I read somewhere that the state changes are costly and therefore I want to keep them to a minimum), I first want to hand ALL the coordinate-arrays, color-arrays and texture-coordinates over to OpenGL. After that I run two for loops. The first one iterates over the textures and binds the texture.
The second for loop iterates over all Tiles and puts all tiles with the corresponding texture into an IndexBuffer. After the second for loop has finished, I call
gl.gl_drawElements()
whith the corresponding index buffer, to draw all tiles with the texture associated. For the next texture I do the same again.
Now I run into some problems:
Allocating and filling the FloatBuffers at the start of each rendering cycle costs very much time. I just run a test, where i wanted to put 400 coordinates into a FloatBuffer which took me about 200ms.
My questions now are:
Is there a better way, handling the
coordinate and color structures?
How
is this correctly done, this is
obviously not the optimal way? ;)
thanks in advance,
regards Markus