Example usage for android.opengl Matrix translateM

List of usage examples for android.opengl Matrix translateM

Introduction

In this page you can find the example usage for android.opengl Matrix translateM.

Prototype

public static void translateM(float[] m, int mOffset, float x, float y, float z) 

Source Link

Document

Translates matrix m by x, y, and z in place.

Usage

From source file:com.google.vrtoolkit.cardboard.samples.treasurehunt.MainActivity.java

private void updateMiniCubePosition(float pitch, float yaw, float roll) {
    float[] temp_modelMiniCube = new float[16];
    float[] temp_mRotate = new float[16];
    Matrix.setIdentityM(temp_modelMiniCube, 0);
    Matrix.translateM(temp_modelMiniCube, 0, -handPos[0] / (float) 50.0, -handPos[2] / (float) 50.0,
            -handPos[1] / (float) 50.0);
    Matrix.setIdentityM(temp_mRotate, 0);
    Matrix.rotateM(temp_mRotate, 0, 45, 1, 0, 0); //This rotates the cube
    //quaternionToMatrix(temp_mRotate, x, y, z, w);

    Matrix.multiplyMM(modelMiniCube, 0, temp_mRotate, 0, temp_modelMiniCube, 0);
}

From source file:com.projecttango.examples.java.openglar.OpenGlAugmentedRealityActivity.java

/**
 * Here is where you would set up your rendering logic. We're replacing it with a minimalistic,
 * dummy example, using a standard GLSurfaceView and a basic renderer, for illustration purposes
 * only.//  www .  j a  v a 2  s .c  o  m
 */
