title: How do glPushMatrix() and glPopMatrix() keep the scene the same?
body: <p>I found some code online which will move a box across the screen, then reset it after the box hits the end of the screen.<br>
  Here is the code:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>void display(void) {
    int sign = 1;
    if (lastFrameTime == 0) {
      /*
       * sets lastFrameTime to be the number of milliseconds since
       * Init() was called;
       */
      lastFrameTime = glutGet(GLUT_ELAPSED_TIME);
    }
  
    int now = glutGet(GLUT_ELAPSED_TIME);
    int elapsedMilliseconds = now - lastFrameTime;
    float elapsedTime = float(elapsedMilliseconds) / 1000.0f;
    lastFrameTime = now;
  
    int windowWidth = glutGet(GLUT_WINDOW_WIDTH);
  
    if (boxX &gt; windowWidth) {
      boxX -= windowWidth;
    }
    boxX += (sign)*256.0f * elapsedTime;
  
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
    glPushMatrix();
    //creates a new matrix at the top that we can do things to?
    glTranslatef(boxX, 0.0f, 0.0f);
  
    /*
     * draw a "quad" (rectangle)
     */
    glBegin(GL_QUADS);
    glVertex2f(0.0f, 0.0f);
    glVertex2f(128.0f, 0.0f);
    glVertex2f(128.0f, 128.0f);
    glVertex2f(0.0f, 128.0f);
    glEnd();
    glPopMatrix();
    //pops that matrix off the stack so we can have a "clean" version to do something next time.?
  
    glutSwapBuffers();
  }
  </code></pre>
  
  <p>Now, the way I understand <a href="http://www.opengl.org/sdk/docs/man2/xhtml/glPushMatrix.xml" rel="noreferrer"><code>glPushMatrix()</code></a> and <a href="http://www.opengl.org/sdk/docs/man2/xhtml/glPopMatrix.xml" rel="noreferrer"><code>glPopMatrix()</code></a> is that <code>glPushMatrix()</code> puts (or pushes) a new matrix on the stack for you to do things to, so that after you pop it back off you have a "clean" slate again. This is why, if I neglect the <code>glPopMatrix()</code> after <code>glEnd()</code>, my square seems to accelerate rather than move at a constant velocity.</p>
  
  <p>How is it, however, that the changes I make inside of <code>glPushMatrix()</code> and <code>glPopMatrix()</code> are kept? When I use <code>glPushMatrix()</code> and make a change to the top matrix, it visualizes the changes, but when i use <code>glPopMatrix()</code>, aren't all those changes gone? When I am restored to a "clean" slate again, how is it that my box moves across the screen? </p>
  
  <p>How is the state of that translation recorded if i just pop the matrix off again after making the change?</p>