views:

253

answers:

2

When setting the ModelView matrix you normally go through several transformations from the identity matrix. for example:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(270.0f, 0.0f, 0.0f, 1.0f);
glTranslatef(-rect.size.height / 2, -rect.size.width / 2, 0.0f);

Instead of doing those operations one after the other (assume there are more than two), wouldn't it be more efficient to simply pre-calculate the resulting matrix and set the ModelView matrix to this manual matrix?

+1  A: 

Sure, as long as the transformation isn't going to change. If, for example, the user can move and rotate your objects with the mouse then you are going to have to keep recalculating your matrix. In that case you may as well let OpenGL do it for you.

Also, unless you are having performance problems I would tend to not worry about efficiency and just stick with whichever way is going to keep your code simpler, which is probably to specify individual transformations.

Incredulous Monk
A: 

Although I'm not entirely certain, Jeff Lamarche's excellent "OpenGL from the Ground Up" tutorials seem to indicate that the matrix multiplications occur on the CPU anyways. See tutorial number 7.

If you're still trying to get things going as fast as possible, his article has some code which uses the iPhone's vector processors (think SSE1/2/3/4 on a desktop) to speed up the code by 7 times (from 0.7% of runtime to 0.1% on Shark).

However, the performance increase may not be worth it, especially in terms of code readability. If you want your code to go as fast as possible, do it, but if not, consider keeping the library transformations so that others can figure out what's going on in your code. Everybody who does OpenGL on iPhone is going to recognize glTranslatef(x,y,z), but maybe not mTranslate(worldview, makeMat(...))

Lion Kabob