Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Should I call glLoadIdentity each time I render or cumulatively add translations? Does the latter option provide any performance advantage?

With glLoadIdentity (pseudo-code)

// in game loop
glTranslatef(position.x, position.y, position.z);

// in input handling code
if (KeyDown(LEFT))
  position.x = position.x - 0.01;

Without glLoadIdentity (pseudo-code)

// in game loop
glTranslatef(change_position.x, ...);

// in input handling code
if (KeyDown(LEFT))
  change_position.x = -0.01;
share|improve this question
That's old way of OpenGL rendering. Modelview and projection matrices have been removed since OpenGL 3. Now they are only emulated. So i would go with uniform vec3 for position. – kravemir Jul 12 '12 at 17:56
I know it's the old way, but the same principles apply if I were to use GLM. Are you saying you would go for the option with glLoadIdentity? If not, please leave an answer with code and clarify. I would happily accept it. – Oskar Jul 12 '12 at 18:11
up vote 4 down vote accepted

In the old days of OpenGL with the builtin matrix stacks, glLoadIdentity served as a simple way of setting the topmost matrix on the stack to the identity matrix.

If you were doing a transform like T1*R1*S1*T2*R2*S2, it has the benefit of letting you use the same cumulative form for each of the operations instead of having to overload the first one to be able to replace the existing state instead of multiply with it.

That is, you have:

glTranslatef(..); glRotatef(); glScalef();
glTranslatef(..); glRotatef(); glScalef();

instead of a hypothetical:

glClearAndTranslatef(..); glRotatef(); glScalef();
glTranslatef(..);         glRotatef(); glScalef();

You can compare this to addition or multiplication of scalars - you can always add in the identity element for the operation: 1 * a * b and 0 + c + d.

Modern matrix libraries tend to eschew the stack model in lieu of matrix objects. As such, there's no real need to have a focus on the current object, as all matrices are fairly equal in importance.

You then only really need an identity matrix for when you really need identity, not to clear out the state for a long multiplication.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.