2017-04-13 51 views
0

我还有一个新的glut和opengl,我试图让鼠标移动时的相机移动,但当试图让鼠标在屏幕上的位置我假设你想传递的方法,你应该只是x和y在glutPassiveMotionFunc()参数中被引用。但是当我尝试赋予CameraMove方法的功能时出现错误。我知道我错了,但我不知道如何。glutPassiveMotionFunc问题

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

假设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类或使该方法静态。