glutPassiveMotionFunc problems

glutPassiveMotionFunc problems

本文关键字:problems glutPassiveMotionFunc      更新时间:2023-10-16

我对 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;
}
}

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

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

但你想给它的是:

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

换句话说,这是一个电话。这是行不通的,因为与 cdecl 相比,thiscall 基本上有一个额外的隐藏参数。总而言之,您可以想象您的CameraMove()为:

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

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