使用 GLKit 旋转 OpenGL ES 对象

发布于 2024-12-24 22:14:32 字数 1159 浏览 0 评论 0原文

我正在尝试使用触摸在 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;
   }

I'm trying to rotate an OpenGL object in iOS using touch, but I'm having some trouble. I am grabbing the user touches here:

- (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;
   }

I'm using that in my update function to perform the rotation. Right now I'm just trying to rotate left to right when I touch left to right and then front to back when I go up and down. I get a weird combination rotation though. Here's the code:

- (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;
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

属性 2024-12-31 22:14:32

因为你告诉它在 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°。)

注意:谢谢@Marcelo Cantos 澄清:)

Because you're telling it to rotate in both x and y :)

Try this :

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

This will rotate startPoint.x radians around the x-axis.

You can rotate about any axis you want by changing the last 3 parameters (i.e. 0,1,0 would rotate about the y-axis, 1,1,0 would rotate about a axis 45° between the x and y.)

NB Thanks @Marcelo Cantos for the clarification :)

记忆で 2024-12-31 22:14:32

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

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

绕轴 (startPoint.x、startPoint.y、0.0f) 旋转 -1 弧度。听起来更像是您想要围绕 (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 轴不一定在您认为的位置这是。这是你关心的事情吗?

As per deanWombourne, you're using GLKMatrix4Rotate incorrectly. When you perform:

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

You rotate -1 radians around the axis (startPoint.x, startPoint.y, 0.0f). It sounds more like you want to rotate startPoint.x radians around (1, 0, 0) and startPoint.y radians around (0, 1, 0). So, for example:

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

Or you probably want to divide startPoint.x and startPoint.y, because that'll be hyper-responsive to touches.

It'll also have some gimbal lock issues — essentially because if you rotate around x first then the y axis isn't necessarily where you think it is, and if you rotate around y first then the x axis isn't necessarily where you think it is. Is that something you're concerned about?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文