quantum231
quantum231

Reputation: 2593

glLoadIdentity and glPushMatrix/glPopMatrix, why not just use the former?

When applying transformations to objects we use glPushMatrix/glPopMatrix. But why don't we use glLoadIdentity only instead?

Thus

    glPushMatrix()
    ..apply tranformations
    ...draw object
    glPopMatrix()
    glPushMatrix()
    ..apply tranformations
    ...draw object
    glPopMatrix()

This is how its supposed to be done right?

can become

    glLoadIdentity()
    ..apply tranformations
    ...draw object
    glLoadIdentity()
    ..apply tranformations
    ...draw object

Upvotes: 5

Views: 1956

Answers (2)

Fox1942
Fox1942

Reputation: 366

I found a good explanation on: https://www.allegro.cc/forums/thread/595000

They don't have the same effect.

glLoadIdentity replaces the current matrix with the identity matrix.

glPushMatrix pushes the current matrix onto a stack. You can then pull the top matrix off the stack and make it the current one later using glPopMatrix.

Those are only the same if:

  • your camera is always fixed at (0, 0, 0) looking along z

  • all of your models are single, rigid shapes

  • you loaded your matrix stack with the identity in the first place

Or if you're doing lots of unnecessary arithmetic yourself on the CPU, making your code more complicated and your application less speedy.

Upvotes: 0

Nicol Bolas
Nicol Bolas

Reputation: 474316

Because you wouldn't be able to do this:

glPushMatrix()
..apply tranformations
glPushMatrix()
..apply ADDITIONAL transformations
..draw object with combined transforms
glPopMatrix()
...draw object without the additional transforms.
glPopMatrix()

Upvotes: 8

Related Questions