private void setupRenderer() {
    mSurfaceView.setEGLContextClientVersion(2);
    mRenderer = new OpenGlAugmentedRealityRenderer(this, new OpenGlAugmentedRealityRenderer.RenderCallback() {
        private double lastRenderedTimeStamp;

        @Override
        public void preRender() {
            // This is the work that you would do on your main OpenGL render thread.

            try {
                // Synchronize against concurrently disconnecting the service triggered
                // from the UI thread.
                synchronized (OpenGlAugmentedRealityActivity.this) {
                    // We need to be careful not to run any Tango-dependent code in the
                    // OpenGL thread unless we know the Tango Service is properly
                    // set up and connected.
                    if (!mIsConnected) {
                        return;
                    }

                    // Set up scene camera projection to match RGB camera intrinsics.
                    if (!mRenderer.isProjectionMatrixConfigured()) {
                        TangoCameraIntrinsics intrinsics = TangoSupport
                                .getCameraIntrinsicsBasedOnDisplayRotation(
                                        TangoCameraIntrinsics.TANGO_CAMERA_COLOR, mDisplayRotation);
                        mRenderer.setProjectionMatrix(projectionMatrixFromCameraIntrinsics(intrinsics));
                    }
                    // Connect the Tango SDK to the OpenGL texture ID where we are
                    // going to render the camera.
                    // NOTE: This must be done after both the texture is generated
                    // and the Tango Service is connected.
                    if (mConnectedTextureIdGlThread != mRenderer.getTextureId()) {
                        mTango.connectTextureId(TangoCameraIntrinsics.TANGO_CAMERA_COLOR,
                                mRenderer.getTextureId());
                        mConnectedTextureIdGlThread = mRenderer.getTextureId();
                        Log.d(TAG, "connected to texture id: " + mRenderer.getTextureId());
                    }
                    // If there is a new RGB camera frame available, update the texture
                    // and scene camera pose.
                    if (mIsFrameAvailableTangoThread.compareAndSet(true, false)) {
                        // {@code mRgbTimestampGlThread} contains the exact timestamp at
                        // which the rendered RGB frame was acquired.
                        mRgbTimestampGlThread = mTango.updateTexture(TangoCameraIntrinsics.TANGO_CAMERA_COLOR);

                        // Get the transform from color camera to Start of Service
                        // at the timestamp of the RGB image in OpenGL coordinates.
                        //
                        // When drift correction mode is enabled in config file, we need
                        // to query the device with respect to Area Description pose in
                        // order to use the drift-corrected pose.
                        //
                        // Note that if you don't want to use the drift corrected pose,
                        // the normal device with respect to start of service pose is
                        // still available.
                        TangoSupport.TangoMatrixTransformData transform = TangoSupport.getMatrixTransformAtTime(
                                mRgbTimestampGlThread, TangoPoseData.COORDINATE_FRAME_AREA_DESCRIPTION,
                                TangoPoseData.COORDINATE_FRAME_CAMERA_COLOR,
                                TangoSupport.TANGO_SUPPORT_ENGINE_OPENGL,
                                TangoSupport.TANGO_SUPPORT_ENGINE_OPENGL, mDisplayRotation);
                        if (transform.statusCode == TangoPoseData.POSE_VALID) {

                            mRenderer.updateViewMatrix(transform.matrix);
                            double deltaTime = mRgbTimestampGlThread - lastRenderedTimeStamp;
                            lastRenderedTimeStamp = mRgbTimestampGlThread;

                            // Set the earth rotation around itself.
                            float[] openGlTEarth = new float[16];
                            Matrix.rotateM(mEarthMoonCenterTEarth, 0, (float) deltaTime * 360 / 10, 0, 1, 0);
                            Matrix.multiplyMM(openGlTEarth, 0, mOpenGLTEarthMoonCenter, 0,
                                    mEarthMoonCenterTEarth, 0);

                            // Set moon rotation around the earth and moon center.
                            float[] openGlTMoon = new float[16];
                            Matrix.rotateM(mEarthMoonCenterTMoonRotation, 0, (float) deltaTime * 360 / 50, 0, 1,
                                    0);
                            float[] mEarthTMoon = new float[16];
                            Matrix.multiplyMM(mEarthTMoon, 0, mEarthMoonCenterTMoonRotation, 0,
                                    mEarthMoonCenterTTranslation, 0);
                            Matrix.multiplyMM(openGlTMoon, 0, mOpenGLTEarthMoonCenter, 0, mEarthTMoon, 0);

                            mRenderer.setEarthTransform(openGlTEarth);
                            mRenderer.setMoonTransform(openGlTMoon);
                        } else {
                            // When the pose status is not valid, it indicates tracking
                            // has been lost. In this case, we simply stop rendering.
                            //
                            // This is also the place to display UI to suggest that the
                            // user walk to recover tracking.
                            Log.w(TAG, "Could not get a valid transform at time " + mRgbTimestampGlThread);
                        }
                    }
                }
                // Avoid crashing the application due to unhandled exceptions.
            } catch (TangoErrorException e) {
                Log.e(TAG, "Tango API call error within the OpenGL render thread", e);
            } catch (Throwable t) {
                Log.e(TAG, "Exception on the OpenGL thread", t);
            }
        }
    });

    // Set the starting position and orientation of the Earth and Moon with respect to the
    // OpenGL frame.
    Matrix.setIdentityM(mOpenGLTEarthMoonCenter, 0);
    Matrix.translateM(mOpenGLTEarthMoonCenter, 0, 0, 0, -1f);
    Matrix.setIdentityM(mEarthMoonCenterTEarth, 0);
    Matrix.setIdentityM(mEarthMoonCenterTMoonRotation, 0);
    Matrix.setIdentityM(mEarthMoonCenterTTranslation, 0);
    Matrix.translateM(mEarthMoonCenterTTranslation, 0, 0.5f, 0, 0);

    mSurfaceView.setRenderer(mRenderer);
}

From source file:com.projecttango.examples.java.occlusion.OcclusionActivity.java

