glPushMatrix() 和 glPopMatrix() 如何保持场景相同?

How do glPushMatrix() and glPopMatrix() keep the scene the same?(glPushMatrix() 和 glPopMatrix() 如何保持场景相同?)
本文介绍了glPushMatrix() 和 glPopMatrix() 如何保持场景相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在网上找到了一些代码,可以在屏幕上移动一个框,然后在框到达屏幕末端后重置它.
代码如下:

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.
Here is the 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 > 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();
}

现在,我理解的方式glPushMatrix()glPopMatrix()glPushMatrix() 在堆栈上放置(或推送)一个新矩阵供您执行操作,以便在您将其弹出后再次拥有干净"的石板.这就是为什么,如果我忽略 glEnd() 之后的 glPopMatrix(),我的方块似乎在加速而不是匀速移动.

Now, the way I understand glPushMatrix() and glPopMatrix() is that glPushMatrix() 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 glPopMatrix() after glEnd(), my square seems to accelerate rather than move at a constant velocity.

但是,我在 glPushMatrix()glPopMatrix() 中所做的更改是如何保留的?当我使用 glPushMatrix() 并对顶部矩阵进行更改时,它会显示更改,但是当我使用 glPopMatrix() 时,所有这些更改不是都消失了吗?当我再次恢复到干净"的状态时,我的盒子如何在屏幕上移动?

How is it, however, that the changes I make inside of glPushMatrix() and glPopMatrix() are kept? When I use glPushMatrix() and make a change to the top matrix, it visualizes the changes, but when i use glPopMatrix(), 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?

如果我在进行更改后再次弹出矩阵,如何记录该翻译的状态?

How is the state of that translation recorded if i just pop the matrix off again after making the change?

推荐答案

只是用于变换坐标的矩阵,由glPopMatrix恢复.不是整个帧缓冲区.帧缓冲区包含四边形的渲染,之后更改矩阵不会影响已渲染的任何内容.

It's only the matrix, used to transform coordinates, that is restored by glPopMatrix. Not the whole framebuffer. The framebuffer contains the rendering of the quadrilateral, changing the matrix afterward doesn't affect anything already rendered.

这篇关于glPushMatrix() 和 glPopMatrix() 如何保持场景相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Prevent class inheritance in C++(防止 C++ 中的类继承)
Why should I declare a virtual destructor for an abstract class in C++?(为什么要在 C++ 中为抽象类声明虚拟析构函数?)
Why is Default constructor called in virtual inheritance?(为什么在虚拟继承中调用默认构造函数?)
C++ cast to derived class(C++ 转换为派生类)
C++ virtual function return type(C++虚函数返回类型)
Is there any real risk to deriving from the C++ STL containers?(从 C++ STL 容器派生是否有任何真正的风险?)