0

I am using default android sample code http://developer.android.com/training/graphics/opengl/touch.html In this sample we can rotate triangle by toucht events.

I want just to add movement by x,y axiss for test purposes. The point that triangle behaviour is not as i am expecting. What i am doing wrong?

Code from tutorial with my new row hilighted:

public void onDrawFrame(GL10 unused) {

        // Draw background color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

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


        // Draw square
        mSquare.draw(mMVPMatrix);



       **//Translating this matrix 'brakes' triangle
  ->      Matrix.translateM(mMVPMatrix, 0, 0, pos, -1.0f);

        //NOTHING happens here: ??? Why?
  ->      Matrix.translateM(mRotationMatrix, 0, pos, 0, -1.0f);**

        // Create a rotation for the triangle
        // long time = SystemClock.uptimeMillis() % 4000L;
        // float angle = 0.090f * ((int) time);
        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);

        // Draw triangle
        mTriangle.draw(mMVPMatrix);
    }

Default behaviour:

enter image description here

With my code:

enter image description here

Ievgen
  • 4,261
  • 7
  • 75
  • 124
  • 1
    Hi. You should probably know that that tutorial is somewhat broken ([see this question](http://stackoverflow.com/questions/11925647/is-googles-android-opengl-tutorial-teaching-incorrect-linear-algebra)). One of the Google Engineers said they would fix it as soon as possible but it looks like from your example it has not yet been fixed. Their example code is a little bit nonsensical. – Tim Sep 28 '12 at 19:46
  • 1
    Also I think one of the answers there contains some fixed code, you may want to try that. – Tim Sep 28 '12 at 19:47
  • 2
    You can not do translation/rotation/scaling on MVP matrix and get results as you expect. you must translate/rotate your object in model matrix(or in View matrix for camera trans/rotation). Look at this http://stackoverflow.com/questions/5550620/the-purpose-of-model-view-projection-matrix to understand better what you need to do – Igor Sep 28 '12 at 19:58
  • ok than. But I have rotation matrix. Can i use it for movement? And even with fixes from that article code does nothing: Matrix.translateM(mRotationMatrix, 0,pos, 0, 0); – Ievgen Sep 28 '12 at 20:14
  • 1
    These are the steps: 1. set M matrix to Identity matrix. Translate or rotate it. Be aware of gimbal lock (http://en.wikipedia.org/wiki/Gimbal_lock) 2. set V matrix Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); 3. you already have projection matrix (in your case mProjMatrix) 4. multippy M * V * P to recieve final MVP matrix – Igor Sep 29 '12 at 15:23
  • 1
    I am facing the [same problem](http://stackoverflow.com/questions/13480043/opengl-es-android-class-structure-understanding). Could you post the solution, if you found it? – Michael T Nov 20 '12 at 21:19

1 Answers1

1

Thanks for a icrev comment:

You can not do translation / rotation / scaling on MVP matrix and get results as you expect.

you must translate / rotate your object in model matrix (or in View matrix for camera trans/rotation).

Look at this The purpose of Model View Projection Matrix to understand better what you need to do

These are the steps:

  1. set M matrix to Identity matrix. Translate or rotate it. Be aware of gimbal lock (en.wikipedia.org/wiki/Gimbal_lock)

  2. set V matrix Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); 3. you already have projection matrix (in your case mProjMatrix)

    1. multippy M * V * P to recieve final MVP matrix
Community
  • 1
  • 1
Ievgen
  • 4,261
  • 7
  • 75
  • 124