glPushMatrix()和glPopMatrix()如何保持场景相同? [英] How do glPushMatrix() and glPopMatrix() keep the scene the same?
问题描述
我在网上找到了一些代码,它会在屏幕上移动一个框,然后在框打到屏幕末尾后重置。
这是代码:
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
恢复。不是整个framebuffer。 framebuffer包含四边形的渲染,以后改变矩阵不会影响已经渲染的任何东西。
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()如何保持场景相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!