Increasing hexagon grid drawing performance.

Problems with Canvas, OpenGL, etc...

Increasing hexagon grid drawing performance.

Postby Quipeace » Sun Jun 05, 2011 3:59 pm

Hello all,

It's been a while, but I'm back once again with another question.

I'm working on a game that's going to be based on a hexagon grid. I'm currently drawing each individual hexagon (using TRIANGLE_STRIP) and then loop through all the tiles to draw them.

This is my hexagon:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class HexTile
  2. {
  3.         private float height;
  4.        
  5.         private int[] textures = new int[1];
  6.        
  7.         private float vertices[] = {     0.0f,   0.0f, 0.0f,    //center
  8.                                                                          0.0f,   1.0f, 0.0f,    // top
  9.                                                                         -1.0f,   0.5f, 0.0f,    // left top
  10.                                                                         -1.0f,  -0.5f, 0.0f,    // left bottom
  11.                                                                          0.0f,  -1.0f, 0.0f,    // bottom
  12.                                                                          1.0f,  -0.5f, 0.0f,    // right bottom
  13.                                                                          1.0f,  0.5f, 0.0f,     // right top
  14.         };
  15.  
  16.         private short[] indices = {      0, 1, 2, 3, 4, 5, 6, 1};
  17.  
  18.         //private float texture[] = { };
  19.  
  20.         private FloatBuffer vertexBuffer;
  21.         private ShortBuffer indexBuffer;
  22.         private FloatBuffer textureBuffer;     
  23.        
  24.         public HexTile()
  25.         {
  26.                 ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
  27.                 vbb.order(ByteOrder.nativeOrder());
  28.                 vertexBuffer = vbb.asFloatBuffer();
  29.                 vertexBuffer.put(vertices);
  30.                 vertexBuffer.position(0);
  31.                
  32.                 ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
  33.                 ibb.order(ByteOrder.nativeOrder());
  34.                 indexBuffer = ibb.asShortBuffer();
  35.                 indexBuffer.put(indices);
  36.                 indexBuffer.position(0);
  37.                
  38.                 /*ByteBuffer tbb = ByteBuffer.allocateDirect(texture.length * 4);
  39.                 tbb.order(ByteOrder.nativeOrder());
  40.                 textureBuffer = tbb.asFloatBuffer();
  41.                 textureBuffer.put(texture);
  42.                 textureBuffer.position(0);*/
  43.         }
  44.        
  45.         public void setHeight(float h)
  46.         {
  47.                 height = h;
  48.         }
  49.         public float getHeight()
  50.         {
  51.                 return height;
  52.         }
  53.        
  54.         public void draw(GL10 gl)
  55.         {
  56.                 gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
  57.        
  58.                 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
  59.                 //gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
  60.  
  61.                 gl.glDrawElements(GL10.GL_TRIANGLE_FAN, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
  62.         }
  63.        
  64.         public void loadGLTexture(GL10 gl, Context context)
  65.         {
  66.                 textures[0] = -1;
  67.             Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.hex);
  68.  
  69.             while(textures[0] <= 0)
  70.                 gl.glGenTextures(1, textures, 0);
  71.  
  72.             //gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
  73.            
  74.             gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
  75.             gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
  76.  
  77.             GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
  78.  
  79.             bitmap.recycle();
  80.         }
  81. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Now as I kind of expected this is killing the framerate. The framerate drops to 37 when drawing the worst case scenario (11 * 7 tiles) and that is without textures, just simple white hexagons.

I really don't have enough experience with openGL to figure out how to efficiently draw this grid. Each tile is going to have it's own texture, and will eventually be 3D (with only the front visible). So uuhm, what would be the most efficient way of drawing a grid like this?

I'd really appreciate if someone could help me with this!
Quipeace
Quipeace
Developer
Developer
 
Posts: 27
Joined: Tue Feb 23, 2010 4:00 pm

Top

Re: Increasing hexagon grid drawing performance.

Postby bxm » Wed Jun 08, 2011 10:37 pm

So, since nobody wants to answer, I figured I'd post something...

Is there a reason you want to draw hexagons instead of having a hexagon-shaped texture over a square? In which case you could use the glDrawTex extension?

In addition, I never bothered to familiarize myself with VBO's (vertex buffer objects) but you may want to look into them, as are supposed to be much faster than vertex array drawing. However, the code you've posted is the fastest you will be able to get that method to draw I'm afraid.
bxm
Junior Developer
Junior Developer
 
Posts: 24
Joined: Tue May 10, 2011 2:51 am

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 1 guest