views:

91

answers:

2

I'm drawing 2D shapes with OpenGL. They aren't using that many polygons. I notice that I can have lots and lots of shapes as long as they don't overlap. If I get a shape behind a shape behind.... etc.. it really starts lagging. I feel like I might be doing something wrong. Is this normal and is there a way to fix this (I can't omit rendering because I do blending for alpha). I also have CW backface culling enabled.

Thanks

A: 

You need to order your shapes front-to-back if they are opaque. Then the depth test can quickly and easily reject each pixel.

Then, you need to order them back-to-front if they are transparent. Rendering transparency out-of-order is very slow.

Edit: Hmm, I (somehow) missed the fact that this is 2D, despite the fact that the OP mentioned it repeatedly.

DeadMG
They are ordered by the order they are drawn, considering i'm drawing 2D shapes should I enable the depth buffer?
Milo
I'm doing glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Milo
How can I solve it in 2D? Thanks
Milo
On any kind of desktop computer, it will be faster to draw opaque objects front-to-back with depth check on. But transparent objects always need to be drawn back-to-front and won't benefit from depth buffering.
Alan
+2  A: 

Are your two cases (overlapping and non-overlapping) using the exact same set of shapes? Because if the overlapping case involves a total area of all your shapes that is larger than the first case, then it would be expected to be slower. If it's the same set of shapes that slows down if some of them overlap, then that would be very unusual and shouldn't happen on any standard hardware OpenGL implementation (what platform are you using?). Backface culling won't be causing any problem.

Whenever a shape is drawn, the GPU has to do some work for each pixel that it covers on the screen. If you draw the same shape 100 times in the same place, then that's 100-times the pixel work. Depth buffering can reduce some of the extra cost for opaque objects if you draw objects in depth-sorted order, but that trick can't work for things that use transparency.

When using transparency, it's the sum of the area of each rendered shape that matters. Not the amount of the screen that's covered after everything is rendered.

Alan
If I have same shape on top of itself 100 times its slower than 100 copies 5 units apart from each other.
Milo
What hardware/driver are you using? That should not happen in any hardware-accelerated GL implementation. Unless your non-overlapping case moves some of your shapes off the edge of the window, because the GPU can skip some work on objects outside the view frustum.
Alan