diff --git a/AreaLearningJava/libs/tango_java_lib.jar b/AreaLearningJava/libs/tango_java_lib.jar index 497d5073..b4766b67 100644 Binary files a/AreaLearningJava/libs/tango_java_lib.jar and b/AreaLearningJava/libs/tango_java_lib.jar differ diff --git a/AreaLearningJava/src/com/projecttango/experiments/javaarealearning/ALRenderer.java b/AreaLearningJava/src/com/projecttango/experiments/javaarealearning/ALRenderer.java index b5ed969c..e3923ade 100644 --- a/AreaLearningJava/src/com/projecttango/experiments/javaarealearning/ALRenderer.java +++ b/AreaLearningJava/src/com/projecttango/experiments/javaarealearning/ALRenderer.java @@ -45,7 +45,7 @@ public class ALRenderer extends Renderer implements GLSurfaceView.Renderer { private CameraFrustum mCameraFrustum; private CameraFrustumAndAxis mCameraFrustumAndAxis; private Grid mFloorGrid; - + private boolean mIsValid = false; @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // Set background color and enable depth testing @@ -63,6 +63,7 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) { Matrix.setIdentityM(mViewMatrix, 0); Matrix.setLookAtM(mViewMatrix, 0, 5f, 5f, 5f, 0f, 0f, 0f, 0f, 1f, 0f); mCameraFrustumAndAxis.setModelMatrix(getModelMatCalculator().getModelMatrix()); + mIsValid = true; } @Override @@ -99,5 +100,9 @@ public Trajectory getBlueTrajectory() { public Trajectory getGreenTrajectory() { return mGreenTrajectory; } + + public boolean isValid(){ + return mIsValid; + } } diff --git a/AreaLearningJava/src/com/projecttango/experiments/javaarealearning/AreaLearningActivity.java b/AreaLearningJava/src/com/projecttango/experiments/javaarealearning/AreaLearningActivity.java index 89774870..7872efa5 100644 --- a/AreaLearningJava/src/com/projecttango/experiments/javaarealearning/AreaLearningActivity.java +++ b/AreaLearningJava/src/com/projecttango/experiments/javaarealearning/AreaLearningActivity.java @@ -274,7 +274,7 @@ public void onPoseAvailable(TangoPoseData pose) { mAdf2DevicePoseDelta = (pose.timestamp - mAdf2DevicePreviousPoseTimeStamp) * SECONDS_TO_MILLI; mAdf2DevicePreviousPoseTimeStamp = pose.timestamp; - if (mIsRelocalized) { + if (mIsRelocalized && mRenderer.isValid()) { updateRenderer = true; mRenderer.getGreenTrajectory().updateTrajectory(translation); } @@ -292,7 +292,7 @@ public void onPoseAvailable(TangoPoseData pose) { mStart2DevicePoseDelta = (pose.timestamp - mStart2DevicePreviousPoseTimeStamp) * SECONDS_TO_MILLI; mStart2DevicePreviousPoseTimeStamp = pose.timestamp; - if (!mIsRelocalized) { + if (!mIsRelocalized && mRenderer.isValid()) { updateRenderer = true; synchronized (mRenderer.getBlueTrajectory()) { @@ -324,7 +324,7 @@ public void onPoseAvailable(TangoPoseData pose) { // Update the trajectory, model matrix, and view matrix, then // render the scene again - if (updateRenderer) { + if (updateRenderer && mRenderer.isValid()) { mRenderer.getModelMatCalculator().updateModelMatrix(translation, pose.getRotationAsFloats()); mRenderer.updateViewMatrix(); diff --git a/MotionTrackingJava/libs/tango_java_lib.jar b/MotionTrackingJava/libs/tango_java_lib.jar index 497d5073..b4766b67 100644 Binary files a/MotionTrackingJava/libs/tango_java_lib.jar and b/MotionTrackingJava/libs/tango_java_lib.jar differ diff --git a/MotionTrackingJava/src/com/projecttango/experiments/javamotiontracking/MTGLRenderer.java b/MotionTrackingJava/src/com/projecttango/experiments/javamotiontracking/MTGLRenderer.java index 735a18ad..078a86c3 100644 --- a/MotionTrackingJava/src/com/projecttango/experiments/javamotiontracking/MTGLRenderer.java +++ b/MotionTrackingJava/src/com/projecttango/experiments/javamotiontracking/MTGLRenderer.java @@ -44,7 +44,7 @@ public class MTGLRenderer extends Renderer implements GLSurfaceView.Renderer { private CameraFrustum mCameraFrustum; private CameraFrustumAndAxis mCameraFrustumAndAxis; private Grid mFloorGrid; - + private boolean mIsValid = false; @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { @@ -62,6 +62,7 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) { Matrix.setIdentityM(mViewMatrix, 0); Matrix.setLookAtM(mViewMatrix, 0, 5f, 5f, 5f, 0f, 0f, 0f, 0f, 1f, 0f); mCameraFrustumAndAxis.setModelMatrix(getModelMatCalculator().getModelMatrix()); + mIsValid = true; } @@ -94,5 +95,8 @@ public CameraFrustumAndAxis getCameraFrustumAndAxis() { public Trajectory getTrajectory() { return mTrajectory; } - + + public boolean isValid(){ + return mIsValid; + } } diff --git a/MotionTrackingJava/src/com/projecttango/experiments/javamotiontracking/MotionTrackingActivity.java b/MotionTrackingJava/src/com/projecttango/experiments/javamotiontracking/MotionTrackingActivity.java index 35e23ac9..ee7b9842 100644 --- a/MotionTrackingJava/src/com/projecttango/experiments/javamotiontracking/MotionTrackingActivity.java +++ b/MotionTrackingJava/src/com/projecttango/experiments/javamotiontracking/MotionTrackingActivity.java @@ -178,6 +178,9 @@ public void onPoseAvailable(final TangoPoseData pose) { // Update the OpenGL renderable objects with the new Tango Pose // data float[] translation = pose.getTranslationAsFloats(); + if(!mRenderer.isValid()){ + return; + } mRenderer.getTrajectory().updateTrajectory(translation); mRenderer.getModelMatCalculator().updateModelMatrix(translation, pose.getRotationAsFloats()); diff --git a/PointCloudJava/libs/tango_java_lib.jar b/PointCloudJava/libs/tango_java_lib.jar index 497d5073..b4766b67 100644 Binary files a/PointCloudJava/libs/tango_java_lib.jar and b/PointCloudJava/libs/tango_java_lib.jar differ diff --git a/PointCloudJava/src/com/projecttango/experiments/javapointcloud/PCRenderer.java b/PointCloudJava/src/com/projecttango/experiments/javapointcloud/PCRenderer.java index a639e7fc..2ffa1a4a 100644 --- a/PointCloudJava/src/com/projecttango/experiments/javapointcloud/PCRenderer.java +++ b/PointCloudJava/src/com/projecttango/experiments/javapointcloud/PCRenderer.java @@ -46,7 +46,7 @@ public class PCRenderer extends Renderer implements GLSurfaceView.Renderer { private Grid mGrid; private CameraFrustumAndAxis mCameraFrustumAndAxis; private int mMaxDepthPoints; - + private boolean mIsValid = false; public PCRenderer(int maxDepthPoints) { mMaxDepthPoints = maxDepthPoints; } @@ -61,6 +61,7 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) { Matrix.setIdentityM(mViewMatrix, 0); Matrix.setLookAtM(mViewMatrix, 0, 5f, 5f, 5f, 0f, 0f, 0f, 0f, 1f, 0f); mCameraFrustumAndAxis.setModelMatrix(getModelMatCalculator().getModelMatrix()); + mIsValid = true; } @Override @@ -86,4 +87,8 @@ public void onDrawFrame(GL10 gl) { public PointCloud getPointCloud() { return mPointCloud; } + + public boolean isValid(){ + return mIsValid; + } } diff --git a/PointCloudJava/src/com/projecttango/experiments/javapointcloud/PointCloudActivity.java b/PointCloudJava/src/com/projecttango/experiments/javapointcloud/PointCloudActivity.java index cb703c4c..86d391af 100644 --- a/PointCloudJava/src/com/projecttango/experiments/javapointcloud/PointCloudActivity.java +++ b/PointCloudJava/src/com/projecttango/experiments/javapointcloud/PointCloudActivity.java @@ -116,7 +116,6 @@ protected void onCreate(Bundle savedInstanceState) { mThirdPersonButton.setOnClickListener(this); mTopDownButton = (Button) findViewById(R.id.top_down_button); mTopDownButton.setOnClickListener(this); - mTango = new Tango(this); mConfig = new TangoConfig(); mConfig = mTango.getConfig(TangoConfig.CONFIG_TYPE_CURRENT); @@ -279,6 +278,9 @@ public void onPoseAvailable(final TangoPoseData pose) { } count++; mPreviousPoseStatus = pose.statusCode; + if(!mRenderer.isValid()){ + return; + } mRenderer.getModelMatCalculator().updateModelMatrix( pose.getTranslationAsFloats(), pose.getRotationAsFloats()); mRenderer.updateViewMatrix(); @@ -295,28 +297,20 @@ public void onXyzIjAvailable(final TangoXyzIjData xyzIj) { mPointCloudFrameDelta = (mCurrentTimeStamp - mXyIjPreviousTimeStamp) * SECS_TO_MILLISECS; mXyIjPreviousTimeStamp = mCurrentTimeStamp; - mPointCount = xyzIj.xyzCount; - byte[] buffer = new byte[xyzIj.xyzCount * 3 * 4]; - // TODO: Use getXYZBuffer() call instead of parcel file directly. - FileInputStream fileStream = new FileInputStream(xyzIj.xyzParcelFileDescriptor - .getFileDescriptor()); - try { - fileStream.read(buffer, xyzIj.xyzParcelFileDescriptorOffset, buffer.length); - fileStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } try { TangoPoseData pointCloudPose = mTango.getPoseAtTime(mCurrentTimeStamp, - framePairs.get(0)); - - mRenderer.getPointCloud().UpdatePoints(buffer, xyzIj.xyzCount); + framePairs.get(0)); + mPointCount = xyzIj.xyzCount; + if(!mRenderer.isValid()){ + return; + } + mRenderer.getPointCloud().UpdatePoints(xyzIj.xyz); mRenderer.getModelMatCalculator().updatePointCloudModelMatrix( - pointCloudPose.getTranslationAsFloats(), - pointCloudPose.getRotationAsFloats()); + pointCloudPose.getTranslationAsFloats(), + pointCloudPose.getRotationAsFloats()); mRenderer.getPointCloud().setModelMatrix( mRenderer.getModelMatCalculator().getPointCloudModelMatrixCopy()); - } catch (TangoErrorException e) { + } catch (TangoErrorException e) { Toast.makeText(getApplicationContext(), R.string.TangoError, Toast.LENGTH_SHORT).show(); } catch (TangoInvalidException e) { diff --git a/QuickstartJava/AndroidManifest.xml b/QuickstartJava/AndroidManifest.xml index 1468697b..151d8573 100644 --- a/QuickstartJava/AndroidManifest.xml +++ b/QuickstartJava/AndroidManifest.xml @@ -31,7 +31,7 @@ android:label="@string/app_name" android:theme="@style/AppTheme" > diff --git a/QuickstartJava/libs/tango_java_lib.jar b/QuickstartJava/libs/tango_java_lib.jar index 497d5073..b4766b67 100644 Binary files a/QuickstartJava/libs/tango_java_lib.jar and b/QuickstartJava/libs/tango_java_lib.jar differ diff --git a/QuickstartJava/src/com/projecttango/quickstartjava/MainActivity.java b/QuickstartJava/src/com/projecttango/experiments/quickstartjava/MainActivity.java similarity index 95% rename from QuickstartJava/src/com/projecttango/quickstartjava/MainActivity.java rename to QuickstartJava/src/com/projecttango/experiments/quickstartjava/MainActivity.java index f4efedf7..a6c27765 100644 --- a/QuickstartJava/src/com/projecttango/quickstartjava/MainActivity.java +++ b/QuickstartJava/src/com/projecttango/experiments/quickstartjava/MainActivity.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.projecttango.quickstartjava; +package com.projecttango.experiments.quickstartjava; import java.util.ArrayList; @@ -27,6 +27,7 @@ import com.google.atap.tangoservice.TangoOutOfDateException; import com.google.atap.tangoservice.TangoPoseData; import com.google.atap.tangoservice.TangoXyzIjData; +import com.projecttango.quickstartjava.R; import android.annotation.SuppressLint; import android.app.Activity; @@ -36,6 +37,7 @@ import android.widget.TextView; import android.widget.Toast; + /** * Main Activity for the Tango Java Quickstart. Demonstrates establishing a * connection to the {@link Tango} service and printing the {@link TangoPose} @@ -53,7 +55,7 @@ public class MainActivity extends Activity { private Tango mTango; private TangoConfig mConfig; private boolean mIsTangoServiceConnected; - + private boolean mIsProcessing = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -153,6 +155,12 @@ private void setTangoListeners() { @SuppressLint("DefaultLocale") @Override public void onPoseAvailable(TangoPoseData pose) { + if (mIsProcessing) { + Log.i(TAG, "Processing UI"); + return; + } + mIsProcessing = true; + // Format Translation and Rotation data final String translationMsg = String.format(sTranslationFormat, pose.translation[0], pose.translation[1], @@ -175,6 +183,7 @@ public void onPoseAvailable(TangoPoseData pose) { public void run() { mTranslationTextView.setText(translationMsg); mRotationTextView.setText(rotationMsg); + mIsProcessing = false; } }); } diff --git a/TangoUtils/src/com/projecttango/tangoutils/Renderer.java b/TangoUtils/src/com/projecttango/tangoutils/Renderer.java index e1d292e3..dd52d19e 100644 --- a/TangoUtils/src/com/projecttango/tangoutils/Renderer.java +++ b/TangoUtils/src/com/projecttango/tangoutils/Renderer.java @@ -1,3 +1,18 @@ +/* + * Copyright 2014 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.tangoutils; import android.opengl.Matrix; diff --git a/TangoUtils/src/com/projecttango/tangoutils/renderables/PointCloud.java b/TangoUtils/src/com/projecttango/tangoutils/renderables/PointCloud.java index 5965dc4e..908feb54 100644 --- a/TangoUtils/src/com/projecttango/tangoutils/renderables/PointCloud.java +++ b/TangoUtils/src/com/projecttango/tangoutils/renderables/PointCloud.java @@ -19,6 +19,7 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; +import java.util.concurrent.atomic.AtomicBoolean; import android.opengl.GLES20; import android.opengl.Matrix; @@ -43,7 +44,11 @@ public class PointCloud extends Renderable { private static final int BYTES_PER_FLOAT = 4; private static final int POINT_TO_XYZ = 3; - private FloatBuffer mVertexBuffer; + + int mVertexVBO; // VertexBufferObject. + private AtomicBoolean mUpdateVBO = new AtomicBoolean(); + private volatile FloatBuffer mPointCloudBuffer; + private final int mProgram; private int mPosHandle; private int mMVPMatrixHandle; @@ -61,42 +66,54 @@ public PointCloud(int maxDepthPoints) { GLES20.glAttachShader(mProgram, fragShader); GLES20.glLinkProgram(mProgram); Matrix.setIdentityM(getModelMatrix(), 0); - mVertexBuffer = ByteBuffer - .allocateDirect(maxDepthPoints * BYTES_PER_FLOAT * POINT_TO_XYZ) - .order(ByteOrder.nativeOrder()).asFloatBuffer(); + + final int buffers[] = new int[1]; + GLES20.glGenBuffers(1, buffers, 0); + mVertexVBO = buffers[0]; } - public synchronized void UpdatePoints(byte[] byteArray, int pointCount) { - FloatBuffer mPointCloudFloatBuffer; - mPointCloudFloatBuffer = ByteBuffer.wrap(byteArray) - .order(ByteOrder.nativeOrder()).asFloatBuffer(); - mPointCount = pointCount; - mVertexBuffer.clear(); - mVertexBuffer.position(0); - mVertexBuffer.put(mPointCloudFloatBuffer); - float totalZ = 0; - for (int i = 0; i < mPointCloudFloatBuffer.capacity() - 3; i = i + 3) { - totalZ = totalZ + mPointCloudFloatBuffer.get(i + 2); - } - mAverageZ = totalZ / mPointCount; + public synchronized void UpdatePoints(FloatBuffer pointCloudFloatBuffer) { + //save the reference in order to update this in the proper thread. + mPointCloudBuffer = pointCloudFloatBuffer; + + //signal the update + mUpdateVBO.set(true); } @Override public synchronized void draw(float[] viewMatrix, float[] projectionMatrix) { + GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mVertexVBO); + + if (mUpdateVBO.getAndSet(false)) { + if (mPointCloudBuffer != null) { + mPointCloudBuffer.position(0); + // Pass the info to the VBO + GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mPointCloudBuffer.capacity() + * BYTES_PER_FLOAT, mPointCloudBuffer, GLES20.GL_STATIC_DRAW); + mPointCount = mPointCloudBuffer.capacity() / 3; + float totalZ = 0; + for (int i = 0; i < mPointCloudBuffer.capacity() - 3; i = i + 3) { + totalZ = totalZ + mPointCloudBuffer.get(i + 2); + } + if (mPointCount != 0) + mAverageZ = totalZ / mPointCount; + // signal the update + mUpdateVBO.set(true); + } + mPointCloudBuffer = null; + } + if (mPointCount > 0) { - mVertexBuffer.position(0); + GLES20.glUseProgram(mProgram); updateMvpMatrix(viewMatrix, projectionMatrix); - mPosHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); - GLES20.glVertexAttribPointer(mPosHandle, COORDS_PER_VERTEX, - GLES20.GL_FLOAT, false, 0, mVertexBuffer); + GLES20.glVertexAttribPointer(mPosHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, + 0); GLES20.glEnableVertexAttribArray(mPosHandle); - mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, - "uMVPMatrix"); - GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, - getMvpMatrix(), 0); + GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, getMvpMatrix(), 0); GLES20.glDrawArrays(GLES20.GL_POINTS, 0, mPointCount); } + GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); } public float getAverageZ() { diff --git a/VideoOverlaySample/AndroidManifest.xml b/VideoOverlaySample/AndroidManifest.xml index 38d4bd0d..fd5a8b7a 100644 --- a/VideoOverlaySample/AndroidManifest.xml +++ b/VideoOverlaySample/AndroidManifest.xml @@ -15,7 +15,7 @@ android:label="@string/app_name" android:theme="@style/AppTheme" > diff --git a/VideoOverlaySample/libs/tango_java_lib.jar b/VideoOverlaySample/libs/tango_java_lib.jar index 497d5073..b4766b67 100644 Binary files a/VideoOverlaySample/libs/tango_java_lib.jar and b/VideoOverlaySample/libs/tango_java_lib.jar differ diff --git a/VideoOverlaySample/src/com/projecttango/videooverlaysample/MainActivity.java b/VideoOverlaySample/src/com/projecttango/experiments/videooverlaysample/MainActivity.java similarity index 73% rename from VideoOverlaySample/src/com/projecttango/videooverlaysample/MainActivity.java rename to VideoOverlaySample/src/com/projecttango/experiments/videooverlaysample/MainActivity.java index 4127e9f4..7dfd4a04 100644 --- a/VideoOverlaySample/src/com/projecttango/videooverlaysample/MainActivity.java +++ b/VideoOverlaySample/src/com/projecttango/experiments/videooverlaysample/MainActivity.java @@ -13,14 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.projecttango.videooverlaysample; +package com.projecttango.experiments.videooverlaysample; import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.os.Bundle; -import android.util.Log; import android.widget.Toast; import com.google.atap.tangoservice.Tango; @@ -33,6 +32,18 @@ import com.google.atap.tangoservice.TangoPoseData; import com.google.atap.tangoservice.TangoXyzIjData; +/** + * An example showing the usage of TangoCameraPreview class + * Usage of TangoCameraPreviewClass: + * To use this class, we first need initialize the TangoCameraPreview class with the activity's + * context and connect to the camera we want by using connectToTangoCamera class.Once the connection + * is established we need to manually update the TangoCameraPreview's texture by using the + * onFrameAvailable callbacks. + * Note: + * To use TangoCameraPreview class we need to ask the user permissions for MotionTracking + * at the minimum level. This is because in Java all the call backs such as + * onPoseAvailable,onXyzIjAvailable, onTangoEvents, onFrameAvailable are set together at once. + */ public class MainActivity extends Activity { private TangoCameraPreview tangoCameraPreview; private Tango mTango; @@ -46,7 +57,6 @@ protected void onCreate(Bundle savedInstanceState) { Tango.getRequestPermissionIntent(Tango.PERMISSIONTYPE_MOTION_TRACKING), Tango.TANGO_INTENT_ACTIVITYCODE); setContentView(tangoCameraPreview); - } @Override @@ -66,11 +76,15 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { // Camera Preview private void startCameraPreview() { + // Connect to color camera tangoCameraPreview.connectToTangoCamera(mTango, TangoCameraIntrinsics.TANGO_CAMERA_COLOR); + // Use default configuration for Tango Service. TangoConfig config = mTango.getConfig(TangoConfig.CONFIG_TYPE_DEFAULT); mTango.connect(config); + // No need to add any coordinate frame pairs since we are not using + // pose data. So just initialize. ArrayList framePairs = new ArrayList(); mTango.connectListener(framePairs, new OnTangoUpdateListener() { @Override @@ -80,7 +94,10 @@ public void onPoseAvailable(TangoPoseData pose) { @Override public void onFrameAvailable(int cameraId) { - if (cameraId == TangoCameraIntrinsics.TANGO_CAMERA_COLOR) {; + + // Check if the frame available is for the camera we want and + // update its frame on the camera preview. + if (cameraId == TangoCameraIntrinsics.TANGO_CAMERA_COLOR) { tangoCameraPreview.onFrameAvailable(); } }