The function of openGL operation matrix under Android

Matrix.multiplyMM(float[] result, int resultOffset, float[] lhs, int lhsOffset, float[] rhs, int rhsOffset)

Multiply two 4x4 matrices together and store the result in a third 4x4 matrix. In matrix notation: result = lhs x rhs. Due to the way matrix multiplication works, the result matrix will have the same effect as first multiplying by the rhs matrix, then multiplying by the lhs matrix. This is the opposite of what you might expect. The same float array may be passed for result, lhs, and/or rhs. However, the result element values are undefined if the result elements overlap either the lhs or rhs elements.

   Multiply two 4x4 matrices and store the result in a third 4x4 matrix. In matrix notation: result = lhs x rhs . Because of the way matrix multiplication works, the resulting matrix has the same effect as being multiplied by the matrix on the right and then by the matrix on the left. This is the opposite of what you would expect. The same float array can be used as the result matrix, left matrix, and right matrix. However, if the elements of the resulting matrix overlap with elements of the right or left matrix, the elements of the resulting matrix cannot be determined.

 

It is strange to see the following code in the official Android example:

Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);

The result matrix and the right matrix are both mmMVPMatrix, and the offset is 0.

Do the following test:

float[] lhs = new float[16];
float[] rhs = new float[16];
float[] res = new float[16];
Matrix.setRotateM(lhs, 0, 45, 1, 1, 0);
Matrix.setRotateM(rhs, 0, 30, 0, 1, 1);
System.out.println("lhs");
MatrixUtil.printMatrix(lhs);
System.out.println("rhs");
MatrixUtil.printMatrix(rhs);
Matrix.multiplyMM(res, 0, lhs, 0, rhs, 0);
System.out.println("res");
MatrixUtil.printMatrix(res);

Matrix.setRotateM(lhs, 0, 45, 1, 1, 0);
Matrix.setRotateM(rhs, 0, 30, 0, 1, 1);
Matrix.multiplyMM(rhs, 0, lhs, 0, rhs, 0);
System.out.println("overlap rhs res");
MatrixUtil.printMatrix(rhs);

Matrix.setRotateM(lhs, 0, 45, 1, 1, 0);
Matrix.setRotateM(rhs, 0, 30, 0, 1, 1);
Matrix.multiplyMM(lhs, 0, lhs, 0, rhs, 0);
System.out.println("overlap lhs res");
MatrixUtil.printMatrix(lhs);

 

 

operation result:

lhs

0.8536  0.1464  0.5000  0.0000  

0.1464  0.8536  -0.5000  0.0000  

-0.5000  0.5000  0.7071  0.0000  

0.0000  0.0000  0.0000  1.0000  

rhs

0.8660  -0.3536  0.3536  0.0000  

0.3536  0.9330  0.0670  0.0000  

-0.3536  0.0670  0.9330  0.0000  

0.0000  0.0000  0.0000  1.0000  

res

0.6142  -0.1316  0.7781  0.0000  

0.6054  0.7111  -0.3576  0.0000  

-0.5062  0.6907  0.5165  0.0000  

0.0000  0.0000  0.0000  1.0000  

overlap rhs res

0.6142  -0.1316  0.7781  0.0000  

0.6054  0.7111  -0.3576  0.0000  

-0.5062  0.6907  0.5165  0.0000  

0.0000  0.0000  0.0000  1.0000  

overlap lhs res

0.6142  -0.0470  0.6805  0.0000  

0.6054  0.5488  -0.2157  0.0000  

-0.5062  0.6929  0.5272  0.0000  

0.0000  0.0000  0.0000  1.0000

 

The result of the operation is correct when the resulting matrix overlaps the matrix on the right.

When the resulting matrix overlaps the left matrix, the result of the operation is incorrect.

It is understandable why Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0); works fine. Of course, in order to avoid problems, it is best not to overlap.

 

void android.opengl.Matrix.setRotateM(float[] rm, int rmOffset, float a, float x, float y, float z)

Set the matrix rm to the rotation matrix that rotates the angle a around axis (x, y, z).

 

void android.opengl.Matrix.rotateM(float[] m, int mOffset, float a, float x, float y, float z)

Rotates matrix m in place by angle a (in degrees) around the axis (x, y, z)

Multiply the matrix m to the right by a rotation matrix.

作用到一个物体上时,效果是让物体先旋转,再被m作用。

 

void android.opengl.Matrix.translateM(float[] m, int mOffset, float x, float y, float z)

Translates matrix m by x, y, and z in place.

让矩阵m右乘一个平移矩阵。

作用到一个物体时,效果是让物体先平移,再被m作用。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326688447&siteId=291194637