谷歌的 Android OpenGL 教程教的线性代数不正确吗? [英] Is Google's Android OpenGL tutorial teaching incorrect linear algebra?

查看:10
本文介绍了谷歌的 Android OpenGL 教程教的线性代数不正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在帮助其他用户解决有关响应触摸事件 Android教程,我下载了源代码,看到的东西很困惑.该教程似乎无法决定是要使用行向量还是列向量,而且在我看来都混为一谈.

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 Matrix 页面上,他们声称他们的约定是列向量/列优先,这是 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 教程的人,我可以确认示例代码不正确.具体来说,着色器代码中因子的顺序应该颠倒:

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"部分),所以它需要是最右边的操作数.此外,您应该按照 Ian Ni-Lewis 的建议使用临时矩阵进行此计算(请参阅下面的更完整答案):

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.

此问题现已在可下载的示例代码和 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!

这篇关于谷歌的 Android OpenGL 教程教的线性代数不正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