What does glLoadIdentity() do in OpenGL?

OpenglGraphics

Opengl Problem Overview


I'm new to OpenGL and I'm a little overwhelmed with all of the random functions that I have in my code. They work and I know when to use them, but I don't know why I need them or what they actually do.

I know that glLoadIdentity() replaces the current matrix with the identity matrix, but what exactly does that do? If every program requires it, why isn't the identity matrix by default unless otherwise specified? I don't like to have functions in my code unless I know what they do. I should note that I am using OpenGL exclusively for rich 2D clients so excuse my ignorance if this is something very obvious for 3D.

Also a little confused about glMatrixMode(GL_PROJECTION) VS glMatrixMode(GL_MODELVIEW).

Opengl Solutions


Solution 1 - Opengl

The identity matrix, in terms of the projection and modelview matrices, essentially resets the matrix back to its default state.

As you hopefully know, glTranslate and glRotate are always relative to the matrix's current state. So for instance, if you call glTranslate, you are translating from the matrix's current 'position', not from the origin. But if you want to start over at the origin, that's when you call glLoadIdentity(), and then you can glTranslate from the matrix which is now located at the origin, or glRotate from the matrix which is now oriented in the default direction.

I think Boon's answer, that it is the equivalent of 1, is not exactly correct. The matrix actually looks like this:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

That is the identity matrix. Boon is correct, mathematically, that any matrix multiplied with that matrix (or a matrix that looks like that; diagonal ones, all else 0s) will result in the original matrix, but I don't believe he explained why this is important.

The reason why this is important is because OpenGL multiplies all positions and rotations through each matrix; so when for instance you draw a polygon (glBegin(GL_FACE), some points, glEnd()), it translates it to "world space" by multiplying it with the MODELVIEW, and then translates it from 3D to 2D by multiplying it with the PROJECT matrix, and that gives it the 2D points on screen, along with the depth (from the screen 'camera'), which it uses to draw pixels. But when one of these matrices are the identity matrix, the points are multiplied with the identity matrix and therefore are not changed, so the matrix has no effect; it does not translate the points, it does not rotate them, it leaves them as-is.

I hope this clarifies a bit more!

Solution 2 - Opengl

Identity matrix is the equivalent of 1 for number. As you know any number that multiplies with 1 is itself (e.g. A x 1 = A),

The same thing goes for matrix ( MatrixA x IdentityMatrix = MatrixA).

So loading an identity matrix is a way to initialize your matrix to the right state before you multiply further matrices into the matrix stack.

glMatrixMode(GL_PROJECTION) : deals with the matrices used by perspective transformation or orthogonal transformation.

glMatrixMode(GL_MODELVIEW) : deals with matrices used by model-view transformation. That is, to transform your object (aka model) to the view coordinate space (or camera space).

Solution 3 - Opengl

The projection matrix is used to create your viewing volume. Imagine a scene in the real world. You don't really see everything around you, only what your eyes allow you to see. If you're a fish for example you see things a bit broader. So when we say that we set up the projection matrix we mean that we set up what we want to see from the scene that we create. I mean you can draw objects anywhere in your world. If they are not inside the view volume you won't see anything. When you create the view volume imagine that you create 6 clipping planes that define your field of view.

As for the modelview matrix, it is used to make various transformations to the models (objects) in your world. Like this you only have to define your object once and then translate it or rotate it or scale it.

You would use the projection matrix before drawing the objects in your scene to set the view volume. Then you draw your object and change the modelview matrix accordingly. Of course you can change your matrix midway of drawing your models if for example you want to draw a scene and then draw some text (which with some methods you can work easier in orthographic projection) then change back to modelview matrix.

As for the name modelview it has to do with the duality of modeling and viewing transformations. If you draw the camera 5 units back, or move the object 5 units forwards it is essentially the same.

Hope I've shed some light

Solution 4 - Opengl

The identity matrix is used to "initialize" a matrix to a sane default.

One important thing to realize is that matrix multiplications are in a sense, additive. For example, if you take a matrix that starts with the identity matrix, multiply it times a rotation matrix, then multiply it times a scaling matrix, you end up with a matrix that rotates and scales the the matrices it is multiplied against.

Solution 5 - Opengl

Just to recap what others have said, the identity matrix is a matrix that is such that when you multiply a vector/matrix with it, the result is that same vector/matrix. It's the equivalent of the number 1 with multiplication or the number 0 with addition.

glLoadIdentity() is a deprecated function, and you are encouraged to manage your own matrices.

Solution 6 - Opengl

glLoadIdentity() function ensures that each time when we enter the projection mode, the matrix will be reset to identity matrix, so that the new viewing parameters are not combined with the previous one.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexanderView Question on Stackoverflow
Solution 1 - OpenglRicketView Answer on Stackoverflow
Solution 2 - OpenglBoonView Answer on Stackoverflow
Solution 3 - OpenglblueView Answer on Stackoverflow
Solution 4 - OpengldicroceView Answer on Stackoverflow
Solution 5 - OpenglThomas KView Answer on Stackoverflow
Solution 6 - OpenglSrinath k sView Answer on Stackoverflow