【问题标题】:Rotating an object around a fixed point in opengl在opengl中围绕固定点旋转对象
【发布时间】:2013-05-10 18:59:06
【问题描述】:

我对这个 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();

我应该怎么做才能让我的机器人绕着它当前所在的点而不是绕着原点转?我认为问题在于这个sn-p。

【问题讨论】:

    标签: opengl rotation


    【解决方案1】:

    沿 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();
    

    【讨论】:

    • 这个问题很老了,已经得到了充分的回答。新答案不会添加任何有用的东西。并且提交了相同的几行代码作为对其他旧问题的回答。
    • 这个答案增加了一些新的东西,即在旋转之后,需要将对象平移回其原始位置。其他答案中没有提到这一点。
    • 这对我有用。没有 Push 和 PopMatrix 语句就无法工作。
    【解决方案2】:

    只需在平移后进行旋转即可。顺序很重要。

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

    【讨论】:

    • 伙计,它工作了谢谢,但问题是机器人围绕的圆圈旋转;我想减小它的半径顺便说一句我只将机器人旋转 90 度等待你的回复尽快谢谢
    • @user2388112:我不太确定您的意思,但我认为这应该会有所帮助:您可以先平移到该点,然后旋转,然后再平移回您想要的任何点(只需取每个坐标的负数)。如果这不是您想要的,请ask a new question about it,因为这些 cmets 不是进行扩展讨论的好地方。
    【解决方案3】:

    翻译后尝试旋转:

        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();
    

    【讨论】:

    • 伙计,它工作了谢谢,但问题是机器人绕着旋转的圆圈;我想减少它的半径顺便说一句我只将机器人旋转 90 度等待你的回复尽快谢谢
    【解决方案4】:

    使用这个

    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您要围绕其旋转的点

    【讨论】:

      猜你喜欢
      • 2014-11-15
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多