Java tutorial
/* * Copyright 2016 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.projecttango.examples.java.augmentedreality; import com.google.atap.tangoservice.Tango; import com.google.atap.tangoservice.Tango.OnTangoUpdateListener; import com.google.atap.tangoservice.TangoCameraIntrinsics; import com.google.atap.tangoservice.TangoConfig; import com.google.atap.tangoservice.TangoCoordinateFramePair; import com.google.atap.tangoservice.TangoErrorException; import com.google.atap.tangoservice.TangoEvent; import com.google.atap.tangoservice.TangoInvalidException; import com.google.atap.tangoservice.TangoOutOfDateException; import com.google.atap.tangoservice.TangoPointCloudData; import com.google.atap.tangoservice.TangoPoseData; import com.google.atap.tangoservice.TangoXyzIjData; import android.Manifest; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.hardware.display.DisplayManager; import android.opengl.GLSurfaceView; import android.opengl.Matrix; import android.os.Bundle; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.util.Log; import android.view.Display; import android.widget.Toast; import org.rajawali3d.scene.ASceneFrameCallback; import org.rajawali3d.view.SurfaceView; import java.util.ArrayList; import java.util.concurrent.atomic.AtomicBoolean; import com.projecttango.tangosupport.TangoSupport; /** * This is a simple example that shows how to use the Tango APIs to create an augmented reality (AR) * application. It displays the Planet Earth floating in space one meter in front of the device, and * the Moon rotating around it. * <p/> * This example uses Rajawali for the OpenGL rendering. This includes the color camera image in the * background and a 3D sphere with a texture of the Earth floating in space three meters forward. * This part is implemented in the {@code AugmentedRealityRenderer} class, like a regular Rajawali * application. * <p/> * This example focuses on how to use the Tango APIs to get the color camera data into an OpenGL * texture efficiently and have the OpenGL camera track the movement of the device in order to * achieve an augmented reality effect. * <p/> * Note that it is important to include the KEY_BOOLEAN_LOWLATENCYIMUINTEGRATION configuration * parameter in order to achieve best results synchronizing the Rajawali virtual world with the * RGB camera. * <p/> * If you're looking for a more stripped down example that doesn't use a rendering library like * Rajawali, see java_hello_video_example. */ public class AugmentedRealityActivity extends Activity { private static final String TAG = AugmentedRealityActivity.class.getSimpleName(); private static final int INVALID_TEXTURE_ID = 0; private static final String CAMERA_PERMISSION = Manifest.permission.CAMERA; private static final int CAMERA_PERMISSION_CODE = 0; private SurfaceView mSurfaceView; private AugmentedRealityRenderer mRenderer; private Tango mTango; private TangoConfig mConfig; private boolean mIsConnected = false; private double mCameraPoseTimestamp = 0; // Texture rendering related fields. // NOTE: Naming indicates which thread is in charge of updating this variable. private int mConnectedTextureIdGlThread = INVALID_TEXTURE_ID; private AtomicBoolean mIsFrameAvailableTangoThread = new AtomicBoolean(false); private double mRgbTimestampGlThread; private int mDisplayRotation = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview); mRenderer = new AugmentedRealityRenderer(this); DisplayManager displayManager = (DisplayManager) getSystemService(DISPLAY_SERVICE); if (displayManager != null) { displayManager.registerDisplayListener(new DisplayManager.DisplayListener() { @Override public void onDisplayAdded(int displayId) { } @Override public void onDisplayChanged(int displayId) { synchronized (this) { setDisplayRotation(); } } @Override public void onDisplayRemoved(int displayId) { } }, null); } setupRenderer(); } @Override protected void onStart() { super.onStart(); // Set render mode to RENDERMODE_CONTINUOUSLY to force getting onDraw callbacks until // the Tango service is properly set up and we start getting onFrameAvailable callbacks. mSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); // Check and request camera permission at run time. if (checkAndRequestPermissions()) { bindTangoService(); } } @Override public void onStop() { super.onStop(); // Synchronize against disconnecting while the service is being used in the OpenGL thread or // in the UI thread. // NOTE: DO NOT lock against this same object in the Tango callback thread. Tango.disconnect // will block here until all Tango callback calls are finished. If you lock against this // object in a Tango callback thread it will cause a deadlock. synchronized (this) { try { mIsConnected = false; mTango.disconnectCamera(TangoCameraIntrinsics.TANGO_CAMERA_COLOR); // We need to invalidate the connected texture ID so that we cause a // re-connection in the OpenGL thread after resume. mConnectedTextureIdGlThread = INVALID_TEXTURE_ID; mTango.disconnect(); mTango = null; } catch (TangoErrorException e) { Log.e(TAG, getString(R.string.exception_tango_error), e); } } } /** * Initialize Tango Service as a normal Android Service. */ private void bindTangoService() { // Since we call mTango.disconnect() in onStop, this will unbind Tango Service, so every // time onStart gets called we should create a new Tango object. mTango = new Tango(AugmentedRealityActivity.this, new Runnable() { // Pass in a Runnable to be called from UI thread when Tango is ready. This Runnable // will be running on a new thread. // When Tango is ready, we can call Tango functions safely here only when there // are no UI thread changes involved. @Override public void run() { // Synchronize against disconnecting while the service is being used in the // OpenGL thread or in the UI thread. synchronized (AugmentedRealityActivity.this) { try { TangoSupport.initialize(); mConfig = setupTangoConfig(mTango); mTango.connect(mConfig); startupTango(); mIsConnected = true; setDisplayRotation(); } catch (TangoOutOfDateException e) { Log.e(TAG, getString(R.string.exception_out_of_date), e); showsToastAndFinishOnUiThread(R.string.exception_out_of_date); } catch (TangoErrorException e) { Log.e(TAG, getString(R.string.exception_tango_error), e); showsToastAndFinishOnUiThread(R.string.exception_tango_error); } catch (TangoInvalidException e) { Log.e(TAG, getString(R.string.exception_tango_invalid), e); showsToastAndFinishOnUiThread(R.string.exception_tango_invalid); } } } }); } /** * Sets up the Tango configuration object. Make sure mTango object is initialized before * making this call. */ private TangoConfig setupTangoConfig(Tango tango) { // Use default configuration for Tango Service, plus color camera, low latency // IMU integration and drift correction. TangoConfig config = tango.getConfig(TangoConfig.CONFIG_TYPE_DEFAULT); config.putBoolean(TangoConfig.KEY_BOOLEAN_COLORCAMERA, true); // NOTE: Low latency integration is necessary to achieve a precise alignment of // virtual objects with the RBG image and produce a good AR effect. config.putBoolean(TangoConfig.KEY_BOOLEAN_LOWLATENCYIMUINTEGRATION, true); // Drift correction allows motion tracking to recover after it loses tracking. // The drift-corrected pose is available through the frame pair with // base frame AREA_DESCRIPTION and target frame DEVICE. config.putBoolean(TangoConfig.KEY_BOOLEAN_DRIFT_CORRECTION, true); return config; } /** * Set up the callback listeners for the Tango Service and obtain other parameters required * after Tango connection. * Listen to updates from the RGB camera. */ private void startupTango() { // No need to add any coordinate frame pairs since we aren't using pose data from callbacks. ArrayList<TangoCoordinateFramePair> framePairs = new ArrayList<TangoCoordinateFramePair>(); mTango.connectListener(framePairs, new OnTangoUpdateListener() { @Override public void onPoseAvailable(TangoPoseData pose) { // We are not using onPoseAvailable for this app. } @Override public void onXyzIjAvailable(TangoXyzIjData xyzIj) { // We are not using onXyzIjAvailable for this app. } @Override public void onPointCloudAvailable(TangoPointCloudData pointCloud) { // We are not using onPointCloudAvailable for this app. } @Override public void onTangoEvent(TangoEvent event) { // We are not using onTangoEvent for this app. } @Override public void onFrameAvailable(int cameraId) { // Check if the frame available is for the camera we want and update its frame // on the view. if (cameraId == TangoCameraIntrinsics.TANGO_CAMERA_COLOR) { // Now that we are receiving onFrameAvailable callbacks, we can switch // to RENDERMODE_WHEN_DIRTY to drive the render loop from this callback. // This will result in a frame rate of approximately 30FPS, in synchrony with // the RGB camera driver. // If you need to render at a higher rate (i.e., if you want to render complex // animations smoothly) you can use RENDERMODE_CONTINUOUSLY throughout the // application lifecycle. if (mSurfaceView.getRenderMode() != GLSurfaceView.RENDERMODE_WHEN_DIRTY) { mSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); } // Mark a camera frame as available for rendering in the OpenGL thread. mIsFrameAvailableTangoThread.set(true); // Trigger a Rajawali render to update the scene with the new RGB data. mSurfaceView.requestRender(); } } }); } /** * Connects the view and renderer to the color camara and callbacks. */ private void setupRenderer() { // Register a Rajawali Scene Frame Callback to update the scene camera pose whenever a new // RGB frame is rendered. // (@see https://github.com/Rajawali/Rajawali/wiki/Scene-Frame-Callbacks) mRenderer.getCurrentScene().registerFrameCallback(new ASceneFrameCallback() { @Override public void onPreFrame(long sceneTime, double deltaTime) { // NOTE: This is called from the OpenGL render thread after all the renderer // onRender callbacks have a chance to run and before scene objects are rendered // into the scene. // Prevent concurrent access to {@code mIsFrameAvailableTangoThread} from the Tango // callback thread and service disconnection from an onPause event. try { synchronized (AugmentedRealityActivity.this) { // Don't execute tango API actions if we're not connected to the service. if (!mIsConnected) { return; } // Set up scene camera projection to match RGB camera intrinsics. if (!mRenderer.isSceneCameraConfigured()) { TangoCameraIntrinsics intrinsics = TangoSupport .getCameraIntrinsicsBasedOnDisplayRotation( TangoCameraIntrinsics.TANGO_CAMERA_COLOR, mDisplayRotation); mRenderer.setProjectionMatrix(projectionMatrixFromCameraIntrinsics(intrinsics)); } // Connect the camera texture to the OpenGL Texture if necessary // NOTE: When the OpenGL context is recycled, Rajawali may regenerate the // texture with a different ID. 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 // with it. if (mIsFrameAvailableTangoThread.compareAndSet(true, false)) { mRgbTimestampGlThread = mTango.updateTexture(TangoCameraIntrinsics.TANGO_CAMERA_COLOR); } // If a new RGB frame has been rendered, update the camera pose to match. if (mRgbTimestampGlThread > mCameraPoseTimestamp) { // Calculate the camera color pose at the camera frame update time in // OpenGL engine. // // When drift correction mode is enabled in config file, we must 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 available. TangoPoseData lastFramePose = TangoSupport.getPoseAtTime(mRgbTimestampGlThread, TangoPoseData.COORDINATE_FRAME_AREA_DESCRIPTION, TangoPoseData.COORDINATE_FRAME_CAMERA_COLOR, TangoSupport.TANGO_SUPPORT_ENGINE_OPENGL, TangoSupport.TANGO_SUPPORT_ENGINE_OPENGL, mDisplayRotation); if (lastFramePose.statusCode == TangoPoseData.POSE_VALID) { // Update the camera pose from the renderer mRenderer.updateRenderCameraPose(lastFramePose); mCameraPoseTimestamp = lastFramePose.timestamp; } else { // When the pose status is not valid, it indicates the tracking has // been lost. In this case, we simply stop rendering. // // This is also the place to display UI to suggest the user walk // to recover tracking. Log.w(TAG, "Can't get device pose 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); } } @Override public void onPreDraw(long sceneTime, double deltaTime) { } @Override public void onPostFrame(long sceneTime, double deltaTime) { } @Override public boolean callPreFrame() { return true; } }); mSurfaceView.setSurfaceRenderer(mRenderer); } /** * Use Tango camera intrinsics to calculate the projection matrix for the Rajawali scene. * * @param intrinsics camera instrinsics for computing the project matrix. */ private static float[] projectionMatrixFromCameraIntrinsics(TangoCameraIntrinsics intrinsics) { float cx = (float) intrinsics.cx; float cy = (float) intrinsics.cy; float width = (float) intrinsics.width; float height = (float) intrinsics.height; float fx = (float) intrinsics.fx; float fy = (float) intrinsics.fy; // Uses frustumM to create a projection matrix taking into account calibrated camera // intrinsic parameter. // Reference: http://ksimek.github.io/2013/06/03/calibrated_cameras_in_opengl/ float near = 0.1f; float far = 100; float xScale = near / fx; float yScale = near / fy; float xOffset = (cx - (width / 2.0f)) * xScale; // Color camera's coordinates has y pointing downwards so we negate this term. float yOffset = -(cy - (height / 2.0f)) * yScale; float m[] = new float[16]; Matrix.frustumM(m, 0, xScale * (float) -width / 2.0f - xOffset, xScale * (float) width / 2.0f - xOffset, yScale * (float) -height / 2.0f - yOffset, yScale * (float) height / 2.0f - yOffset, near, far); return m; } /** * Set the color camera background texture rotation and save the camera to display rotation. */ private void setDisplayRotation() { Display display = getWindowManager().getDefaultDisplay(); mDisplayRotation = display.getRotation(); // We also need to update the camera texture UV coordinates. This must be run in the OpenGL // thread. mSurfaceView.queueEvent(new Runnable() { @Override public void run() { if (mIsConnected) { mRenderer.updateColorCameraTextureUvGlThread(mDisplayRotation); } } }); } /** * Check to see we have the necessary permissions for this app, and ask for them if we don't. * * @return True if we have the necessary permissions, false if we haven't. */ private boolean checkAndRequestPermissions() { if (!hasCameraPermission()) { requestCameraPermission(); return false; } return true; } /** * Check to see we have the necessary permissions for this app. */ private boolean hasCameraPermission() { return ContextCompat.checkSelfPermission(this, CAMERA_PERMISSION) == PackageManager.PERMISSION_GRANTED; } /** * Request the necessary permissions for this app. */ private void requestCameraPermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(this, CAMERA_PERMISSION)) { showRequestPermissionRationale(); } else { ActivityCompat.requestPermissions(this, new String[] { CAMERA_PERMISSION }, CAMERA_PERMISSION_CODE); } } /** * If the user has declined the permission before, we have to explain that the app needs this * permission. */ private void showRequestPermissionRationale() { final AlertDialog dialog = new AlertDialog.Builder(this) .setMessage("Java Augmented Reality Example requires camera permission") .setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { ActivityCompat.requestPermissions(AugmentedRealityActivity.this, new String[] { CAMERA_PERMISSION }, CAMERA_PERMISSION_CODE); } }).create(); dialog.show(); } /** * Result for requesting camera permission. */ @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (hasCameraPermission()) { bindTangoService(); } else { Toast.makeText(this, "Java Augmented Reality Example requires camera permission", Toast.LENGTH_LONG) .show(); } } /** * Display toast on UI thread. * * @param resId The resource id of the string resource to use. Can be formatted text. */ private void showsToastAndFinishOnUiThread(final int resId) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(AugmentedRealityActivity.this, getString(resId), Toast.LENGTH_LONG).show(); finish(); } }); } }