OpenGL 模型视图矩阵 4x4 的紧凑表示

发布于 2024-10-15 12:01:39 字数 485 浏览 2 评论 0原文

仅存储 OGL 模型视图 (4x4) 矩阵的旋转部分的最用户友好的方法是什么?

例如;在关卡编辑器中设置对象的旋转时,可以轻松使用 XYZ 欧拉角。然而,这似乎是一个与矩阵一起使用的非常棘手的系统。

我需要能够从这个新的表示中获取并设置旋转。

(另一种方法是存储旋转部分(4*3 数字),但用户很难操作这些)

我在这里找到了一些代码 http://www.google.com/codesearch/p?hl= en#HQY9Wd_snmY/mesh/matrix3.h&q=matrix3&sa=N&cd=1&ct=rc 允许我设置和获取角度旋转(3 个浮点数)。这是理想的。

What is the most user friendly way to store only the rotation part of an OGL modelview (4x4) matrix?

For example; in a level editor to set the rotation for an object it would be easy to use the XYZ Euler angles. However this seems a very tricky system to use with matrices.

I need to be able to get AND set the rotation from this new representation.

(The alternative is to store the rotation part (4*3 numbers) but it is hard for a user to manipulate these)

I found some code here http://www.google.com/codesearch/p?hl=en#HQY9Wd_snmY/mesh/matrix3.h&q=matrix3&sa=N&cd=1&ct=rc that allows me to set and get rotation from angles (3 floats). This is ideal.

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

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

发布评论

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

