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();
}
```

- 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.

- 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,

Jorge Maldonado

Respectfully,

Jorge Maldonado

Think of the current matrix as a global variable (which it is). `glPushMatrix`

saves whatever is in this variable at the time.

That is literally all I can tell you about the matrix that gets saved in the first glPushMatrix call. It is whatever has been done to the current matrix before your function was called. I don’t have all of your code, so I can’t say what other code might be doing matrix operations.

All OpenGL state is initialized to a default value. The current matrix is part of OpenGL’s state, so it has a default value that it gets initialized to when OpenGL is first created. The default state of all matrices is the identity matrix.

So, assuming that no other code in your application modifies the current matrix, then the current matrix will be that default value: the identity matrix. Therefore, given that assumption, the first glPushMatrix pushes the identity matrix to the stack.

This also means that when the last glPopMatrix is called, the current matrix again becomes the identity matrix (since that was what was saved first).