glBindTexture() + glBegin(foo) slow?
I am building a small game for MacOS using Cocoa + OpenGL to create the GUI. The game is a BoulderDash-Clone, so its basic开发者_运维问答ally a 2D-array of objects, and not THAT many (a level is like 40 objects wide and 25 objects high). A lot objects are animated, so i've got to dynamically fetch textures when drawing them (i'm using 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 really slow (Activity Monitor showed 20% CPU usage). Since i did not create any textures yet i am using a placeholder which allowed me to test
glBindTexture(foo);
for(y1, ..., yn) for(x1, ..., xn)
{
glBegin(GL_QUAD);
[drawing of quad with texture]
glEnd();
}
which was a lot faster (2% CPU usage). So i thought it was glBindTexture() which caused the massive slowdown. I then tried to find out how slow glBindTexture() really 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 into the same loop, since i've got to bind a texture according to an object and its animation. So i need to find out what's causing the performance problems in the first code-example and how i could speed things up. I always thought some hundred objects with different textures wouldn't be that slow with OpenGL. Oh, and i already build this game with Java + JOGL once, and if i recall correctly i did the exact same thing and it was a lot faster. Shouldn't Objective-C / C++ destroy Java performance-wise?
The OpenGL spec gives implementations quite a bit of leeway when it comes to command processing. Your glBindTexture()
call may just set a int in the command queue, which doesn't actually get processed until you swap buffers or call glFinish()
/glFlush()
. It may even be (mostly) ignored if you call it and then don't give OpenGL any geometry to use it with.
Use texture atlases to cut down on glBindTexture()
calls.
glBegin()
and friends are just about the slowest way to submit geometry to OpenGL. Try to batch up your geometry as much as possible and use vertex arrays/VBOs to cut down on function-call overhead.
精彩评论