我在打开的gl es2中使用了以下几行来旋转对象。
平移和缩放功能正常运行,但不会发生旋转。

//modelViewMatrix is float[16];

Matrix.setIdentity(modelViewMatrix,0);
Matrix.translateM(modelViewMatrix, 0, x, y, z);
Matrix.rotateM(modelViewMatrix, 0, ax, 1, 0, 0);

//shader concatentation
//gl_Position = projection*modelview*position;


我尝试制作一个自定义矩阵进行旋转,但结果仍然相同。

如果删除旋转线,则平移效果很好,但是一旦添加旋转线,平移也会停止。

令人惊讶地发现,这种相同的方法可以在iOS的OpenGLES2中正常工作。

我该如何解决?在android上的OpenGL es2中旋转对象的正确方法是什么?

最佳答案

既然还没有合格的答案,那么我将把我从openGLES2中“挑选出来”的东西中解脱出来,这可能是正确的,也可能是不正确的。

我的代码似乎类似于ClayMontogmery所说的-我翻译我的模型矩阵,然后有一个单独的float [16]矩阵来保存旋转(mCurrentRotation)。完成此操作后,我将矩阵相乘并将结果放入一个临时矩阵中,然后将其用于投影(这里的术语可能有点怪异),下面显示了我如何平移和旋转。我从前段的示例中将我的卵石拼凑起来:

    Matrix.translateM(mModelMatrix, 0, 0.0f, 0.8f, -3.5f);

    // Set a matrix that contains the current rotation.
    Matrix.setIdentityM(mCurrentRotation, 0);
    Matrix.rotateM(mCurrentRotation, 0, mDeltaX, 0.0f, 1.0f, 0.0f);
    Matrix.rotateM(mCurrentRotation, 0, mDeltaY, 1.0f, 0.0f, 0.0f);

    // Multiply the current rotation by the accumulated rotation, and then set the accumulated rotation to the result.
    Matrix.multiplyMM(mTemporaryMatrix, 0, mCurrentRotation, 0, mAccumulatedRotation, 0);
    System.arraycopy(mTemporaryMatrix, 0, mAccumulatedRotation, 0, 16);

    // Rotate the model taking the overall rotation into account.
    Matrix.multiplyMM(mTemporaryMatrix, 0, mModelMatrix, 0, mAccumulatedRotation, 0);
    System.arraycopy(mTemporaryMatrix, 0, mModelMatrix, 0, 16);


然后在drawModel中

/* this multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix (which now contains model * view * projection). */
Matrix.multiplyMM(mTemporaryMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);


以及onSurfaceCreated的最后一行:

Matrix.setIdentityM(mAccumulatedRotation, 0);


这可能无法完全回答您的问题,但是此代码可以满足我的需要,因此您可以从中获得一些帮助。如果有任何“不明智”的事,我事先表示歉意,我对此的知识充其量是稀缺的,我之所以回答是因为没有其他人给出任何可用的答案。

07-28 01:49
查看更多