1
\$\begingroup\$

I'm building an augmented reality application where I want objects on the screen to move with respect to the orientation of the phone. For example, if the phone is held still - the object stays at the center of the screen. If the phone is rotated left about its vertical axis in portrait mode, the object moves to the right. This is the sensor I'm accessing in Android

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
        SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
        mGLAssetSurfaceView.getRenderer().setRotationMatrix(mRotationMatrix);
    }
}

This is how I'm applying it to my OpenGL renderer:

@Override
public void onDrawFrame(GL10 gl) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    Matrix.multiplyMM(mViewMatrix, 0, mViewMatrix, 0, mRotationMatrix, 0);

    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);

    // Draw shape
    mSquare.draw(mMVPMatrix);
}

As you can see, I'm computing the product

MVRP

and passing that into my vertex shader. The result of this is the square in the middle of the screen rotating about its own axis, not with respect to the phone.

\$\endgroup\$
  • \$\begingroup\$ Try MRVP. Operations are applied in the order in which the matrices are concatenated. Though, that information should really already be included in your view matrix. \$\endgroup\$ – 3Dave Jun 2 '17 at 22:36
  • \$\begingroup\$ That seemed to do the trick thanks! Feel free to put it as an answer to this question and I can accept \$\endgroup\$ – Carpetfizz Jun 2 '17 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.