OpenGl -对象在旋转180度后反转

OpenGl - Object inverts after 180 degrees of rotation

本文关键字:180度 旋转 对象 OpenGl      更新时间:2023-10-16

我有一个对象渲染,我试图用鼠标旋转对象。对象将旋转180度,但在此之后,对象将反转(如果面向摄像机,则切换到远离摄像机),就像预期的鼠标移动一样,即如果向右拖动鼠标使对象顺时针旋转,那么它现在将逆时针旋转。一旦它达到下一个180度,它再次反转,恢复正常。我相信一定有什么简单的东西是我没有看到的?

下面是我的代码:
// Detect mouse state
void
mouse(int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        moving = 1;
        beginX = x;
        beginY = y;
    }
    if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
    {
        moving = 0;
    }
}
// Detect mouse movement
void motion(int x, int y)
{
    if (moving)
    {
        angleX = (angleX + (x - beginX));
        angleY = (angleY + (y - beginY));
        beginX = x;
        beginY = y;
        newModel = 1;
        glutPostRedisplay();
    }
}
// Rotate object
void recalcModelView(void)
{
    // Get object's centre
    int hh = head->GetHeight() / 2;
    int hw = head->GetWidth() / 2;
    int hd = head->GetDepth() / 2;
    glPopMatrix();
    glPushMatrix();
    // Rotate object based on mouse movement
    glTranslatef(hw, hd, hh);
    float temp1 = angleX / 5;
    float temp2 = angleY / 5;
    printf("TEMP1: %gn", temp1);
    printf("TEMP2: %gn", temp2);
    glRotatef(temp1, 0.0, 1.0, 0.0);
    glRotatef(-temp2, 1.0, 0.0, 0.0);
    glTranslatef(-hw, -hd, -hh);
    newModel = 0;
}

使用archball之类的东西来修复这个问题