1

当我按照以下方式进行操作时,指针的坐标会发生变化。现在如何重置我的坐标系以便0,0,0在绘制另一个对象之前?

glPushMatrix();
glTranslatef(0.0f, -500.0f, 1200.0f);
glRotatef(270.0f, 1.0f, 0.0f, 0.0f);

glPushMatrix();
glColor3f(0.0f, 1.0f, 1.0f);
gluCylinder(quadric,10.0f,10.0f,1000.0f,32,32);
glPopMatrix();

我将绘制另一个对象,但当前的原点系统不是0,0,0. 我认为重点在于已经画出的圆柱体。

而且,如果我在第一个对象之后绘制另一个对象,它也会被旋转。为什么?

4

2 回答 2

1
glPushMatrix () ;
glTranslatef(0.0f, -500.0f, 1200.0f ) ;
glRotatef ( 270.0f, 1.0f, 0.0f, 0.0f );
glColor3f ( 0.0f, 1.0f, 1.0f );
gluCylinder(quadric,10.0f,10.0f,1000.0f,32,32);
glPopMatrix();

glPushMatrix () ;
glTranslatef(0.0f, -500.0f, 1200.0f ) ;
glRotatef ( 270.0f, 0.0f, 1.0f, 0.0f );
glColor3f ( 0.0f, 1.0f, 1.0f );
gluCylinder(quadric,10.0f,10.0f,1000.0f,32,32);
glPopMatrix();

(This code draws 2 independent objects: one with rotated x-axis, the other one with rotated y-axis)

Every glPushMatrix() call needs an correspondending glPopMatrix() call. If you want to make 'local' transformations for one object (i.e. translate, rotate) you can simply call glPushMatrix(), do your transformation, draw your object and call glPopMatrix(). Then your coordinate system is finally untransformed again, and you can draw your next object.

If you don't get that, you could also use glLoadIdentity() on your Modelview Matrix after you applied some transformations (to reset all transformations again) - but if you're using transformation related code (i.e. gluLookAt) on your ModelviewMatrix you have to do that again, after every glLoadIdentity() call.

glLoadIdentity();
glTranslatef(0.0f, -500.0f, 1200.0f ) ;
glRotatef ( 270.0f, 1.0f, 0.0f, 0.0f );
glColor3f ( 0.0f, 1.0f, 1.0f );
gluCylinder(quadric,10.0f,10.0f,1000.0f,32,32);

glLoadIdentity();
glTranslatef(0.0f, -500.0f, 1200.0f ) ;
glRotatef ( 270.0f, 0.0f, 1.0f, 0.0f );
glColor3f ( 0.0f, 1.0f, 1.0f );
gluCylinder(quadric,10.0f,10.0f,1000.0f,32,32);
于 2012-11-23T14:06:29.947 回答
1

glPushMatrix()将矩阵推送到当前活动的矩阵堆栈(在您的情况下可能是模型视图)。glTranslate()glRotate()与顶部模型视图矩阵一起使用,渲染也是如此。glPopMatrix()从堆栈中删除顶部矩阵。

因此,一旦您平衡glPushMatrix()glPopMatrix()调用,堆栈将处于与您开始时相同的状态。

于 2012-11-23T12:42:07.790 回答