我使用opengl並嘗試創建第一人稱相機。所有的例子都使用GLUT,我需要在可可中獲得鼠標差異。但是我遇到了一些問題,只要鼠標移動(這是可以預料的),就會調用mouseMoved。有沒有辦法讓這個mroe準確?或者像GLUTS glutMouseFunc這樣的simialer函數?如何使用Cocoa改變鼠標位置差異?
當前的嘗試:
-(void)mouseMoved:(NSEvent *)event{
static bool wrap = false;
if(!wrap){
NSPoint eventLocation = [event locationInWindow];
float centerX = self.frame.size.width/2 + [self window].frame.origin.x;
float centerY = self.frame.size.height/2 + [self window].frame.origin.y;
CGPoint mousePointCenter = CGPointMake(centerX, centerY);
CGWarpMouseCursorPosition(mousePointCenter);
int dx = eventLocation.x - self.frame.size.width/2 ;
int dy = eventLocation.y - self.frame.size.height/2 ;
const float mousespeed = 0.001;
angles.x += dx * mousespeed;
angles.y += dy * mousespeed;
if(angles.x < -M_PI)
angles.x += M_PI * 2;
else if(angles.x > M_PI)
angles.x -= M_PI * 2;
if(angles.y < -M_PI/2)
angles.y = -M_PI/2;
if(angles.y > M_PI/2)
angles.y = M_PI/2;
lookat.x = sinf(angles.x) * cosf(angles.y);
lookat.y = sinf(angles.y);
lookat.z = cosf(angles.x) * cosf(angles.y);
CGWarpMouseCursorPosition(mousePointCenter);
[self setNeedsDisplay:YES];
}
else{
wrap = true;
}
}
你可以分享你的完整代碼... –