繁体   English   中英

使用GLKit旋转OpenGL ES对象

[英]Rotating an OpenGL ES Object with GLKit

我正在尝试使用触摸在iOS中旋转OpenGL对象,但是遇到了一些麻烦。 我在这里抓住用户的触摸:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    UITouch *touch = [touches anyObject];
    startPoint = [touch locationInView:self.view];
    }

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
   {
   UITouch *touch = [touches anyObject];
   CGPoint point = [touch locationInView:self.view];
   dx = point.y - startPoint.y;
   dy = point.x - startPoint.x;
   startPoint = point;
   }

我在更新功能中使用它来执行旋转。 现在,我只是想在我从左到右触摸时从左到右旋转,然后在上下时从前到后旋转。 虽然我得到一个奇怪的组合旋转。 这是代码:

- (void)update
   {    
   float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
   GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);

   self.effect.transform.projectionMatrix = projectionMatrix;

   GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -3.5f);
   modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, -1, startPoint.x, startPoint.y, 0.0f);
   dx = dy =0;
   self.effect.transform.modelviewMatrix = modelViewMatrix;
   }

因为你告诉它在x和y中旋转:)

尝试这个 :

modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, startPoint.x, 1.0f, 0.0f, 0.0f);

这将围绕x轴旋转startPoint.x弧度。

您可以通过更改最后3个参数来旋转您想要的任何轴(即0,1,0将围绕y轴旋转,1,1,0将围绕x和y之间的轴旋转45°。)

NB感谢@Marcelo Cantos澄清:)

根据deanWombourne,您使用的GLKMatrix4Rotate错误。 执行时:

GLKMatrix4Rotate(modelViewMatrix, -1, startPoint.x, startPoint.y, 0.0f);

绕轴旋转-1弧度(startPoint.x,startPoint.y,0.0f)。 听起来更像是想要围绕(1,0,0)旋转startPoint.x弧度和围绕(0,1,0)旋转startPoint.y弧度。 所以,例如:

modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, startPoint.x, 1.0f, 0.0f 0.0f);
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, startPoint.y, 0.0f, 1.0f 0.0f);

或者你可能想要划分startPoint.x和startPoint.y,因为它会对触摸反应过度。

它也会有一些万向节锁定问题 - 主要是因为如果你先绕x旋转那么y轴不一定是你想象的那样,如果你先绕y旋转,那么x轴不一定是你想的地方它是。 您担心的是那件事吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM