【问题标题】:Why android.Matrix.setRotateM returns a matrix whose rotation direction is the opposite of the expected rotation matrix?为什么android.Matrix.setRotateM返回一个旋转方向与预期旋转矩阵相反的矩阵?
【发布时间】:2019-06-26 04:02:40
【问题描述】:

例如,如果我给出以下参数:

float[] rotateMatrix = identityMatrix.clone();
Matrix.setRotateM(rotateMatrix, 0, 90, 0, 0, 1);

我期望一个逆时针旋转 90 度的矩阵,应该是:

0       -1.0     
1.0    0

但实际上返回的rotateMatrix是:

0       1.0    
-1.0    0

奇怪的是,渲染输出是正确的,图像逆时针(而不是顺时针)旋转了 90 度。为什么?

【问题讨论】:

  • 是不是因为返回的矩阵是列主顺序,所以我解释错了?

标签: android matrix opengl-es rotation glsl


【解决方案1】:

这是因为 OpenGL (ES) 矩阵是由向量按列主要顺序指定的。

android.opengl.Matrix:

矩阵数学实用程序。这些方法对 OpenGL ES 格式矩阵和存储在浮点数组中的向量进行操作。

矩阵是以列优先顺序存储的 4 x 4 列向量矩阵:

m[offset +  0] m[offset +  4] m[offset +  8] m[offset + 12]
m[offset +  1] m[offset +  5] m[offset +  9] m[offset + 13]
m[offset +  2] m[offset +  6] m[offset + 10] m[offset + 14]
m[offset +  3] m[offset +  7] m[offset + 11] m[offset + 15]

参见OpenGL ES Shading Language 3.20 Specification, 5.4.2 Vector and Matrix Constructors,第 110 页:

要通过指定向量或标量来初始化矩阵,组件会以列优先顺序分配给矩阵元素 .

mat4(float, float, float, float,  // first column
     float, float, float, float,  // second column
     float, float, float, float,  // third column
     float, float, float, float); // fourth column

所以 GLSL mat4 可以通过轴和平移的 4 个向量来设置:

mat4 m44 = mat4(
    vec4( Xx, Xy, Xz, 0.0),
    vec4( Yx, Xy, Yz, 0.0),
    vec4( Zx  Zy  Zz, 0.0),
    vec4( Tx, Ty, Tz, 1.0) );

绕z轴(0, 0, 1)向右(逆时针)数学旋转后,x轴为(0, 1, 0) em>,y轴为(-1, 0, 0),由此引出矩阵:

 0  1  0  0   // x axis vector
-1  0  0  0   // y axis vector
 0  0  1  0   // z axis vector
 0  0  0  1   // translation vector

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 2019-02-08
    相关资源
    最近更新 更多