JulenissensHjelper
JulenissensHjelper

Reputation: 969

Should I use several glDrawArrays() or gather all the vertices to one big glDrawArrays-call?

I'm working on a personal Java OpenGL (JOGL) project and I'm using some custom objects with separate draw functions and vertices.

public class Cube extends PhysicalObject {

 public void draw(GL gl) {

         gl.glColor3f(1.0f, 1.0f, 0.0f);

         gl.glEnableClientState(GL.GL_VERTEX_ARRAY);  // Enable Vertex Arrays

            gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);  

            gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertices); 

            gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, texCoords); 

            gl.glDrawArrays(GL.GL_QUADS, 0, 4*6);  


            gl.glDisableClientState(GL.GL_VERTEX_ARRAY);  

            gl.glDisableClientState(GL.GL_TEXTURE_COORD_ARRAY); 

}

I then loop through a whole bunch of these cubes, calling their draw-functions. My question is the following: Should I gather all the vertices to one big glDrawArrays-call, i.e gathering all the vertices to one big array and draw that? Does it do much for performance and fps?

Upvotes: 4

Views: 2910

Answers (2)

Robert Rouhani
Robert Rouhani

Reputation: 14688

The general rule is to minimize the number of OpenGL calls, especially in languages like Java or C# where there's overhead to interfacing with native code. However, you shouldn't group together different objects if any of their properties will ever change (applying a different model matrix, having a different color, etc.) because it's not possible to apply two separate model matrices to different parts of the same draw call. So basically, if all of your cubes never change, it's better to group them all together, otherwise keep them separate.

Another thing that will be helpful with performance is to minimize the number of state changes. If you're drawing 10,000 cubes, move the glEnableClientState and glDisableClientState calls out of the cube draw method and only call them before/after all the cubes are drawn. If they're all using the same texture, bind the texture once at the beginning and unbind it once at the end.

Oh and if you're really worried about performance, most computers (even 2 year old netbooks) support OpenGL 1.5, so moving your data to VBOs will give you a significant performance benefit. And if you're doing something like Minecraft, then the best method of optimization is to go through all your cubes and only draw the surface faces.

Upvotes: 6

Erwald
Erwald

Reputation: 2216

If your concern is regarding performance, I don't think you'll see a very big change with either of you proposed implementations...

From my past experience, one thing that could make a performance increase would be to use List (better memory performance for sure).

This is a good Opengl bottleneck pdf

Upvotes: 1

Related Questions