只要按住鼠标右键,我就想放大一个物体。现在的问题是我每次想要缩放都要点击它。有没有办法可以修改我的代码,以便在按住按钮时进行缩放,而不是单击它?
void mouse(int button, int state, int x, int y)
{
// Save the left button state
if (button == GLUT_LEFT_BUTTON)
{
leftMouseButtonDown = (state == GLUT_DOWN);
zMovement += 0.1f;
}
else if (button == GLUT_RIGHT_BUTTON)
{
leftMouseButtonDown = (state == GLUT_DOWN);
zMovement -= 0.1f;
}
// Save the mouse position
mouseXPos = x;
mouseYPos = y;
}
答案 0 :(得分:2)
函数的状态变量告诉你发生了什么类型的鼠标按钮事件:它可以是GLUT_DOWN或GLUT_UP。
了解这一点,只要状态设置为true,就可以将此状态存储在鼠标函数之外的额外变量中并进行缩放(这必须在每个帧的某处完成)。代码可能如下所示:
void mouse(int button, int state, int x, int y)
{
// Save the left button state
if (button == GLUT_LEFT_BUTTON)
{
leftMouseButtonDown = (state == GLUT_DOWN);
}
else if (button == GLUT_RIGHT_BUTTON)
{
// \/ right MouseButton
rightMouseButtonDown = (state == GLUT_DOWN);
}
// Save the mouse position
mouseXPos = x;
mouseYPos = y;
}
void callThisInEveryFrame()
{
if (leftMouseButtonDown)
zMovement += 0.1f;
if (rightMouseButtonDown)
zMovement -= 0.1f;
}