/**
 * Replace the earth 30 cm above the clicked point.
 *//*  ww w . java 2  s .  c o m*/
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
        // Calculate click location in u,v (0;1) coordinates.
        float u = motionEvent.getX() / view.getWidth();
        float v = motionEvent.getY() / view.getHeight();

        try {
            // Fit a plane on the clicked point using the latest point cloud data.
            // Synchronize against concurrent access to the RGB timestamp in the OpenGL thread
            // and a possible service disconnection due to an onPause event.
            float[] planeFitTransform;
            synchronized (this) {
                planeFitTransform = doFitPlane(u, v, mRgbTimestampGlThread);
            }

            if (planeFitTransform != null) {
                // Place the earth 30 cm above the plane.
                Matrix.translateM(planeFitTransform, 0, 0, 0, 0.3f);
                mRenderer.updateEarthTransform(planeFitTransform);
            }

        } catch (TangoException t) {
            Toast.makeText(getApplicationContext(), R.string.failed_measurement, Toast.LENGTH_SHORT).show();
            Log.e(TAG, getString(R.string.failed_measurement), t);
        } catch (SecurityException t) {
            Toast.makeText(getApplicationContext(), R.string.failed_permissions, Toast.LENGTH_SHORT).show();
            Log.e(TAG, getString(R.string.failed_permissions), t);
        }
    }
    return true;
}

From source file:com.projecttango.examples.java.modelcorrespondence.ModelCorrespondenceActivity.java

/**
 * Calculate the transform needed to place the model in the upper left corner of the camera,
 * and rotate it to show the next point to make the correspondence.
 *//*from   w  ww.j  av  a  2 s. c o  m*/
private float[] calculateModelTransformFixedToCam(int mDisplayRotation) {
    // Translate to the upper left corner and ahead of the cam if the device is in landscape
    // mode or to the upper center if it is in portrait mode.
    float[] rgbTHouse = new float[16];
    Matrix.setIdentityM(rgbTHouse, 0);
    if (mDisplayRotation == Surface.ROTATION_0 || mDisplayRotation == Surface.ROTATION_180) {
        Matrix.translateM(rgbTHouse, 0, 0f, 1.2f, -4);
    } else {
        Matrix.translateM(rgbTHouse, 0, -1.5f, 0.3f, -4);
    }

    // Rotate it 180 degrees around the Z axis to show the front of the house as default
    // orientation.
    Matrix.rotateM(rgbTHouse, 0, 180, 0, 0, 1);
    // Rotate it around the X axis so it looks better as seen from above.
    Matrix.rotateM(rgbTHouse, 0, 70, 1, 0, 0);
    // Rotate it around the Z axis to show the next correspondence point to be added.
    Matrix.rotateM(rgbTHouse, 0, -mModelZRotation, 0, 0, 1);
    // Scale it to a proper size.
    Matrix.scaleM(rgbTHouse, 0, 0.03f, 0.03f, 0.03f);
    return rgbTHouse;
}

From source file:com.tumblr.cardboard.Tumblr3DActivity.java

/**
 * Moves the texture to the middle and makes it big.
 *
 * @param photoIndex the index of the texture to move
 *//*from   w w  w  . j ava2 s.c om*/
private void selectPhoto(int photoIndex) {
    final int i = NUM_IMAGES_STATIC + photoIndex;
    Matrix.scaleM(mModelRect[i], 0, mImageRect[i], 0, mScaleTheater, mScaleTheater, 1f);
    Matrix.translateM(mModelRect[i], 0, 0f, 0f, -SPHERE_RADIUS);
}

From source file:com.tumblr.cardboard.Tumblr3DActivity.java

private static void placePhoto(float[][] modelRects, float[][] imageRects, int texIndex, float scale,
        float azimuth, float inclination, float yTranslate) {
    float[] azimuthMatrix = new float[16];
    Matrix.setRotateM(azimuthMatrix, 0, azimuth, 0, 1, 0);

    float[] inclinationMatrix = new float[16];
    Matrix.setRotateM(inclinationMatrix, 0, inclination, 1, 0, 0);

    float[] rotationMatrix = new float[16];
    Matrix.multiplyMM(rotationMatrix, 0, azimuthMatrix, 0, inclinationMatrix, 0);

    Matrix.multiplyMM(modelRects[texIndex], 0, imageRects[texIndex], 0, rotationMatrix, 0);
    Matrix.translateM(modelRects[texIndex], 0, 0f, 0f, yTranslate);
    Matrix.scaleM(modelRects[texIndex], 0, scale, scale, 1f);
}