从OpenGL中的矩阵获取位置,旋转和缩放 [英] Get position, rotation and scale from matrix in OpenGL

查看:470
本文介绍了从OpenGL中的矩阵获取位置,旋转和缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在学习用于Android开发的OpenGL ES.现在我要处理ModelMatrixCameraMatrix了,但是我有一些疑问:在OpenGL中,我们总是使用4x4矩阵,我知道为什么这样做,但是我不知道在哪里不同的值存储在此矩阵中.例如旋转和位置.假设我们有这个4x4矩阵:

At the moment I'm learning OpenGL ES for Android development. Now I'm at the point where I have to deal with the ModelMatrix and the CameraMatrix but I have some questions for it: In OpenGL we always use 4x4 matrix and I understand why we do this but I do not know where the different values are stored in this matrix. For example the rotation and the position. Let's say we have this 4x4 matrix:

      0   1   2   3
    -----------------
 0  |   |   |   |   |
    -----------------
 1  |   |   |   |   |
    -----------------
 2  |   |   |   |   |
    -----------------
 3  |   |   |   |   |
    -----------------

您能告诉我在此矩阵中哪里可以找到对象/摄像机的当前rotationpositionscale吗?难道每个矩阵都在同一位置吗?

Can you please tell me where I can find the current rotation, position and the scale of the object / camera in this matrix? And is this for every matrix in the same position?

推荐答案

为简单起见,我假设矩阵仅包含旋转,平移和均匀缩放的组合.通用转换矩阵可以表示其他类型的翻译,例如:

To keep things simple, I assume that the matrix only contains a combination of rotation, translation, and uniform scaling. General transformation matrices can represent other types of translations, like:

  • 非均匀缩放.
  • 剪切变形.

旋转和缩放在矩阵的左上3x3部分中,它们是在每个方向上索引为0到3的元素.如果没有缩放比例,则此3x3矩阵的每个行向量的长度为1.0.如果存在缩放,则可以通过计算以下任何行向量的长度来计算缩放因子:

Rotation and scale are in the top-left 3x3 part of the matrix, which are the elements with index 0 to 3 in each direction. If there is no scaling, each row vector of this 3x3 matrix has length 1.0. If there is scaling, you can calculate the scaling factor by calculating the length of any of these row vectors:

scalingFactor = sqrt(m00 * m00 + m01 * m01 + m02 * m02);

然后通过将3x3子矩阵的所有元素除以比例因子来获得旋转矩阵:

The rotation matrix is then obtained by dividing all elements of the 3x3 sub-matrix by the scaling factor:

                                         [ m00 m01 m02 ]
rotationMatrix = (1.0 / scalingFactor) * [ m10 m11 m12 ]
                                         [ m20 m21 m22 ]

翻译向量位于最后一列的前3个元素中:

The translation vector is in the first 3 elements of the last column:

                    [ m03 ]
translationVector = [ m13 ]
                    [ m23 ]

所有这些,请记住,OpenGL期望矩阵以列优先顺序存储.因此,矩阵在内存中的排列为:

With all of this, keep in mind that OpenGL expects matrices stored in column-major order. So the arrangement of the matrix in memory is:

m00 m10 m20 m30 m01 m11 ...

这篇关于从OpenGL中的矩阵获取位置,旋转和缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