Is Google's Android Opengl Tutorial Teaching Incorrect Linear Algebra

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

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!

OpenGL ES Android Matrix Transformations

I´ve been working with the example from the Android Training, the following approach finally works for me. (Based on Android Training > Displaying Graphics with OpenGL ES > Adding Motion)

  1. Use the correct vertex shader:

    private final String vertexShaderCode =
    // This matrix member variable provides a hook to manipulate
    // the coordinates of the objects that use this vertex shader
    "uniform mat4 uMVPMatrix;" +
    "attribute vec4 vPosition;" +
    "void main() {" +
    // the matrix must be included as a modifier of gl_Position
    " gl_Position = uMVPMatrix * vPosition;" +
    "}";
  2. In the renderer class:

    public class MyGL20Renderer implements GLSurfaceView.Renderer {
    [...]
    // create a model matrix for the triangle
    private final float[] mModelMatrix = new float[16];
    // create a temporary matrix for calculation purposes,
    // to avoid the same matrix on the right and left side of multiplyMM later
    // see https://stackoverflow.com/questions/13480043/opengl-es-android-matrix-transformations#comment18443759_13480364
    private float[] mTempMatrix = new float[16];
    [...]
  3. Apply transformations in onDrawFrame, start with translation:

    public void onDrawFrame(GL10 unused) {
    [...]
    Matrix.setIdentityM(mModelMatrix, 0); // initialize to identity matrix
    Matrix.translateM(mModelMatrix, 0, -0.5f, 0, 0); // translation to the left
  4. Then rotation:

    // Create a rotation transformation for the triangle
    long time = SystemClock.uptimeMillis() % 4000L;
    float mAngle = 0.090f * ((int) time);
    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);
  5. Combine rotation and translation, avoid using mModelMatrix

    "as the same matrix on the right and left side of multiplyMM" (see 2)

    // Combine Rotation and Translation matrices 
    mTempMatrix = mModelMatrix.clone();
    Matrix.multiplyMM(mModelMatrix, 0, mTempMatrix, 0, mRotationMatrix, 0);
  6. Combine the model matrix with the projection and camera view; again avoid using mModelMatrix

    "as the same matrix on the right and left side of multiplyMM" (see 2)

    // Combine the model matrix with the projection and camera view
    mTempMatrix = mMVPMatrix.clone();
    Matrix.multiplyMM(mMVPMatrix, 0, mTempMatrix, 0, mModelMatrix, 0);
  7. Draw the shape

    // Draw shape
    mTriangle.draw(mMVPMatrix);

Thank you all, for all the useful input I could draw from this thread.

Best OpenGL ES version for beginners

Your question should be closed since the answer to it would be completely subjective. Where it comes to the ES version there is no "better", only "better for your needs". I suggest you pick either ES1 or ES2.

There is a huge difference between the ES1 and the ES2 where the ES1 is still using a fixed pipeline and the ES2 is not. What that means is you will have to learn shaders in order to use ES2 and a lot of very handy tools such as a matrix stack are removed in ES2 due to the shaders. There are other libraries replacing those functionality but you still do need to understand them a bit more then in fixed pipeline.

So the ES1 is probably much easier for a beginner since you can have a nice drawing in a very short time but you will want to migrate to higher versions as quick as possible at which point most of the ES1 stuff will be useless to you. The ES2 can be a real pain to begin with and once you understand how things work you have quite a lot of power working with it, still that might take quite some time.

Also most of the current topics considering the openGL and the Android are targeting the ES2 version which means you will have most updated sources around the web.

Still in the end the choice is yours. I know I have not given you the answer but still I hope this helps you understand a bit what you are working with.

Best OpenGL ES version for beginners

Your question should be closed since the answer to it would be completely subjective. Where it comes to the ES version there is no "better", only "better for your needs". I suggest you pick either ES1 or ES2.

There is a huge difference between the ES1 and the ES2 where the ES1 is still using a fixed pipeline and the ES2 is not. What that means is you will have to learn shaders in order to use ES2 and a lot of very handy tools such as a matrix stack are removed in ES2 due to the shaders. There are other libraries replacing those functionality but you still do need to understand them a bit more then in fixed pipeline.

So the ES1 is probably much easier for a beginner since you can have a nice drawing in a very short time but you will want to migrate to higher versions as quick as possible at which point most of the ES1 stuff will be useless to you. The ES2 can be a real pain to begin with and once you understand how things work you have quite a lot of power working with it, still that might take quite some time.

Also most of the current topics considering the openGL and the Android are targeting the ES2 version which means you will have most updated sources around the web.

Still in the end the choice is yours. I know I have not given you the answer but still I hope this helps you understand a bit what you are working with.



Related Topics



Leave a reply



Submit