在opengl中围绕固定点旋转对象

在opengl中围绕固定点旋转对象,opengl,rotation,Opengl,Rotation,我对这个openGL代码有一个问题: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPushMatrix(); // put current matrix on stack //glTranslatef(0.0f, 0.0f, 0.0f); //glTranslatef(-4*1.5, 0.0, 4*1.5); glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f); // rotate the robot

我对这个openGL代码有一个问题:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix(); // put current matrix on stack

//glTranslatef(0.0f, 0.0f, 0.0f);   
//glTranslatef(-4*1.5, 0.0, 4*1.5);

glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f); // rotate the robot on its y-axis
glTranslatef(xpos, ypos, zpos);
DrawRobot(xpos, ypos, zpos); // draw the robot
glPopMatrix();

我该怎么做才能使我的机器人绕过它当前所在的点,而不是原点?我认为问题在于这个片段。

只需在翻译后进行旋转即可。秩序很重要

glTranslatef(xpos, ypos, zpos);
glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f);

尝试在平移后旋转:

    glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix(); // put current matrix on stack

//glTranslatef(0.0f, 0.0f, 0.0f);   
//glTranslatef(-4*1.5, 0.0, 4*1.5);

glTranslatef(xpos, ypos, zpos);
glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f); // rotate the robot on its y-axis
DrawRobot(xpos, ypos, zpos); // draw the robot
glPopMatrix();

沿z轴围绕其中心旋转对象的示例:

glPushMatrix();

glTranslatef(250,250,0.0); // 3. Translate to the object's position.

glRotatef(angle,0.0,0.0,1.0); // 2. Rotate the object.

glTranslatef(-250,-250,0.0); // 1. Translate to the origin.

// Draw the object
glPopMatrix();
用这个

house();

glTranslatef(x, y, 0.0); // 3. Translate back to original
glRotatef(theta, 0.0, 0.0, 1.0); // 2. Rotate the object around angle
glTranslatef(-m, -n, 0.0); // 1. Move to origin

house();
其中m和n是要围绕其旋转的对象上的
x和y是你想要旋转的

都德,谢谢,但问题是机器人旋转的圆圈;我想减小它的半径顺便说一句,我正在90度旋转机器人,只等待你的回复asap@user2388112:我不太清楚你的意思,但我认为这应该有帮助:你可以围绕你想要的任何点旋转,先平移到该点,然后旋转,然后再平移回来(只取每个坐标的负数)。如果这不是你想要的,请注意,因为这些评论不是一个进行深入讨论的好地方;我想减小它的半径顺便说一句,我正在90度旋转机器人,只等待你的回答,因为这个问题很老了,已经得到了充分的回答。新答案并没有增加任何有用的东西。同样的几行代码被提交作为对其他旧问题的回答。这个回答增加了一些新的东西,即在旋转之后,物体需要被翻译回它原来的位置。其他答案中没有提到这一点,这对我很有效。没有Push和PopMatrix语句就无法工作。