Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.ackee.ar.vuforia;
- import android.content.Context;
- import android.graphics.BitmapFactory;
- import android.graphics.Color;
- import android.graphics.PixelFormat;
- import android.graphics.drawable.ColorDrawable;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.opengl.GLSurfaceView;
- import android.os.Bundle;
- import android.support.v7.app.ActionBarActivity;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.Surface;
- import android.view.ViewGroup;
- import android.widget.FrameLayout;
- import com.threed.jpct.Camera;
- import com.threed.jpct.FrameBuffer;
- import com.threed.jpct.Light;
- import com.threed.jpct.Loader;
- import com.threed.jpct.Logger;
- import com.threed.jpct.Matrix;
- import com.threed.jpct.Object3D;
- import com.threed.jpct.RGBColor;
- import com.threed.jpct.SimpleVector;
- import com.threed.jpct.Texture;
- import com.threed.jpct.TextureManager;
- import com.threed.jpct.World;
- import com.threed.jpct.util.MemoryHelper;
- import java.io.IOException;
- import java.lang.reflect.Field;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- /**
- * A simple demo. This shows more how to use jPCT-AE than it shows how to write
- * a proper application for Android. It includes basic activity management to
- * handle pause and resume...
- *
- * @author EgonOlsen
- */
- public class ExampleActivity extends ActionBarActivity implements SensorEventListener {
- // Used to handle pause and resume...
- private static ExampleActivity master = null;
- private GLSurfaceView mGLView;
- private FrameBuffer fb = null;
- private World world = null;
- private Object3D model = null;
- private int fps = 0;
- private Light sun = null;
- private SensorManager mSensorManager;
- private int mSensorSpeed = SensorManager.SENSOR_DELAY_FASTEST;
- private float[] rotationMatrix = new float[9];
- private android.hardware.Camera mCamera;
- private int cameraId;
- protected void onCreate(Bundle savedInstanceState) {
- Logger.log("onCreate");
- if (master != null) {
- copy(master);
- }
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_gyroscop);
- getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.argb(10, 200, 200, 200)));
- mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
- //init views
- mGLView = (GLSurfaceView) findViewById(R.id.GLsurfaceView);
- mGLView.setEGLContextClientVersion(2);
- mGLView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
- MyRenderer renderer = new MyRenderer();
- mGLView.setRenderer(renderer);
- mGLView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
- //init rotation matrix
- rotationMatrix[0] = 1;
- rotationMatrix[4] = 1;
- rotationMatrix[8] = 1;
- }
- @Override
- protected void onPause() {
- super.onPause();
- mGLView.onPause();
- mSensorManager.unregisterListener(this);
- releaseCamera();
- }
- @Override
- protected void onStop() {
- super.onStop();
- }
- @Override
- protected void onResume() {
- super.onResume();
- mGLView.onResume();
- //register listeners
- Sensor rotationVector = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
- mSensorManager.registerListener(this, rotationVector, mSensorSpeed);
- //init camera preview
- initCameraPreview();
- }
- private void releaseCamera() {
- if (mCamera != null) {
- mCamera.lock();
- mCamera.release(); // release the camera for other applications
- mCamera = null;
- }
- }
- private void initCameraPreview() {
- mCamera = getCameraInstance();
- if (mCamera == null) {
- return;
- }
- setCameraDisplayOrientation(mCamera);
- CameraPreview preview1 = new CameraPreview(this, mCamera);
- preview1.setId(12345);
- FrameLayout preview = (FrameLayout) findViewById(R.id.surfaceView);
- preview.removeAllViews();
- FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
- params.gravity = Gravity.CENTER;
- preview.addView(preview1, params);
- }
- private android.hardware.Camera getCameraInstance() {
- int cameraCount = 0;
- android.hardware.Camera cam = null;
- android.hardware.Camera.CameraInfo cameraInfo = new android.hardware.Camera.CameraInfo();
- cameraCount = android.hardware.Camera.getNumberOfCameras();
- for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
- android.hardware.Camera.getCameraInfo(camIdx, cameraInfo);
- if (cameraInfo.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK) {
- try {
- cam = android.hardware.Camera.open(camIdx);
- cameraId = camIdx;
- } catch (RuntimeException e) {
- Log.e("Camera", "Camera failed to open: " + e.getLocalizedMessage());
- }
- }
- }
- if (cam != null) { //if no front facing camera was found return some default picked by system
- return cam;
- } else {
- return android.hardware.Camera.open();
- }
- }
- public void setCameraDisplayOrientation(android.hardware.Camera camera) {
- android.hardware.Camera.CameraInfo info =
- new android.hardware.Camera.CameraInfo();
- android.hardware.Camera.getCameraInfo(cameraId, info);
- int rotation = getWindowManager().getDefaultDisplay().getRotation();
- int degrees = 0;
- switch (rotation) {
- case Surface.ROTATION_0:
- degrees = 0;
- break;
- case Surface.ROTATION_90:
- degrees = 90;
- break;
- case Surface.ROTATION_180:
- degrees = 180;
- break;
- case Surface.ROTATION_270:
- degrees = 270;
- break;
- }
- int result;
- if (info.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT) {
- result = (info.orientation + degrees) % 360;
- result = (360 - result) % 360; // compensate the mirror
- } else { // back-facing
- result = (info.orientation - degrees + 360) % 360;
- }
- camera.setDisplayOrientation(result);
- }
- private void copy(Object src) {
- try {
- Logger.log("Copying data from master Activity!");
- Field[] fs = src.getClass().getDeclaredFields();
- for (Field f : fs) {
- f.setAccessible(true);
- f.set(this, f.get(src));
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- @Override
- public void onSensorChanged(SensorEvent event) {
- if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
- float[] values = event.values.clone();
- SensorManager.getRotationMatrixFromVector(rotationMatrix, values);
- }
- }
- @Override
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- }
- class MyRenderer implements GLSurfaceView.Renderer {
- private final String TAG = MyRenderer.class.getName();
- private long time = System.currentTimeMillis();
- Camera cam;
- public MyRenderer() {
- }
- public void onSurfaceChanged(GL10 gl, int w, int h) {
- if (fb != null) {
- fb.dispose();
- }
- fb = new FrameBuffer(w, h);
- if (master == null) {
- world = new World();
- world.setAmbientLight(20, 20, 20);
- sun = new Light(world);
- sun.setIntensity(250, 250, 250);
- // Create a texture out of the icon...:-)
- Texture texture = null;
- try {
- texture = new Texture(BitmapFactory.decodeStream(getAssets().open("texture.bmp")));
- } catch (IOException e) {
- e.printStackTrace();
- }
- TextureManager.getInstance().addTexture("tower", texture);
- try {
- model = Loader.loadOBJ(getAssets().open("tower.obj"), getAssets().open("tower.mtl"), 0.2f)[0];
- } catch (IOException e) {
- e.printStackTrace();
- }
- model.build();
- world.addObject(model);
- cam = world.getCamera();
- cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
- cam.lookAt(model.getTransformedCenter());
- SimpleVector sv = new SimpleVector();
- sv.set(model.getTransformedCenter());
- sv.y -= 100;
- sv.z -= 100;
- sun.setPosition(sv);
- MemoryHelper.compact();
- if (master == null) {
- Logger.log("Saving master Activity!");
- master = ExampleActivity.this;
- }
- }
- }
- public void onSurfaceCreated(GL10 gl, EGLConfig config) {
- }
- public void onDrawFrame(GL10 gl) {
- float[] result = new float[rotationMatrix.length];
- //remap coordinate system - main problem, dont know how to make it properly
- SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, result);
- Matrix r = getRotationMatrix(result);
- model.setRotationMatrix(r);
- fb.clear(new RGBColor());
- world.renderScene(fb);
- world.draw(fb);
- fb.display();
- if (System.currentTimeMillis() - time >= 1000) {
- Logger.log(fps + "fps");
- fps = 0;
- time = System.currentTimeMillis();
- Log.d(TAG, "rotation matrix " + r);
- }
- fps++;
- }
- private Matrix getRotationMatrix(float[] data) {
- Matrix ret = new Matrix();
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- ret.set(i, j, data[i * 3 + j]);
- }
- }
- ret.setRow(3, 0, 0, 0, 1);
- ret.setColumn(3, 0, 0, 0, 1);
- return ret;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement