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

use*_*112 11 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 on its y-axis
glTranslatef(xpos, ypos, zpos);
DrawRobot(xpos, ypos, zpos); // draw the robot
glPopMatrix();
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能让我的机器人转过它当前所处的位置而不是原点?我认为问题出在这个片段上.

小智 13

沿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();
Run Code Online (Sandbox Code Playgroud)

  • 这个答案增加了一些新东西,即在旋转之后,需要将对象转换回原始位置.其他答案中没有提到这一点. (11认同)
  • 这个问题很古老,已经得到了充分的回答.新答案没有添加任何有用的东西.并提交了相同的几行代码作为其他旧问题的答案. (2认同)

ham*_*mar 11

只需在翻译后进行旋转.订单很重要.

glTranslatef(xpos, ypos, zpos);
glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f);
Run Code Online (Sandbox Code Playgroud)