用平移手势上下左右旋转相机

我想使用opengl lookat函数向上或向下,向左或向右旋转摄像头(360度视图),使用opengl lookat函数,我使用swift和Metal(在本例中为Metal = opengl es)。 这里是代码:

lookat函数(这个函数在另一个ViewController中,它是inheritance了ViewController的主要ViewController和pan手势函数):

let viewMatrix = lookAt(location, center: ktarget, up: up) 

viewDidLoad和var:

  var ktarget = V3f() var up = V3f() let location =V3f() override func viewDidLoad() { super.viewDidLoad() location = V3f(0.0, 0.0, -2.0) ktarget = V3f(0.0, 0.0, 0.0) up = V3f(0.0, 1.0, 0.0) } 

平底锅手势:

  func pan(panGesture: UIPanGestureRecognizer){ up = V3f(0.0, 1.0, 0.0).Normalized() forward = (ktarget + -location).Normalized() right = Cross(forward, b: up).Normalized() if panGesture.state == UIGestureRecognizerState.Changed{ let pointInView = panGesture.locationInView(self.view) let xDelta = (lastPanLocation.x - pointInView.x)/self.view.bounds.width * panSensivity let yDelta = (lastPanLocation.y - pointInView.y)/self.view.bounds.height * panSensivity // To rotate left or right, rotate the forward around up and then use a cross product between forward and up to get the right vector. forward = rotationM3f(up, angle: Float(xDelta)) * forward.Normalized() right = Cross(forward, b: up).Normalized() // To rotate up or down, rotate forward vector around right vector and use a cross product between the right and forward to get the new up vector. forward = rotationM3f(right, angle: Float(yDelta)) * forward.Normalized() up = V3f(0.0, 1.0, 0.0).Normalized() ktarget = location + forward lastPanLocation = pointInView } else if panGesture.state == UIGestureRecognizerState.Began { ktarget = location + forward lastPanLocation = panGesture.locationInView(self.view) } } 

但是平移相机,我总是设置向量=(0,1,0),使人们只能看到180度垂直。 如果用户仍然平移当摄像机向上或向下(最大值)屏幕将翻转,目标的实际值x和z是每帧一点一点地改变(非常小的数字,如0.0000somenumber),所以绘制函数将每帧画对象差别不大。 任何人都知道如何修复翻转? 谢谢~~~