2017-04-13 49 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類或使該方法靜態。