Optimisation problem with terrain rendering.

I’m calling the following method for each tile of a 64x64 terrain grid onscreen, and I’m finding that each method call takes the equivalent of about 10,000 clock cycles. Clearly, I must be doing something wrong.



    protected void renderTo(Viewer v, GL rGL) {
      
      currTex = type.texture;
      currUV = UV[1][1];
      currTex.bindTex(rGL);
      rGL.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, WHITE, 0);
      rGL.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, WHITE, 0);
      rGL.glMaterialfv(GL.GL_FRONT, GL.GL_EMISSION, BLACK, 0);
      rGL.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, BLACK, 0);
      rGL.glMaterialf(GL.GL_FRONT, GL.GL_SHININESS, 0);
      
      rGL.glTexCoordPointer(2, GL.GL_FLOAT, 0, currUV);
      rGL.glVertexPointer(3, GL.GL_FLOAT, 0, vertB);
      rGL.glNormalPointer(GL.GL_FLOAT, 0, normB);
      
      rGL.glDrawArrays(GL.GL_TRIANGLES, 0, 12);
    }

(bindTex(), by the way, is no more than:

     if(cached) bGL.glBindTexture(GL.GL_TEXTURE_2D, glID);

…so I’ve no real idea why on earth this should be so expensive.)
I’m using a shiny new 2.2 GHz Intel Core 2 Duo Macbook, with a GeForce 8600M GT. Each terrain tile is simply a set of 4 polys arranged around a centre of origin. What can I do to seriously optimise?

You could try to set the materials just once.
You’re passing a java array to glTexCoordPointer (and I guess the same is true for all other glXPointer calls). You should see a much better performance when you switch to vertex buffer objects (VBO) with direct buffers instead.

[quote]You could try to set the materials just once.
[/quote]
Ideally, yes, but the textures actually vary from tile to tile. I’m working on a system to render tiles with a similar texture state in one batch, and tests thus far seem promising, but I haven’t quite finished it off yet. (Problem is, there’s not much point to using VBOs in that case, though I’ll definitely give them a look.) Even if I cut out the material and texture routines, glDrawArrays seems to eat up about 75% of the rendering time. What I can’t understand is why it’s so slow in the first place when I’m only rendering 4 vertices apiece. Individual vertex calls actually seem to be faster.

Maybe there is some general overhead that disqualifies glDrawArrays with small arrays. Try to upload all the data in a big array at once and just vary the first and count arguments of glDrawArray.

I got some replies on the GameDev boards which indicate that’s likely the problem. Thanks for all the help.