Ios 使用平移手势上下左右旋转相机
我想上下左右旋转相机来观察对象(360度视图),使用opengl lookat功能进行平移手势,我使用swift和Metal(在本例中Metal=opengl es)。代码如下: lookat功能(该功能位于另一个ViewController中,该ViewController继承自具有viewDidLoad和pan手势功能的主ViewController): viewDidLoad和var:Ios 使用平移手势上下左右旋转相机,ios,swift,opengl-es,metal,Ios,Swift,Opengl Es,Metal,我想上下左右旋转相机来观察对象(360度视图),使用opengl lookat功能进行平移手势,我使用swift和Metal(在本例中Metal=opengl es)。代码如下: lookat功能(该功能位于另一个ViewController中,该ViewController继承自具有viewDidLoad和pan手势功能的主ViewController): viewDidLoad和var: var ktarget = V3f() var up = V3f() let location =
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),因此绘制功能将在每帧中绘制差异非常小的对象。有人知道如何修理翻转吗?谢谢~~~这不是问题所在,但是:永远不要规范化位置,只有基向量。当您调用“normalized”方法时,很可能是a=a.normalized()或者没有可见的结果。此时您的结果是什么?我希望闪烁,因为我相信它应该是向上=交叉(右,向前),而不是交叉(向前,右)?无论如何,如果这是正确的,那么我看到结果(0,0,0)的唯一方式是因为没有对基向量进行归一化。嗯…当我平移时,对象将消失@请输入一个断点,查看哪些向量发生了剧烈的变化,以及您现在发布的值太大了。可能是溢油?将这些向量规格化为up=Cross(a,b)
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)
}
}