JORGEMAL

10-15-2015, 08:50 AM

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

}

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

}