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

6
  • 1
    Hi. You should probably know that that tutorial is somewhat broken (see this question). 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. Commented Sep 28, 2012 at 19:46
  • 1
    Also I think one of the answers there contains some fixed code, you may want to try that. Commented Sep 28, 2012 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 stackoverflow.com/questions/5550620/… to understand better what you need to do Commented Sep 28, 2012 at 19:58
  • 1
    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) 4. multippy M * V * P to recieve final MVP matrix Commented Sep 29, 2012 at 15:23
  • 1
    I am facing the same problem. Could you post the solution, if you found it? Commented Nov 20, 2012 at 21:19

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.