glutPassiveMotionFunc问题

时间:2017-04-13 12:05:48

标签: c++ opengl glut freeglut

我对glut和opengl有点新意,我试图在鼠标移动时使相机移动但是当我试图在屏幕上获得鼠标位置时,我假设你想要传递x和y的方法应该被引用在glutPassiveMotionFunc()参数中。但是在尝试为CameraMove方法提供函数时,我遇到了错误。我知道我传递的方法不对,但我不确定如何。

void helloGl::CameraMove(int x, int y)
{
oldMouseX = mouseX;
oldMouseY = mouseY;

// get mouse coordinates from Windows
mouseX = x;
mouseY = y;

// these lines limit the camera's range
if (mouseY < 60)
    mouseY = 60;
if (mouseY > 450)
    mouseY = 450;

if ((mouseX - oldMouseX) > 0)       // mouse moved to the right
    angle += 3.0f;`enter code here`
else if ((mouseX - oldMouseX) < 0)  // mouse moved to the left
    angle -= 3.0f;
}




void helloGl::mouse(int button, int state, int x, int y)
{
switch (button)
{
    // When left button is pressed and released.
case GLUT_LEFT_BUTTON:

    if (state == GLUT_DOWN)
    {
        glutIdleFunc(NULL);

    }
    else if (state == GLUT_UP)
    {
        glutIdleFunc(NULL);
    }
    break;
    // When right button is pressed and released.
case GLUT_RIGHT_BUTTON:
    if (state == GLUT_DOWN)
    {
        glutIdleFunc(NULL);
        //fltSpeed += 0.1;
    }
    else if (state == GLUT_UP)
    {
        glutIdleFunc(NULL);
    }
    break;
case WM_MOUSEMOVE:

    glutPassiveMotionFunc(CameraMove);

    break;

default:
    break;
}
}

1 个答案:

答案 0 :(得分:1)

假设helloGl是一个班级。那么答案是,你不能。函数与方法不同。问题是glutPassiveMotionFunc()期望:

void(*func)(int x, int y)

但是你试图给它的是:

void(helloGl::*CameraMove)(int x, int y)

换句话说,thiscall。这不起作用,因为与thiscall相比,cdecl 基本上有一个额外的隐藏参数。简单来说,您可以将CameraMove()想象为:

void CameraMove(helloGl *this, int x, int y)

如你所见,这不是一样的。因此,解决方案是将CameraMove()移出helloGl类或使方法保持静态。