tags:

views:

137

answers:

1

I want to draw many polygons to the screen but i'm quickly noticing that it slows down quickly. As a test I did this:

for(int i = 0; i < 50; ++i)
{
        glBegin( GL_POLYGON);
        glColor3f( 0.0f, 1, 0.0f ); glVertex2f( 500.0 + frameGL.GetCameraX(), 0.0f + frameGL.GetCameraY());
        glColor3f( 0.0f, 1.0f, 0.0f ); glVertex2f( 900.0 + frameGL.GetCameraX(), 0.0f + frameGL.GetCameraY());
        glColor3f( 0.0f, 0.0f, 0.5 ); glVertex2f(900.0 + frameGL.GetCameraX(), 500.0f + frameGL.GetCameraY() + (150));
        glColor3f( 0.0f, 1.0f, 0.0f ); glVertex2f( 500 + frameGL.GetCameraX(), 500.0f + frameGL.GetCameraY());
        glColor3f( 1.0f, 1.0f, 0.0f ); glVertex2f( 300 + frameGL.GetCameraX(), 200.0f + frameGL.GetCameraY());
        glEnd();
}

This is only 50 polygons and already it's gtting slow. I can't upload them directly to the card because my program will allow the user to reshape the verticies.

My question is, how can I speed this up. I'm not using depth. I also know it's not my GetCamera() functions because if I create 500,000 polygons spread apart t's fine, it just has trouble showing them in the view. If a graphics card can support 500,000,000 on screen polygons per second, this should be easy right?

Thanks

+11  A: 
  1. as already mentioned don't do glBegin and glEnd in the loop but outside
  2. for even better performance use vertex arrays
  3. for optimal performance use vertex buffer objects

The solutions are ordered in how much speed gain you will get, and inversly how wide supported they are. That said, any modern graphics card supports all of those -- you just need to be careful when coding for embedded OpenGL systems.

Contemporary games (the ones that achieve your cited 500kk limit) all use at least VBO's (if not Geometry Shaders, but that's even a step beyond). To effectively learn the techniques mentioned I honestly suggest taking them a step at a time -- e.g. first learning display lists, then vertex arrays, then VBO's, because in practice each one builds on top of the former.

Immediate mode (characterized by using a GL command per object) is extremely slow, and even deprecated in the current GL standard -- a long theory short, it's because one of the most expensive graphics operation (apart from texture manipulation) are draw calls -- calls between the graphics card and the processor -- so in practice it's best to prepare all you can beforehand, and submit it to the GPU in one call (or as little as possible).

Good luck!

Kornel Kisielewicz
+1 for giving an ordered list instead of citing a single possibility as *the* solution.
Jerry Coffin
VBOs build on vertex arrays, but not on display lists. You can safely skip learning about display lists, or look at them briefly so you'll know when to use them.
Thomas
Just one thing, what if i'm using the GLU tessellation?
Milo
So basically I can just pass the verts from the tesselator to my vertexarray....
Milo
@Jerry Coffin : I'd almost considered down-voting the answer for the very same reason you like it. Display lists and Vertex Arrays *without* use of buffer objects is removed in OpenGL 3.1 core and later. Why do we persist teaching people to use legacy OpenGL functionality?
Mads Elvheim
@Mads, because of OpenGL ES and legacy embedded OpenGL-like API's on console platforms
Kornel Kisielewicz
@Mads: First, because removing them was a mistake. Second, because even for the (relatively few) people who really need VBOs, the others are useful stepping stones.
Jerry Coffin
@Kornel : OpenGL != OpenGL ES. Don't mix the two standards.
Mads Elvheim
@Mads, I don't mix them, but 99% of people asking about OpenGL do.
Kornel Kisielewicz
@Jerry : Sooner or later, people will start using OpenGL 3.x or 4.x over OpenGL 2.1, and I think we're doing new programmers a major disfavor by teaching them the old ways of doing things. Not only do they have to learn the new techniques, they also have to unlearn what we taught them. It's about time OpenGL users start to get with the program. Just my not-so-humble opinion.
Mads Elvheim
That OpenGL 3.x and 4 is overly complex to use, that I do agree with, though.
Mads Elvheim
@Mads:Basically, you're saying Khronos is in charge, and the programmers should do what they're told. IMO, the opposite is true: at least by rights, the programmers should be in charge, and Khronos (and other standards setters) should serve them.
Jerry Coffin
Display lists are going the way of the dodo, you can safely skip them and not miss anything (They're not part of OpenGL ES, nor OpenGL 3.0). There's no reason to use anything but VBOs nowadays.
Andreas Brinck
Display lists haven't been a good way of drawing things in many, many years. Graphics drivers don't work like that any more and do not optimize display lists in the way that old-school gl coders think they do. Vertex arrays and VBOs are very similar to each other to work with and should be the preferred solution.
Alan
@Mads, @Andreas, @Alan -- you convinced me -- I removed Display Lists so that everyone may safely forget 'bout their existance ;)
Kornel Kisielewicz