I'm trying to change my program to use VBOs instead of immediate rendering for performance reason. The rendering consists of thousands of hexagons, all of the same size/shape. Each hexagon has a different color, so I'm trying to figure out how to change the color of the VBO for each rendered hex. Here's the code I'm using right now:
private static final int NUM_VERTICES = 6;
private static final int VERTEX_SIZE = 2;
private static final int COLOR_SIZE = 3;
private int vboVertexHandle;
private int vboColorHandle;
// This is called once on initialization
private void initVbo() {
DoubleBuffer vertexBuffer = BufferUtils.createDoubleBuffer(VERTEX_SIZE * NUM_VERTICES);
FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(COLOR_SIZE * NUM_VERTICES);
for (Point vertex : WorldHandler.TILE_VERTICES) {
vertexBuffer.put(new double[]{vertex.x(), vertex.y()});
colorBuffer.put(new float[]{1f, 0f, 0f});
}
vertexBuffer.flip();
colorBuffer.flip();
vboVertexHandle = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboVertexHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuffer, GL15.GL_STATIC_DRAW);
GL30.glBindVertexArray(0);
vboColorHandle = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboColorHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, colorBuffer, GL15.GL_STATIC_DRAW);
GL30.glBindVertexArray(0);
}
// This is called thousands of times per frame
private void drawHex(Color color) {
// Set up the vertex buffer
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboVertexHandle);
GL11.glVertexPointer(VERTEX_SIZE, GL11.GL_DOUBLE, 0, 0L);
// Set up the color buffer
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboColorHandle);
GL11.glColorPointer(COLOR_SIZE, GL11.GL_FLOAT, 0, 0L);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
GL11.glDrawArrays(GL11.GL_POLYGON, 0, NUM_VERTICES);
GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
}
Note that I'm using OpenGL in Java through LWJGL. One solution I found online is to create a color VBO at initialization for each tile, then access each one when rendering that tile. That would probably solve my problem, but it may be more expensive than I'd like considering the lookup time when there are potentially millions of hexes that are off-screen.
Is there anything else I'm doing that is glaringly wrong? Even with those solution I'm only getting ~20 fps which is obviously far lower than I'd like.