1
votes

I've been programming OpenGL for some time now but I'm quite new to OpenGL ES. One of issues I'm having is drawing primitives on the screen in an efficient maner.

I need to draw a number of equal line loops on the screen (with different translation) and noticed a huge performance drop with this code:

gl.glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, this.vertices);

for (int j = 0; j < height; j++)
{
    for (int i = 0; i < width; i++)
    {
        gl.glPushMatrix();

        gl.glTranslatef(i, j, 0);
        gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 4);

        gl.glPopMatrix();
    }
}

I figure that the glDrawArrays is the bad guy here.

So the question is how can I draw a bunch of line loops efficiently? The glDrawArrays must be called for each primitive when using GL_LINE_LOOP so do I need to change the type to GL_LINES? This would impose a huge memory waste since the vertex array (in code above, holding 4 vertices) will then hold width * height * 8 vertices.

I'm programming for Android OpenGL ES 1.1.

2

2 Answers

1
votes

I figure that the glDrawArrays is the bad guy here.

Yes and no. You are right in saying that DrawArrays is not a fast call, but it is the fastest way of rendering something if you don't call it often.

so do I need to change the type to GL_LINES?

Yes, so that you can draw everything with one draw call

This would impose a huge memory waste since the vertex array (in code above, holding 4 vertices) will then hold width * height * 8 vertices.

Compared to just 4 vertices, I guess you could see it as a memoty waste. But GPU's are built to render big batches at a time, it will hate you for drawing lot's of little batches.

Also, you should have more than enough memory to hold the vertices, that shouldn't be a problem.

1
votes

I figure that the glDrawArrays is the bad guy here.

The bad guy is you, making such inefficient use of your function calling/context switching budget. Pack all your lines into one array (as GL_LINES) and send it in one single call to the GPU. And no, you're not wasting memory, because you can't allocate less than one page (usually 4kiB) anyway.