OpenGl - 在180度旋转后反转

时间:2013-03-28 19:22:45

标签: c++ opengl rotation

我有一个渲染的对象,我正在尝试用鼠标旋转对象。物体将精确旋转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: %g\n", temp1);
    printf("TEMP2: %g\n", temp2);
    glRotatef(temp1, 0.0, 1.0, 0.0);
    glRotatef(-temp2, 1.0, 0.0, 0.0);
    glTranslatef(-hw, -hd, -hh);
    newModel = 0;
}

1 个答案:

答案 0 :(得分:0)

使用类似Arcball的内容来解决此问题。

相关问题