开发者

Creating many polygons with OpenGL is slow?

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


  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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