3

In my opengl app, I am drawing the same polygon approximately 50k times but at different points on the screen. In my current approach, I do the following:

  • Draw the polygon once into a display list
  • for each instance of the polygon, push the matrix, translate to that point, scale and rotate appropriate (the scaling of each point will be the same, the translation and rotation will not).

However, with 50k polygons, this is 50k push and pops and computations of the correct matrix translations to move to the correct point.

A coworker of mine also suggested drawing the entire scene into a buffer and then just drawing the whole buffer with a single translation. The tradeoff here is that we need to keep all of the polygon vertices in memory rather than just the display list, but we wouldn't need to do a push/translate/scale/rotate/pop for each vertex.

The first approach is the one we currently have implemented, and I would prefer to see if we can improve that since it would require major changes to do it the second way (however, if the second way is much faster, we can always do the rewrite).

Are all of these push/pops necessary? Is there a faster way to do this? And should I be concerned that this many push/pops will degrade performance?

2 Answers 2

4

It depends on your ultimate goal. More recent OpenGL specs enable features for "geometry instancing". You can load all the matrices into a buffer and then draw all 50k with a single "draw instances" call (OpenGL 3+). If you are looking for a temporary fix, at the very least, load the polygon into a Vertex Buffer Object. Display Lists are very old and deprecated.

Are these 50k polygons going to move independently? You'll have to put up with some form of "pushing/popping" (even though modern scene graphs do not necessarily use an explicit matrix stack). If the 50k polygons are static, you could pre-compile the entire scene into one VBO. That would make it render very fast.

4
  • The polygons are not going to be moving independently, so hopefully that simplifies things. However, attributes of the objects may change independently, such as color. Would it still make sense to compile the whole scene into a single vbo? Oct 25, 2011 at 19:55
  • 1
    Possibly. It still depends on the nature of the changes. I would say yes. You could mark your vertex VBO as GL_STATIC_DRAW (indicating to the GPU that you won't be altering it all that much). Mark your color VBO as GL_DYNAMIC_DRAW (tells the GPU you will change it a lot). You then have to know how to calculate the offsets when editing that VBO, but it could be done.
    – TheBuzzSaw
    Oct 25, 2011 at 20:00
  • Also, what is the nature of the color changes? If there is a mathematical pattern to the changes, it could also be handled in a shader!
    – TheBuzzSaw
    Oct 25, 2011 at 20:01
  • The color changes could be for example if the user clicks on an object it turns to a "selected" color. Or the user can actually select "color these items blue". There may be other changes, not just colors though, such as shape or size as well. Oct 25, 2011 at 20:07
3

If you can assume a recent version of OpenGL (>=3.1, IIRC) you might want to look at glDrawArraysInstanced and/or glDrawElementsInstanced. For older versions, you can probably use glDrawArraysInstancedEXT/`glDrawElementsInstancedEXT, but they're extensions, so you'll have to access them as such.

Either way, the general idea is fairly simple: you have one mesh, and multiple transforms specifying where to draw the mesh, then you step through and draw the mesh with the different transforms. Note, however, that this doesn't necessarily give a major improvement -- it depends on the implementation (even more than most things do).

2
  • Unfortunately I can only count on openGL 2.1. Users may have higher, but that's our minimum requirement. But it sounds like you are agreeing that it should be drawn as a single scene and storing all the polygons at their respective locations in the scene and then just drawing the whole scene at once? Is that correct (similar to the 2nd idea I posed from my co-worker)? Oct 25, 2011 at 19:53
  • Jerry, please see my comment to @TheBuzzSaw. Does it matter that the colors of individual objects will change independently of others? Would I need to redraw the entire mesh each time any of these attributes changed? Oct 25, 2011 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.