在OpenGL中旋转对象

时间:2013-05-05 20:18:49

标签: opengl rotation glrotate

我想在OpenGL中移动一个objecto。我在(0,0,0)中绘制对象,如果对象在该点上它旋转正常(使用对象的中心作为参考旋转)。当我移动对象时,问题出现了,它使用与参考相同的点而不是它所在的点旋转。代码是:

glPushMatrix();
glRotatef(xangle,1.0f,0.0f,0.0f);
    glRotatef(yangle,0.0f,1.0f,0.0f);
    glRotatef(zangle,0.0f,0.0f,1.0f);
    glTranslatef(x,y,z);
//draw object
    glPopMatrix();

有没有人知道它必须做什么,所以对象总是使用它作为参考的点旋转?

1 个答案:

答案 0 :(得分:1)

只需重新排序您的转换:

glPushMatrix();
glTranslatef(x,y,z);
glRotatef(xangle,1.0f,0.0f,0.0f);
glRotatef(yangle,0.0f,1.0f,0.0f);
glRotatef(zangle,0.0f,0.0f,1.0f);
//draw object
glPopMatrix();