我渲染了一个对象,我正在尝试用鼠标旋转对象。对象将正常旋转 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;
}