Advertisement
Guest User

Untitled

a guest
Aug 7th, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.69 KB | None | 0 0
  1. package cz.ackee.ar.vuforia;
  2.  
  3. import android.content.Context;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.Color;
  6. import android.graphics.PixelFormat;
  7. import android.graphics.drawable.ColorDrawable;
  8. import android.hardware.Sensor;
  9. import android.hardware.SensorEvent;
  10. import android.hardware.SensorEventListener;
  11. import android.hardware.SensorManager;
  12. import android.opengl.GLSurfaceView;
  13. import android.os.Bundle;
  14. import android.support.v7.app.ActionBarActivity;
  15. import android.util.Log;
  16. import android.view.Gravity;
  17. import android.view.Surface;
  18. import android.view.ViewGroup;
  19. import android.widget.FrameLayout;
  20.  
  21. import com.threed.jpct.Camera;
  22. import com.threed.jpct.FrameBuffer;
  23. import com.threed.jpct.Light;
  24. import com.threed.jpct.Loader;
  25. import com.threed.jpct.Logger;
  26. import com.threed.jpct.Matrix;
  27. import com.threed.jpct.Object3D;
  28. import com.threed.jpct.RGBColor;
  29. import com.threed.jpct.SimpleVector;
  30. import com.threed.jpct.Texture;
  31. import com.threed.jpct.TextureManager;
  32. import com.threed.jpct.World;
  33. import com.threed.jpct.util.MemoryHelper;
  34.  
  35. import java.io.IOException;
  36. import java.lang.reflect.Field;
  37.  
  38. import javax.microedition.khronos.egl.EGLConfig;
  39. import javax.microedition.khronos.opengles.GL10;
  40.  
  41. /**
  42.  * A simple demo. This shows more how to use jPCT-AE than it shows how to write
  43.  * a proper application for Android. It includes basic activity management to
  44.  * handle pause and resume...
  45.  *
  46.  * @author EgonOlsen
  47.  */
  48. public class ExampleActivity extends ActionBarActivity implements SensorEventListener {
  49.  
  50.     // Used to handle pause and resume...
  51.     private static ExampleActivity master = null;
  52.  
  53.     private GLSurfaceView mGLView;
  54.     private FrameBuffer fb = null;
  55.     private World world = null;
  56.  
  57.     private Object3D model = null;
  58.     private int fps = 0;
  59.  
  60.     private Light sun = null;
  61.  
  62.     private SensorManager mSensorManager;
  63.  
  64.     private int mSensorSpeed = SensorManager.SENSOR_DELAY_FASTEST;
  65.     private float[] rotationMatrix = new float[9];
  66.  
  67.     private android.hardware.Camera mCamera;
  68.     private int cameraId;
  69.  
  70.     protected void onCreate(Bundle savedInstanceState) {
  71.  
  72.         Logger.log("onCreate");
  73.  
  74.         if (master != null) {
  75.             copy(master);
  76.         }
  77.  
  78.         super.onCreate(savedInstanceState);
  79.         setContentView(R.layout.activity_gyroscop);
  80.         getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.argb(10, 200, 200, 200)));
  81.  
  82.         mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  83.  
  84.         //init views
  85.         mGLView = (GLSurfaceView) findViewById(R.id.GLsurfaceView);
  86.         mGLView.setEGLContextClientVersion(2);
  87.         mGLView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
  88.         MyRenderer renderer = new MyRenderer();
  89.         mGLView.setRenderer(renderer);
  90.         mGLView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
  91.  
  92.         //init rotation matrix
  93.         rotationMatrix[0] = 1;
  94.         rotationMatrix[4] = 1;
  95.         rotationMatrix[8] = 1;
  96.     }
  97.  
  98.     @Override
  99.     protected void onPause() {
  100.         super.onPause();
  101.         mGLView.onPause();
  102.         mSensorManager.unregisterListener(this);
  103.         releaseCamera();
  104.     }
  105.  
  106.     @Override
  107.     protected void onStop() {
  108.         super.onStop();
  109.     }
  110.  
  111.     @Override
  112.     protected void onResume() {
  113.         super.onResume();
  114.         mGLView.onResume();
  115.         //register listeners
  116.  
  117.         Sensor rotationVector = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
  118.         mSensorManager.registerListener(this, rotationVector, mSensorSpeed);
  119.         //init camera preview
  120.         initCameraPreview();
  121.     }
  122.  
  123.     private void releaseCamera() {
  124.         if (mCamera != null) {
  125.             mCamera.lock();
  126.             mCamera.release();        // release the camera for other applications
  127.             mCamera = null;
  128.         }
  129.     }
  130.  
  131.     private void initCameraPreview() {
  132.         mCamera = getCameraInstance();
  133.         if (mCamera == null) {
  134.             return;
  135.         }
  136.         setCameraDisplayOrientation(mCamera);
  137.         CameraPreview preview1 = new CameraPreview(this, mCamera);
  138.         preview1.setId(12345);
  139.         FrameLayout preview = (FrameLayout) findViewById(R.id.surfaceView);
  140.         preview.removeAllViews();
  141.         FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  142.         params.gravity = Gravity.CENTER;
  143.         preview.addView(preview1, params);
  144.     }
  145.  
  146.  
  147.     private android.hardware.Camera getCameraInstance() {
  148.         int cameraCount = 0;
  149.         android.hardware.Camera cam = null;
  150.         android.hardware.Camera.CameraInfo cameraInfo = new android.hardware.Camera.CameraInfo();
  151.         cameraCount = android.hardware.Camera.getNumberOfCameras();
  152.         for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
  153.             android.hardware.Camera.getCameraInfo(camIdx, cameraInfo);
  154.             if (cameraInfo.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK) {
  155.                 try {
  156.                     cam = android.hardware.Camera.open(camIdx);
  157.                     cameraId = camIdx;
  158.                 } catch (RuntimeException e) {
  159.                     Log.e("Camera", "Camera failed to open: " + e.getLocalizedMessage());
  160.                 }
  161.             }
  162.         }
  163.         if (cam != null) { //if no front facing camera was found return some default picked by system
  164.             return cam;
  165.         } else {
  166.             return android.hardware.Camera.open();
  167.         }
  168.     }
  169.  
  170.     public void setCameraDisplayOrientation(android.hardware.Camera camera) {
  171.  
  172.         android.hardware.Camera.CameraInfo info =
  173.                 new android.hardware.Camera.CameraInfo();
  174.  
  175.         android.hardware.Camera.getCameraInfo(cameraId, info);
  176.  
  177.         int rotation = getWindowManager().getDefaultDisplay().getRotation();
  178.         int degrees = 0;
  179.  
  180.         switch (rotation) {
  181.             case Surface.ROTATION_0:
  182.                 degrees = 0;
  183.                 break;
  184.             case Surface.ROTATION_90:
  185.                 degrees = 90;
  186.                 break;
  187.             case Surface.ROTATION_180:
  188.                 degrees = 180;
  189.                 break;
  190.             case Surface.ROTATION_270:
  191.                 degrees = 270;
  192.                 break;
  193.         }
  194.  
  195.         int result;
  196.         if (info.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT) {
  197.             result = (info.orientation + degrees) % 360;
  198.             result = (360 - result) % 360;  // compensate the mirror
  199.         } else {  // back-facing
  200.             result = (info.orientation - degrees + 360) % 360;
  201.         }
  202.         camera.setDisplayOrientation(result);
  203.     }
  204.  
  205.     private void copy(Object src) {
  206.         try {
  207.             Logger.log("Copying data from master Activity!");
  208.             Field[] fs = src.getClass().getDeclaredFields();
  209.             for (Field f : fs) {
  210.                 f.setAccessible(true);
  211.                 f.set(this, f.get(src));
  212.             }
  213.         } catch (Exception e) {
  214.             throw new RuntimeException(e);
  215.         }
  216.     }
  217.  
  218.     @Override
  219.     public void onSensorChanged(SensorEvent event) {
  220.  
  221.         if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
  222.             float[] values = event.values.clone();
  223.             SensorManager.getRotationMatrixFromVector(rotationMatrix, values);
  224.         }
  225.  
  226.     }
  227.  
  228.  
  229.     @Override
  230.     public void onAccuracyChanged(Sensor sensor, int accuracy) {
  231.  
  232.     }
  233.  
  234.     class MyRenderer implements GLSurfaceView.Renderer {
  235.  
  236.         private final String TAG = MyRenderer.class.getName();
  237.         private long time = System.currentTimeMillis();
  238.         Camera cam;
  239.  
  240.  
  241.         public MyRenderer() {
  242.         }
  243.  
  244.         public void onSurfaceChanged(GL10 gl, int w, int h) {
  245.             if (fb != null) {
  246.                 fb.dispose();
  247.             }
  248.             fb = new FrameBuffer(w, h);
  249.  
  250.             if (master == null) {
  251.  
  252.                 world = new World();
  253.                 world.setAmbientLight(20, 20, 20);
  254.  
  255.                 sun = new Light(world);
  256.                 sun.setIntensity(250, 250, 250);
  257.  
  258.                 // Create a texture out of the icon...:-)
  259.                 Texture texture = null;
  260.                 try {
  261.                     texture = new Texture(BitmapFactory.decodeStream(getAssets().open("texture.bmp")));
  262.                 } catch (IOException e) {
  263.                     e.printStackTrace();
  264.                 }
  265.                 TextureManager.getInstance().addTexture("tower", texture);
  266.  
  267.                 try {
  268.                     model = Loader.loadOBJ(getAssets().open("tower.obj"), getAssets().open("tower.mtl"), 0.2f)[0];
  269.                 } catch (IOException e) {
  270.                     e.printStackTrace();
  271.                 }
  272.  
  273.                 model.build();
  274.                 world.addObject(model);
  275.  
  276.  
  277.                 cam = world.getCamera();
  278.                 cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
  279.                 cam.lookAt(model.getTransformedCenter());
  280.  
  281.                 SimpleVector sv = new SimpleVector();
  282.                 sv.set(model.getTransformedCenter());
  283.                 sv.y -= 100;
  284.                 sv.z -= 100;
  285.                 sun.setPosition(sv);
  286.                 MemoryHelper.compact();
  287.  
  288.                 if (master == null) {
  289.                     Logger.log("Saving master Activity!");
  290.                     master = ExampleActivity.this;
  291.                 }
  292.             }
  293.         }
  294.  
  295.         public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  296.         }
  297.  
  298.         public void onDrawFrame(GL10 gl) {
  299.             float[] result = new float[rotationMatrix.length];
  300.             //remap coordinate system - main problem, dont know how to make it properly
  301.             SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, result);
  302.  
  303.             Matrix r = getRotationMatrix(result);
  304.             model.setRotationMatrix(r);
  305.  
  306.             fb.clear(new RGBColor());
  307.             world.renderScene(fb);
  308.             world.draw(fb);
  309.             fb.display();
  310.  
  311.             if (System.currentTimeMillis() - time >= 1000) {
  312.                 Logger.log(fps + "fps");
  313.                 fps = 0;
  314.                 time = System.currentTimeMillis();
  315.                 Log.d(TAG, "rotation matrix " + r);
  316.             }
  317.             fps++;
  318.         }
  319.  
  320.         private Matrix getRotationMatrix(float[] data) {
  321.             Matrix ret = new Matrix();
  322.  
  323.             for (int i = 0; i < 3; i++) {
  324.                 for (int j = 0; j < 3; j++) {
  325.                     ret.set(i, j, data[i * 3 + j]);
  326.                 }
  327.             }
  328.             ret.setRow(3, 0, 0, 0, 1);
  329.             ret.setColumn(3, 0, 0, 0, 1);
  330.             return ret;
  331.         }
  332.     }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement