如何在Android OpenGL中创建3D旋转效果?

发布于 2024-12-18 06:58:51 字数 356 浏览 1 评论 0原文

我目前正在使用 OpenGL-ES 开发适用于 Android 的 3D 模型查看器。我想根据给定的手势创建旋转效果。 我知道如何进行单轴旋转,例如仅在 x、y 或 z 轴上旋转。然而,我的问题是我不知道如何将它们全部组合在一起,并且让我的应用程序知道我想根据触摸手势在哪个轴上旋转。 我想到的手势是:

  • 向上/向下滑动用于 x 轴
  • 向左/向右滑动用于 y 轴
  • 进行圆周运动滑动用于 z 轴

我该怎么做?

编辑:我发现 3 种类型的滑动会让这一刻变得非常难看。因此我所做的就是删除 z 轴运动。删除该条件后,我发现其他两个与相同的算法结合使用效果非常好。

I am currently working on a 3D model viewer for Android using OpenGL-ES. I want to create a rotation effect according to the gesture given.
I know how to do single-axis rotation, such as rotate solely on the x-, y- or z-axis. However, my problem is that I don't know how to combine them all 3 together and have my app know in which axis I want to rotate depending on the touch gesture.
Gestures I have in mind were:

  • Swipe up/down for x-axis
  • Swipe left/right for y-axis
  • swipe in circular motion for z-axis

How can I do this?

EDIT: I found out that 3 types of swipes can make the moment very ugly. Therefore what I did was remove the z-axis motion. After removing that condition, I found that the other 2 work really well in conjunction with the same algorithm.

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

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

发布评论

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

评论(2

泛滥成性 2024-12-25 06:58:51

http://developer.android.com/resources/articles/gestures.html有一些关于构建“手势库”的信息。我自己没有检查过,但似乎适合您正在寻找的内容

或者,尝试类似 gestureworks.com (再次,我自己没有尝试过)

我使用进度条视图来放大和缩小。然后,我使用 GLSurfaceView 上的 x 方向的移动来绕 y 轴旋转,并使用 y 方向的移动来绕 x 轴旋转。

手势的问题在于,当应用程序尝试确定用户使用的手势时,响应不是即时的。如果您随后使用用户移动手指的距离来确定旋转/缩放的量,则没有即时反馈,因此用户需要时间来学习如何控制旋转/缩放的量。我想如果你每个手势旋转/缩放一定的量就会起作用

http://developer.android.com/resources/articles/gestures.html has some info on building a 'gesture library'. Not checked it out myself, but seems to fit what you're looking for

Alternatively, try something like gestureworks.com (again, I've not tried it myself)

I use a progressbar view for zooming in and out. I then use movement in x on the GLSurfaceView to rotate around the y axis and movement in y to rotate around the x axis

The problem with a gesture is that the response is not instant as the app tries to determine what gesture the user used. If you then use how far the user moves their finger to determine the amount to rotate/zoom then there is no instant feedback and so it takes time for the user to learn how to control rotate/zoom amount. I guess it would work if you were rotating/zooming by a set amount each gesture

新一帅帅 2024-12-25 06:58:51

听起来你想做的事情比你想象的更加数学密集。有两种方法可以从数学上做到这一点。 (1)使用四元数(2)使用基本线性代数(但如果你不小心,会导致万向节锁定..但因为你只是旋转,所以这不是你关心的)..让我们走第二条路,因为它更容易..您需要做的是通过手势工具接收滑动的起点和终点,当您获得这两个点时..计算它所形成的线。当你有了这条线时,你可以用高中数学轻松找到该线的垂直向量。现在这应该是旋转矩阵中的旋转轴:

    //Rotate around the axis based on the rotation matrix (rotation, x, y, z)
    gl.glRotatef(rot, 1.0f, 1.0f, 0.0f);

不需要 Z 旋转,因为您无法使用 2D 平板电脑在 Z 平面中旋转。 1.0f、1.0f 是代表向量 x、y 的变量值。 (rot) 应该作为两点之间距离的大小。

我已经有一段时间没有这样做了,所以如果您需要更高的精度,请告诉我。

It sounds like what you are looking to do is more math intensive than you might know. There are two ways to do this mathematically. (1) using quaternions (2) using basic linear algebra (but will result in gimbal lock if you arent careful.. but since you are just spinning then this is not a concern to you).. Lets go the second route since its easier.. What you need to do is recieve the beginning and end points of the swipe via a gesture implement and when you have those two points.. calculate the line that it makes. When you have that line, you can easily find the perpendicular vector to that line with high school math. That should now be your axis of rotation in your rotation matrix:

    //Rotate around the axis based on the rotation matrix (rotation, x, y, z)
    gl.glRotatef(rot, 1.0f, 1.0f, 0.0f);

No need for the Z rotation since you can not rotate in the Z plane with a 2D tablet. The 1.0f, 1.0f are the values that should be variables that represent the x,y of your vector. The (rot) should serve as the magnitude of the distance between the two points.

I havent done this in a while so let me know if you need more precision.

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