# Identity matrix, What does it really do?

I understand that multiplying by the identity matrix is like multiplying by 1. Why would you multiply a matrix that will only contain the same result?

Also, I'm experimenting with some OpenGL code and found some very interesting things. Like for example:

When you use setIdentity(), the image rotates slower

Matrix.setIdentityM(mProjectionMatrix, 0);
Matrix.rotateM(mProjectionMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);


But if you try to remove the setIdentity() the image will rotate so fast.. 1st it will rotate clockwise so fast then becoming slower until it stop then rotate counter clockwise then will stop again then rotate clockwise.

I really don't understand how identity matrix affects them despite multiplying by 1.

Can someone explain to me whats going on?

Here is the tutorial i am following www.learnopengles.com/android-lesson-one-getting-started/

TL; DR: If you multiply stuff together, you need to start with a 1

Forget about matrices for a second, let's talk about numbers. Suppose to rotate by 90, you multiply by 90. So

P' = 90*P

Now you do other transforms - a rotation R, a translation T, a scale S and so on. So

P' = T*R*S*P

Since you will apply all these transforms to a lot of points, you want to precompute them into a single transform so you can apply it many times.

M = T*R*S

P1' = P1*M

P2' = P2*M

and so on.

Now OpenGL has the concept of the "current transform" M. The way you construct M is by multiplying stuff into it, something like this

M = S

M = R*M

M = T*M

So there are two operations you can do on the current transform: set it to something (M = S) or multiply it by something (M = R*M). So you'd need several sets of functions, like setScale and multiplyByScale, setRotation and multiplyByRotation, and so on.

OR you can have a single setIdentity that sets M = 1, and then you only need the functions that multiply the other transforms over it:

M = 1

M = S*M

M = R*M

M = T*M

So let's go back to your example.

Matrix.setIdentityM(mProjectionMatrix, 0);


This sets the current transform to identity (1)...

Matrix.rotateM(mProjectionMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);


...and this multiplies the current transform by a rotation matrix.

Why do things rotate "faster" if you don't reset the matrix to identity? Because you're adding a rotation on top of the rotation you already had!

First frame:

M = 1

M *= 10 <--- M is a 10 degree rotation

Second frame:

M *= 10 <--- M is now a 20 degree rotation, because you started from 10, not from 1!

Identity matrix does nothing. It looks like this:

1,0,0,0
0,1,0,0
0,0,1,0
0,0,0,1


And that just multiplies everything by 1 if applied to other matrices or vectors.