评论(5

指尖微凉心微凉 2024-10-22 12:01:39

尽管它们经常被使用,但我忽略了欧拉角的使用。它们是有问题的,因为它们只保留对象的指向方向,而不保留该方向的双切线。更重要的是:它们很容易出现万向节锁 http://en.wikipedia.org/wiki/Gimbal_lock

存储旋转的一种更高级的方法是四元数。通俗地说,四元数由旋转轴和绕该轴的旋转角度组成。因此它是 4 个标量 a、b、c、d 的元组。四元数为 Q = a + i*b + j*c + k*d, |Q| = 1,具有 i,j,k 的特殊属性 i² = j² = k² = i·j·k = -1 且 i·j = k, j·k = i, k·i = j,这意味着 j ·i = -k, k·j = -i, i·k = -j

四元数因此是复数的扩展。如果您还记得复数理论,您会记得两个复数 a =/= b 与 |a| 的乘积=|b| = 1 是复平面上的旋转。因此,很容易假设 3D 中的旋转可以通过将复数扩展到复超平面来描述。这就是四元数。

有关详细信息,请参阅这篇文章。
http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation

Although they're used regularily, I disregard the use of Euler angles. They're problematic as they only preserve the pointing direction of the object, but not the bitangent to that direction. More important: They're prone to gibal lock http://en.wikipedia.org/wiki/Gimbal_lock

A far superior method for storing rotations are Quarternions. In layman terms a quaternion consists of the rotational axis and the angle of rotation around this axis. It is thus a tuple of 4 scalars a,b,c,d. The quaternion is then Q = a + i*b + j*c + k*d, |Q| = 1, with the special properties of i,j,k that i² = j² = k² = i·j·k = -1 and i·j = k, j·k = i, k·i = j, which implies j·i = -k, k·j = -i, i·k = -j

Quaterions are thus extensions of complex numbers. If you recall complex number theory, you'll remember that the product of two complex numbers a =/= b with |a| = |b| = 1 is a rotation in the complex plane. It is thus easy to assume that rotations in 3D can be described by an extension of complex numbers into a complex hyperplane. This is what quaternions are.

See this article on the details.
http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation

南七夏 2024-10-22 12:01:39

在标准 3D 矩阵中,您只需要左上角的 3x3 值即可进行旋转。稍后要将矩阵应用为 4x4,您需要将除对角线上的其他值设为 0。

这是一个仅旋转矩阵,其中值 vXY 给出旋转。

[v00 v01 v02  0]
[v10 v11 v12  0]
[v20 v21 v22  0]
[  0   0   0  1]

有趣的是,这些值形成了将对象旋转到的坐标系的基础,因此在新系统中,x 轴沿 [v00 v01 v02],y 轴沿 [v10 v11 v12],z 轴沿 [v00 v01 v02] -axis 显然是[v20 v21 v22]。

您可以在对象旁边显示这些轴,并让另一个轴拖动它们来更改旋转。

In a standard 3D matrix you only need the top left 3x3 values to give the rotation. To apply the matrix as a 4x4 later on, you need to make the other values 0 apart from on the diagonal.

Here's a rotation only matrix where the values vXY give the rotations.

[v00 v01 v02  0]
[v10 v11 v12  0]
[v20 v21 v22  0]
[  0   0   0  1]

Interestingly, the values form the bases of the coordinate system you have rotated the object into, so in the new system, the x-axis is along [v00 v01 v02], the y-axis is along [v10 v11 v12] and the z-axis obviously [v20 v21 v22].

You could show these axes beside the object and let the other drag them around to change the rotation, perhaps.

南风几经秋 2024-10-22 12:01:39

我想说这取决于用户,但对我来说最“用户友好”的方式是存储“roll”、“pitch”和“yaw”。这些是普通用户可以理解和调整的非技术术语,您应该很容易获取这些值并计算矩阵。

I would say this depends on the user, but to me the most "user friendly" way is to store "roll", "pitch" and "yaw". These are very non-technical terms that an average user can understand and adjust, and it should be easy for you to take these values and compute the matrix.

自由如风 2024-10-22 12:01:39

IMO,最“用户友好的格式”旋转是存储 Euler XYZ 角度,这通常是任何 3D 内容创建软件中旋转的展示方式。

欧拉角很容易转换为矩阵,请参阅此处了解涉及的矩阵产品。

但是你不应该混淆给予GUI/用户的格式和数据的存储格式:Euler XYZ角度在制作动画时有自己的问题,万向节锁可能会导致不良行为。

存储/计算旋转的另一个候选者是四元数。与 XYZ 角度相比,它们具有数学优势,尤其是在两个旋转之间进行插值时。当然,您不想将四元数值直接暴露给任何人类用户,您需要将它们转换为 XYZ 角度。您可以在网上找到大量相关代码。

我不建议直接以矩阵格式存储旋转。从中提取用户友好的值很困难,它不提供任何有趣的动画/插值行为,它需要存储。 IMO,当需要变换几何形状时将创建矩阵。

总而言之,有几个选项,您应该选择最适合您的。你打算不做动画吗?等等

编辑
另外,您不应该使用模型矩阵和视图矩阵进行混合。它们在语义上非常不同,并且仅出于性能原因在 OpenGL 中组合。我在上面的“模型矩阵”中想到了什么。视图矩阵通常由视图系统/相机管理器给出,并与模型矩阵相结合。

IMO, the most 'user friendly format' for rotation is storing Euler XYZ angles, this is generally how rotations are exposed in any 3d content creation software.

Euler angles are easy to transform to matrices, see here for the involved matrix product.

But you should not confuse the format given to the GUI/user and the storage format of the data: Euler XYZ angles have problems of their own when doing animation, gimbal lock can introduce unwanted behaviour.

Another candidate for storing/computing rotations is quaternions. They offer mathematical advantages over XYZ angles, essentially when interpolating between two rotations. Of course, you don't want to expose the quaternion values directly to any human user, you'll need to convert them to XYZ angles. You'll find plenty of code to that on the Web.

I would not recommend storing the rotation directly in matrix format. Extracting user friendly values from it is difficult, it does not offer any interesting behaviour for animation/interpolation, it takes for storage. IMO, matrices are to be created when needed to transform the geometry.

To conclude, there are a few options, you should select what suits you most. Do you plan to having animation or not ? etc.

EDIT
Also, you should not make an amalgam with model and view matrices. They are semantically very different, and are combined in OpenGL only for performance reasons. What I had in mind above in the 'model matrix'. The view matrix is generally given by your view system/camera manager, and is combined with you model matrix.

暖树树初阳… 2024-10-22 12:01:39

尽管数学“晦涩难懂”,但四元数却对用户非常友好,因为它代表绕轴旋转给定角度。

旋转轴只是指向该方向的单位向量乘以 1/2 旋转角度的正弦,“模糊”第四分量等于 1/2 旋转角度的余弦。

乍一看感觉有点“不自然”,但一旦掌握了……还能更容易一点吗?

A quaternion is, although the math is "obscure and unintellegible" surprisingly user friendly, as it represents rotation around an axis by a given angle.

The axis of rotation is simply a unit vector pointing in that direction, multiplied by the sine of 1/2 the rotation angle, and the "obscure" 4th component equals the cosine of 1/2 the rotation angle.

It feels kind of "unnatural" at first sight, but once you grasp it... can it be any easier?

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