Performance Question about matrix transformations

Started by CodeBunny, April 15, 2012, 21:49:31

Previous topic - Next topic

CodeBunny

So, I have to apply a translate, rotate, and scale operation before rendering a 2D entity. After I've done that, I wish to restore the modelview matrix to its original state (not the Identity matrix). Right now I do the following:

glTranslatef(tX, tY, 0);
glRotatef(r, 0, 0, 1);
glScalef(sX, sY, 1f);

// render the object

glScalef(1 / sX, 1 / sY, 1f);
glRotatef(-r, 0, 0, 1);
glTranslatef(-tX, -tY, 0);


However, would it be faster or slower to do the following instead?
glPushMatrix();
glTranslatef(tX, tY, 0);
glRotatef(r, 0, 0, 1);
glScalef(sX, sY, 1f);

// render the object

glPopMatrix();

Orangy Tang

Both are going to be pretty darn inefficient, but the pop method may be fractionally quicker.

But you shouldn't be doing it like that because each sprite will have *tons* of overhead from all the draw calls. It'd be much better to push all your sprites into a few big arrays, then draw all of them at the same time (eg. glDrawArrays)

CodeBunny

I see.

Part of the reason I'm doing this is because it's simple to encapsulate the render calls and do whatever with them. I'll take a look into glDrawArrays, however.

matheus23

I think Translating should be a bit faster, because it just changed 3 values of the matrix without any sin or cos calculations. But acctually the very fastest would be to just never use matrices at all in 2D rendering, and just give them the positions in gl_Vertex calls. And maybe create a shader, where you aren't calling gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex, but gl_Position = gl_Vertex. But that acctually isn't a good idea, if you want to scale or rotate things. if you want to do that, you should just use your code as you have it.
My github account and currently active project: https://github.com/matheus23/UniverseEngine

CodeBunny

I understand that if I compiled geometry and used VBOs or VAs then this would be far faster.

However, I want a simple way of drawing things that don't require much overhead on the CPU.

matheus23

Idk, thats just a suggestion... but maybe compile everything in a display list? Also, matrix transformations shouldn't make a big change for normal cpus. The biggest performance hit for 2D games is usually transferring the texture data. So try to reduce glBind calls as much as you can. Also, avoid glEnable[/Disable](GL_TEXTURE_2D); as often as you can. Some people use that as "unbinding textures". Thats all I know... i think...
My github account and currently active project: https://github.com/matheus23/UniverseEngine

CodeBunny

It's the whole static vs. dynamic approach. Static is faster, but has more restrictions.