# Help to understand push and pop operations of matrices

I have been trying to understand how glPushMatrix() and glPopMatrix() work but I cannot get a clear idea. I know that these operations have to do with a stack so a push implies to add to the top of the stack and a pop takes from the top.

I will try to describe the code below:
1) The first push inserts a matrix to the stack and makes it the current matrix so the next 3 instructions are performed on such a matrix. On what are these instructions applied? There are no objects yet.
2) The second push inserts another matrix to the stack makes it the current matrix so the next 2 instructions are performed on such a matrix. These instructions are applied on the cube #1.
3) Here comes the first pop which takes matrix in step (2) out of the stack. Where are the next 3 instruction applied to? Are they applied to matrix from step (1)?
4) The third push inserts a matrix to the stack and makes it the current matrix so the next 2 instructions are performed on such a matrix. These instructions are applied on the cube #2. At this point there are only 2 matrices in the stack.
5) Here come 2 pops which take the 2 left matrices in the stack out.

I will very much appreciate your comments so I can effectively understand this concept.

``````
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();
glTranslatef(-1.0, 0.0, 0.0);
glRotatef((GLfloat)shoulder, 0.0, 0.0, 1.0);
glTranslatef(1.0, 0.0, 0.0);

glPushMatrix();
glScalef(2.0, 0.4, 1.0);
glutWireCube(1.0);

glPopMatrix();
glTranslatef(1.0, 0.0, 0.0);
glRotatef((GLfloat)elbow, 0.0, 0.0, 1.0);
glTranslatef(1.0, 0.0, 0.0);

glPushMatrix();
glScalef(2.0, 0.4, 1.0);
glutWireCube(1.0);

glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}

``````
1. The first push inserts a matrix to the stack and makes it the current matrix so the next 3 instructions are performed on such a matrix. On what are these instructions applied? There are no objects yet.

Your question betrays a misunderstanding of how OpenGL works.

Transformation instructions are not applied to “objects”. All they do is generate matrices, then multiply those matrices with the current matrix.

So `glTranslatef` creates a translation matrix and multiplies it into the current translation matrix. The result becomes the new current matrix. It’s equivalent to this:

``````
current_matrix = current_matrix * translation_matrix(...);

``````

Modifying the current matrix alone does nothing except modify the current matrix.

Rendering commands use whatever matrix happens to be current to transform those rendered objects.

1. Here comes the first pop which takes matrix in step (2) out of the stack. Where are the next 3 instruction applied to? Are they applied to matrix from step (1)?

Push operations take the current matrix and saves it to the top of the stack. So whatever matrix was current when `glPushMatrix` was called is stored in the stack.

Pop operations take the matrix on top of the stack and make that matrix become the new current matrix (and then remove it from the top of the stack). So after `glPopMatrix`, the current matrix is whatever it was when the corresponding `glPushMatrix` was called.

I can definitely see that I am completely lost with these stack operations.
Please, I kindly ask for patience and for an explanation about what is going on in the sample code. I really need to understand this concept, it is very important so I can start working in an assignment. I have already spent time reading from several sources but I still do not get the idea.

What is the current matrix in the first glPushMatrix()? What is saved in the stack?

Respectfully,
Think of the current matrix as a global variable (which it is). `glPushMatrix` saves whatever is in this variable at the time.