Is GlBindTexture () + glBegin (foo) slow?

I am creating a small game for MacOS using Cocoa + OpenGL to create a GUI. The game is a BoulderDash-Clone, so basically it is a 2D array of objects, not THAT many (the level is approximately equal to 40 objects and 25 objects). Many objects are animated, so I have to dynamically extract the textures when they are drawn (I use NSTimer to constantly redraw the scene for animation). This seems to cause serious performance issues.

I first did

for(y1, ..., yn) for(x1, ..., xn) { glBindTexture(foo); glBegin(GL_QUAD); [drawing the quad with texture] glEnd(); } 

which worked but was very slow (Activity Monitor showed 20% CPU usage). Since I did not create any textures, but I use a placeholder that allowed me to test

 glBindTexture(foo); for(y1, ..., yn) for(x1, ..., xn) { glBegin(GL_QUAD); [drawing of quad with texture] glEnd(); } 

which was much faster (2% CPU usage). So I thought it was glBindTexture () that caused the significant slowdown. Then I tried to figure out how slow glBindTexture () is and did

 for(y1, ..., yn) for(x1, ..., xn) { glBindTexture(foo); // no drawing this time } 

which was also very fast (2% CPU usage). Why is this?

Ultimately, I will have to put glBindTexture () and the drawing in the same loop, since I need to snap the texture according to the object and its animation. Therefore, I need to find out what causes performance problems in the first code example and how I can speed up the process. I always thought that several hundred objects with different textures would not be slow with OpenGL. Oh, and I already build this game with Java + JOGL once, and if I call it right, I did the same, and it was much faster. Should Objective-C / C ++ Destroy Java Performance?

+4
source share
1 answer

The OpenGL specification gives implementations quite a bit of freedom when it comes to processing commands. Your call to glBindTexture() can simply set the int to the command queue, which is not actually processed until you replace the buffers or call glFinish() / glFlush() . It can even be (mostly) ignored if you call it, and then don't give OpenGL any geometry to use it with.

Use texture atlases to shorten glBindTexture() calls.

glBegin() , and friends is the slowest way to represent OpenGL geometry. Try to maximize your geometry and use vertex / VBOs arrays to reduce function overhead.

+4
source

Source: https://habr.com/ru/post/1337147/


All Articles