Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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
glLoadIdentity();
glTranslatef(position.x, position.y, position.z);
glDrawArrays(...);

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

Without glLoadIdentity (pseudo-code)

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

// 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. –  Miro 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
add comment

1 Answer

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:

glLoadIdentity();
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
add comment

Your Answer

 
discard

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.