rotate camera up and down, left and right with pan gesture

点点圈 提交于 2019-12-10 23:01:17

问题


I want to rotate camera up or down and left or right to look around an object, (360 degrees view) with pan gesture using opengl lookat function, I use swift and Metal(in this case Metal = opengl es). here is the code:

the lookat function(this function is in another ViewController which is inheritance from the main ViewController which is with the viewDidLoad and pan gesture function):

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

the viewDidLoad and 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)
}

the pan gesture:

    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)
            }
        }

But pan the camera, I set up the up vector always =(0,1,0), to make people only can see 180 degree vertically. If user still pan When Camera look up or down(max value)the screen will flip, the real value x and z of target is changed little by little(very small numebr, such as 0.0000somenumber) every frames, so the draw function will draw the object very little difference every frames. Anyone knows how can fix the flip? Thanks~~~

来源:https://stackoverflow.com/questions/36360738/rotate-camera-up-and-down-left-and-right-with-pan-gesture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!