Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating many polygons with OpenGL is slow?

Tags:

c++

c

opengl

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

like image 610
jmasterx Avatar asked May 23 '10 15:05

jmasterx


1 Answers

  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!

like image 196
Kornel Kisielewicz Avatar answered Nov 14 '22 06:11

Kornel Kisielewicz