I'm making an Android app and I need to draw some polygons using OpenGL ES. I'm able to render them but I'm disappointed with performance. I updated the code to use VBO but I didn't notice any improvement. I want to render at 60 frames per second (16 ms per frame).
I have a test project where I render several triangles on the screen. When I render 1000 triangles it takes about 20 ms per frame (depending on the device).
I want to keep the rendering under 10 ms because I need the rest (6 ms) to perform other calculations (e.g. update positions, detect collisions, etc.).
Here is the code where I render a triangle:
public void render(int positionLocation, int colorLocation)
{
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.bufferIndex);
GLES20.glVertexAttribPointer(positionLocation, Polygon.POSITION_COMPONENT_COUNT, GLES20.GL_FLOAT, false, Polygon.STRIDE, 0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
GLES20.glUniform4f(colorLocation, this.red, this.green, this.blue, this.alpha);
GLES20.glDrawArrays(this.mode, 0, this.length);
}
https://github.com/mauriciotogneri/test/blob/master/src/com/testopengl/Polygon.java#L51-66
Here is the code where iterate over the triangles:
private void renderTriangles()
{
long start = System.nanoTime();
for (int i = 0; i < COLUMNS; i++)
{
for (int j = 0; j < ROWS; j++)
{
float x = (i * TRIANGLE_SIZE) + (TRIANGLE_SIZE / 2);
float y = (j * TRIANGLE_SIZE) + (TRIANGLE_SIZE / 2);
moveTo(x, y);
this.triangles[i][j].render(this.positionLocation, this.colorLocation);
}
}
long stop = System.nanoTime();
this.totalTime += stop - start;
this.totalFrames++;
Log.d("TEST", (this.totalTime / this.totalFrames) / 1000000 + " ms");
}
https://github.com/mauriciotogneri/test/blob/master/src/com/testopengl/MapRenderer.java#L117-139
(Change the value of NUMBER_OF_TRIANGLES to display more triangles)
For what I understand, the method GLES20.glDrawArrays(...) takes too much time if I need to call it 1000 times per frame (one per triangle).
Is there another way to render several polygons that doesn't take too much time?
Notes:
- In the example all the triangles have a fixed position on the screen but in the real scenario they will move around
- In the example I assign a random color to each triangle but in the real scenario each of them will have a fixed color