LWJGL Forum

Programming => OpenGL => Topic started by: CodeBunny on April 15, 2012, 21:49:31

Title: Performance Question about matrix transformations
Post by: CodeBunny on April 15, 2012, 21:49:31
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();
Title: Re: Performance Question about matrix transformations
Post by: Orangy Tang on April 15, 2012, 21:54:31
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)
Title: Re: Performance Question about matrix transformations
Post by: CodeBunny on April 15, 2012, 22:06:17
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.
Title: Re: Performance Question about matrix transformations
Post by: matheus23 on April 16, 2012, 12:54:11
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.
Title: Re: Performance Question about matrix transformations
Post by: CodeBunny on April 16, 2012, 18:50:14
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.
Title: Re: Performance Question about matrix transformations
Post by: matheus23 on April 17, 2012, 17:12:15
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...
Title: Re: Performance Question about matrix transformations
Post by: CodeBunny on April 17, 2012, 18:38:43
It's the whole static vs. dynamic approach. Static is faster, but has more restrictions.