是谷歌的Andr​​oid的OpenGL教程教不正确线性代数?线性代数、不正确、教程、Andr

2023-09-12 01:09:03 作者:幕后煮屎者

在帮助其他用户有一个问题关于响应触摸事件 Android的教程中,我下载源$ C ​​$ C,并颇受我所看到的困惑。本教程似乎无法决定是否要使用行向量或列向量,它看起来全都搞混了,给我的。

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 tutorial seems to not be able to decide whether it wants to use row vectors or column vectors, and it looks all mixed up to me.

在Android的矩阵页面上,他们声称他们的惯例是列向量/列为主,这是典型的OpenGL的。

On the Android Matrix page, they claim that their convention is column-vector/column-major, which is typical of OpenGL.

我是正确的,或者是有什么我失踪?下面是它的相关位:

Am I right, or is there something I am missing? Here are the relevant bits of it:

开始由乘以mProjMatrix * mVMatrix创建MVPMatrix。到目前为止好。

Start out by creating a MVPMatrix by multiplying mProjMatrix * mVMatrix. So far so good.

    // Set the camera position (View matrix)
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    // Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0)

接着它们附加一个旋转到MVPMatrix的左手侧?这似乎有点不可思议。

Next they are appending a rotation to the left hand side of the MVPMatrix? This seems a little weird.

    // Create a rotation for the triangle
    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);

    // Combine the rotation matrix with the projection and camera view
    Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0)

上传非调换顺序。

Uploading in non-transposed order.

    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);

最后,在他们的着色器,一个向量*矩阵乘法?

Finally in their shader, a vector*matrix multiplication?

    // the matrix must be included as a modifier of gl_Position
    "  gl_Position = vPosition * uMVPMatrix;" 

添加此一起,我们得到:

Adding this all together, we get:

gl_Position = vPosition * mRotation * mProjection * mView;

这是没有任何发挥我的想象力是正确的。有没有办法,我没有看到,什么是怎么回事任何解释?

Which is not correct by any stretch of my imagination. Is there any explanation that I'm not seeing as to what's going on here?

推荐答案

至于谁写的OpenGL的教程的家伙,我可以证实,例如code不正确。具体而言,该因素在着色器code的顺序应该颠倒:

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;"

至于旋转矩阵的应用,各因素的次序也应该颠倒,使得旋转的最后一个因素。经验法则是,矩阵适用于从右到左的顺序,以及旋转首先应用(这是MVP的M的一部分),所以它需要最右边的操作数。此外,你应该使用一个临时矩阵以此计算,所推荐的伊恩镍 - 刘易斯(看他的更完整的答案,下同):

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);

感谢提请注意这个问题。我会得到的培训班和样品code为尽快解决,我可以。

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

编辑::该问题已在可下载的样本code和OpenGL ES的培训班,其中包括的因素,以正确的顺序上的评论得到纠正。感谢您的反馈,乡亲们!

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!