model rotation can now be controlled by the device's sensors
This commit is contained in:
@@ -3,7 +3,8 @@
|
||||
package="com.android.modelviewer">
|
||||
<application android:label="ModelViewer">
|
||||
<activity android:name="SimpleModel"
|
||||
android:label="SimpleModel">
|
||||
android:label="SimpleModel"
|
||||
android:screenOrientation="nosensor">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
|
||||
@@ -22,4 +22,6 @@
|
||||
android:title="@string/load_model" />
|
||||
<item android:id="@+id/display_options"
|
||||
android:title="@string/display_options" />
|
||||
<item android:id="@+id/sensor"
|
||||
android:title="@string/sensor" />
|
||||
</menu>
|
||||
|
||||
@@ -21,4 +21,5 @@
|
||||
<skip />
|
||||
<string name="load_model">Load Model</string>
|
||||
<string name="display_options">Display Options</string>
|
||||
<string name="sensor">Toggle Sensor</string>
|
||||
</resources>
|
||||
|
||||
@@ -85,6 +85,9 @@ public class SimpleModel extends Activity {
|
||||
return true;
|
||||
case R.id.display_options:
|
||||
return true;
|
||||
case R.id.sensor:
|
||||
mView.toggleSensor();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@@ -70,6 +70,10 @@ public class SimpleModelRS {
|
||||
mScript.invoke_onActionMove(x, y);
|
||||
}
|
||||
|
||||
public void onPostureChanged(Matrix4f posture) {
|
||||
mScript.set_gPostureMatrix(posture);
|
||||
}
|
||||
|
||||
private void initPFS() {
|
||||
ProgramStore.Builder b = new ProgramStore.Builder(mRS);
|
||||
|
||||
|
||||
@@ -16,29 +16,46 @@
|
||||
|
||||
package com.android.modelviewer;
|
||||
|
||||
import android.renderscript.Matrix4f;
|
||||
import android.renderscript.RSSurfaceView;
|
||||
import android.renderscript.RenderScriptGL;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.ScaleGestureDetector;
|
||||
import android.util.Log;
|
||||
|
||||
public class SimpleModelView extends RSSurfaceView {
|
||||
public class SimpleModelView extends RSSurfaceView implements SensorEventListener {
|
||||
|
||||
private RenderScriptGL mRS;
|
||||
private SimpleModelRS mRender;
|
||||
|
||||
private ScaleGestureDetector mScaleDetector;
|
||||
|
||||
private SensorManager mSensorManager;
|
||||
private Sensor mRotationVectorSensor;
|
||||
private final float[] mRotationMatrix = new float[16];
|
||||
|
||||
private static final int INVALID_POINTER_ID = -1;
|
||||
private int mActivePointerId = INVALID_POINTER_ID;
|
||||
private boolean mUseSensor = false;
|
||||
private Matrix4f mIdentityMatrix = new Matrix4f();
|
||||
|
||||
public SimpleModelView(Context context) {
|
||||
super(context);
|
||||
ensureRenderScript();
|
||||
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
|
||||
// Get an instance of the SensorManager
|
||||
mSensorManager = (SensorManager)getContext().getSystemService(Context.SENSOR_SERVICE);
|
||||
// find the rotation-vector sensor
|
||||
mRotationVectorSensor = mSensorManager.getDefaultSensor(
|
||||
Sensor.TYPE_ROTATION_VECTOR);
|
||||
mIdentityMatrix.loadIdentity();
|
||||
}
|
||||
|
||||
private void ensureRenderScript() {
|
||||
@@ -51,6 +68,16 @@ public class SimpleModelView extends RSSurfaceView {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resume() {
|
||||
mSensorManager.registerListener(this, mRotationVectorSensor, 10000);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pause() {
|
||||
mSensorManager.unregisterListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onAttachedToWindow() {
|
||||
super.onAttachedToWindow();
|
||||
@@ -139,6 +166,32 @@ public class SimpleModelView extends RSSurfaceView {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
// we received a sensor event. it is a good practice to check
|
||||
// that we received the proper event
|
||||
if (mUseSensor) {
|
||||
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
|
||||
// convert the rotation-vector to a 4x4 matrix. the matrix
|
||||
// is interpreted by Open GL as the inverse of the
|
||||
// rotation-vector, which is what we want.
|
||||
SensorManager.getRotationMatrixFromVector(
|
||||
mRotationMatrix , event.values);
|
||||
|
||||
if (mRender != null) {
|
||||
mRender.onPostureChanged(new Matrix4f(mRotationMatrix));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
}
|
||||
|
||||
public void toggleSensor() {
|
||||
mUseSensor = !mUseSensor;
|
||||
if (mUseSensor == false) {
|
||||
mRender.onPostureChanged(mIdentityMatrix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -28,6 +28,8 @@ rs_program_store gPFSBackground;
|
||||
rs_font gItalic;
|
||||
rs_allocation gTextAlloc;
|
||||
|
||||
rs_matrix4x4 gPostureMatrix;
|
||||
|
||||
typedef struct MeshInfo {
|
||||
rs_mesh mMesh;
|
||||
int mNumIndexSets;
|
||||
@@ -89,6 +91,7 @@ void init() {
|
||||
gRotateY = 0.0f;
|
||||
gZoom = 50.0f;
|
||||
gLookAt = 0.0f;
|
||||
rsMatrixLoadIdentity(&gPostureMatrix);
|
||||
}
|
||||
|
||||
void updateMeshInfo() {
|
||||
@@ -149,8 +152,10 @@ int root(void) {
|
||||
rsMatrixLoadIdentity(&matrix);
|
||||
// Position our models on the screen
|
||||
rsMatrixTranslate(&matrix, gLookAt.x, gLookAt.y, gLookAt.z - gZoom);
|
||||
rsMatrixMultiply(&matrix, &gPostureMatrix);
|
||||
rsMatrixRotate(&matrix, gRotateX, 1.0f, 0.0f, 0.0f);
|
||||
rsMatrixRotate(&matrix, gRotateY, 0.0f, 1.0f, 0.0f);
|
||||
|
||||
rsgProgramVertexLoadModelMatrix(&matrix);
|
||||
|
||||
renderAllMeshes();
|
||||
|
||||
Reference in New Issue
Block a user