使用平移手势向上和向下,向左和向右旋转相机

时间:2016-04-01 15:57:26

标签: ios swift opengl-es metal

我想使用opengl lookat函数向上或向下和向左或向右旋转相机以使用平移手势环顾对象(360度视图),我使用swift和Metal(在本例中为Metal = opengl es)。这是代码:

lookat函数(此函数位于另一个ViewController中,它继承自主ViewController,具有viewDidLoad和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度。如果用户仍然平移当Camera向上或向下(最大值)时,屏幕将翻转,目标的实际值x和z逐渐变化(非常小的数字,例如0.0000sonsumber)每帧,因此绘制功能将每帧画出的物体差别很小。谁知道如何修复翻盖?感谢~~~

0 个答案:

没有答案