Hello Guest

Generate hex grid in a single mesh

  • 5 Replies
  • 5721 Views
Generate hex grid in a single mesh
« on: October 22, 2013, 20:45:55 »
I'm trying to make a hexagonal grid for a RTS game, and the way I originally intended to draw the grid, it was causing terrible lag because each hex tile was a separate Render object, and those are called in a loop. The way I now think is the best is to make each tile part of a larger Render call, except I don't know how to generate the vertices and indices like this efficiently. If anyone could help that would be great.

*

Offline Cornix

  • *****
  • 488
Re: Generate hex grid in a single mesh
« Reply #1 on: October 22, 2013, 21:16:06 »
How many triangles are you rendering right now? Most of the time the issue is not the mesh but the way you render things.
With a somewhat new card you should easily be able to render hundred thousands of triangles per frame with steady 60 frames per second.
With my, now 4 years old, I get 1.5 million triangles with steady 60 frames per second.

Re: Generate hex grid in a single mesh
« Reply #2 on: October 22, 2013, 21:32:33 »
It's much less than that. A couple thousand, max.

*

Offline Cornix

  • *****
  • 488
Re: Generate hex grid in a single mesh
« Reply #3 on: October 22, 2013, 22:21:24 »
How do you draw? Immediate mode? (that is glBegin(...) {...} glEnd())
It is generally much faster to use either Display-Lists or VBO's for static portions of the game which terrain usually is. Even if it changes regularily VBO's can still be much faster then immediate mode.

You should also keep in mind to switch textures and shaders as little as possible. Try to sort your draw calls by texture and shader and batch them together. Each time you switch a texture or shader it costs you dearly in performance. In my tests is switching shaders is far worse then textures, but I dont know if this is true on all hardware.
You might want to use a texture atlas instead of having separate textures for each tile-type. (basically having all textures in one big image)

You might also want to try drawing the hexagons as quads with transparent corners.

Re: Generate hex grid in a single mesh
« Reply #4 on: October 23, 2013, 16:14:12 »
I'm drawing by generating vertices/indices in the constructor and then using glDrawElements to draw. I'm not sure what to call that.
I'm not texturing them yet, but when I am I will make sure to use a texture map.
I'll try messing with the drawing a bit to remove the switching of shaders.

*

Offline Cornix

  • *****
  • 488
Re: Generate hex grid in a single mesh
« Reply #5 on: October 23, 2013, 16:25:10 »
Maybe you would like to show your code.