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

后端 未结 5 1490
野性不改
野性不改 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:46

    I’m working on the same issue and that’s what I found:

    I believe that Joe’s sample is CORRECT,
    including the order of the factors in the shader code:

    gl_Position = vPosition * uMVPMatrix;
    

    To verify it just try to rotate the triangle with reversed factors order, it will stretch the triangle to vanishing point at 90 degrees.

    The real problem seems to be in setLookAtM function.
    In Joe’s sample parameters are:

    Matrix.setLookAtM(mVMatrix, 0,
         0f, 0f,-3f,   0f, 0f, 0f,   0f, 1f, 0f );
    

    which is perfectly logical as well.
    However, the resulting view matrix looks weird to me:

    -1  0  0  0
     0  1  0  0
     0  0 -1  0
     0  0 -3  1
    

    As we can see, this matrix will invert X coordinate, since the first member is –1,
    which will lead to left/right flip on the screen.
    It will also reverse Z-order, but let's focus on X coordinate here.

    I think that setLookAtM function is also working correctly.
    However, since Matrix class is NOT a part of OpenGL, it can use some other coordinates system,
    for example - regular screen coordinates with Y axis pointing down.
    This is just a guess, I didn’t really verify that.

    Possible solutions:
    We can build desirable view matrix manually,
    the code is:

    Matrix.setIdentityM(mVMatrix,0);
    mVMatrix[14] = -3f;
    

    OR
    we can try to trick setLookAtM function by giving it reversed camera coordinates: 0, 0, +3 (instead of –3).

    Matrix.setLookAtM(mVMatrix, 0,
         0f, 0f, 3f,   0f, 0f, 0f,   0f, 1f, 0f );
    

    The resulting view matrix will be:

    1  0  0  0
    0  1  0  0
    0  0  1  0
    0  0 -3  1
    

    That’s exactly what we need.
    Now camera behaves as expected,
    and sample works correctly.

提交回复
热议问题