【问题标题】:OpenGL GLUT left mouse click overrides holding right mouseOpenGL GLUT 鼠标左键单击覆盖按住鼠标右键
【发布时间】:2019-05-11 19:22:27
【问题描述】:

我正在修改一些骨架代码来了解 OpenGL 的工作原理,并在 SphericalCameraManipulator.cpp 中提供了它允许我在按住鼠标右键的同时平移和倾斜相机:

void SphericalCameraManipulator::handleMouseMotion(int x, int y)
{
    //Motion
    float xMotion = (float)x - previousMousePosition[0];
    float yMotion = (float)y - previousMousePosition[1];

    if(reset)
    {
        xMotion = yMotion = 0.0;
        reset = false;
    }

    this->previousMousePosition[0] = (float)x;
    this->previousMousePosition[1] = (float)y;

    //Right Button Action
    if(this->currentButton == GLUT_RIGHT_BUTTON && this->currentState == GLUT_DOWN)
    {
        this->pan  -= xMotion*this->panTiltMouseStep ;
        this->tilt += yMotion*this->panTiltMouseStep ;
    }

    //Enforce Ranges
    this->enforceRanges();
} 

我删除了鼠标左键,因为我不想让它控制相机,现在它在主代码体中执行一个命令。

//Left Button Action
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        ...   //game action
    }

我的问题是,当我单击鼠标左键时,取消了鼠标右键的按下,我必须释放并重新按下鼠标才能继续控制相机。

有没有办法防止这种情况发生?它打断了游戏的流程。我正在使用 GLUT

【问题讨论】:

  • 缺少minimal reproducible examplebuttoncurrentButton 从哪里获取它们的值?
  • "当我单击鼠标左键时,鼠标右键的按下被取消"鼠标按下是单个事件。按下按钮时,您可能设置了一个状态变量。当按下鼠标左键时,这种状态会发生什么是代码中的错误。
  • 我刚刚在问题中澄清了,鼠标右键是用来移动相机的。我认为这是我正在更改的原始代码中的意图,但我不熟悉按钮按下的工作原理。如果有人可以告诉我可以纠正,我会尝试更多,但不想在不可能的事情上花费更多时间。
  • @William 没有鼠标“按住”事件。有一个鼠标按下和一个鼠标释放事件。您的程序逻辑中存在错误。

标签: c++ opengl mouseevent glut


【解决方案1】:

Rabbid76 的评论说没有鼠标按住事件让我走上了正确的道路。

我写了一个小函数,简单地记录鼠标按钮 2 的最后状态(“相机环视”按钮):

void SphericalCameraManipulator::handleMouse(int button, int state)
{
    this->currentButton = button;
    this->currentState  = state;

    //to enable mouse look around
    if (this->currentButton == 2) //right mouse
        this->lastLookState = this->currentState;

    if(this->currentState == GLUT_UP)
    {
        reset = true;
    }
}

这意味着即使当前状态是关于鼠标左键(游戏动作),仍然可以环顾四周,因为没有发生“鼠标两键向上”事件:

this->currentButton = 2;
this->currentState  = 1;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    相关资源
    最近更新 更多