Is Google's Android OpenGL tutorial teaching incorrect linear algebra?

后端 未结 5 1493
野性不改
野性不改 2020-11-28 08:07

After helping another user with a question regarding the Responding to Touch Events Android tutorial, I downloaded the source code, and was quite baffled by what I saw. The

5条回答
  •  北海茫月
    2020-11-28 08:39

    As the guy who wrote that OpenGL tutorial, I can confirm that the example code is incorrect. Specifically, the order of the factors in the shader code should be reversed:

    "  gl_Position = uMVPMatrix * vPosition;"
    

    As to the application of the rotation matrix, the order of the factors should also be reversed so that the rotation is the last factor. The rule of thumb is that matrices are applied in right-to-left order, and the rotation is applied first (it's the the "M" part of "MVP"), so it needs to be the rightmost operand. Furthermore, you should use a scratch matrix for this calculation, as recommended by Ian Ni-Lewis (see his more complete answer, below):

    float[] scratch = new float[16];
    // Combine the rotation matrix with the projection and camera view
    Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);
    

    Thanks for calling attention to this problem. I'll get the training class and sample code fixed as soon as I can.

    Edit: This issue has now been corrected in the downloadable sample code and the OpenGL ES training class, including comments on the correct order of the factors. Thanks for the feedback, folks!

提交回复
热议问题