Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 6.53 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. orientation and android openGl with sensor not working the way i expected
  2. package com.eliddell.AR;
  3.  
  4.     import android.content.Context;
  5.     import android.hardware.Sensor;
  6.     import android.hardware.SensorEvent;
  7.     import android.hardware.SensorEventListener;
  8.     import android.hardware.SensorManager;
  9.     import android.widget.Toast;
  10.  
  11.     /**
  12.      * Uses the sensor API to determine the phones orientation.
  13.      * Registering for events from the accelerator and the magnetometer (compass)
  14.      * a rotation matrix is computed. This matrix can be used to rotate an
  15.      * OpenGL scene.
  16.      */
  17.     public class PhoneOrientation {
  18.         private SensorManager sensorMan;
  19.         private Sensor sensorAcce;
  20.         private Sensor sensorMagn;
  21.         private SensorEventListener listener;
  22.         private float matrix[]=new float[16];
  23.         private Context ctx;
  24.  
  25. public PhoneOrientation(Context context) {
  26.     ctx = context;
  27. }
  28.  
  29. public void start(Context context) {
  30.             listener = new SensorEventListener() {
  31.         private float orientation[]=new float[3];
  32.         private float acceleration[]=new float[3];
  33.  
  34.         public void onAccuracyChanged(Sensor arg0, int arg1){}
  35.  
  36.         public void onSensorChanged(SensorEvent evt) {
  37.             int type=evt.sensor.getType();
  38.  
  39.             //Smoothing the sensor data a bit seems like a good idea.
  40.             if (type == Sensor.TYPE_MAGNETIC_FIELD) {
  41.                 orientation[0]=(orientation[0]*1+evt.values[0])*0.5f;
  42.                 orientation[1]=(orientation[1]*1+evt.values[1])*0.5f;
  43.                 orientation[2]=(orientation[2]*1+evt.values[2])*0.5f;
  44.             } else if (type == Sensor.TYPE_ACCELEROMETER) {
  45.                 acceleration[0]=(acceleration[0]*2+evt.values[0])*0.33334f;
  46.                 acceleration[1]=(acceleration[1]*2+evt.values[1])*0.33334f;
  47.                 acceleration[2]=(acceleration[2]*2+evt.values[2])*0.33334f;
  48.             }
  49.             if ((type==Sensor.TYPE_MAGNETIC_FIELD) || (type==Sensor.TYPE_ACCELEROMETER)) {
  50.                 float newMat[]=new float[16];
  51.                 //Toast toast = Toast.makeText(ctx.getApplicationContext(), "accel", Toast.LENGTH_SHORT);
  52.                 //toast.show();
  53.                 SensorManager.getRotationMatrix(newMat, null, acceleration, orientation);
  54.                 SensorManager.remapCoordinateSystem(newMat,
  55.                         SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X,
  56.                         newMat);
  57.                 matrix=newMat;
  58.             }
  59.         }
  60.     };
  61.  
  62.     sensorMan = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
  63.     sensorAcce = sensorMan.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
  64.     sensorMagn = sensorMan.getSensorList(Sensor.TYPE_MAGNETIC_FIELD).get(0);
  65.  
  66.     sensorMan.registerListener(listener, sensorAcce, SensorManager.SENSOR_DELAY_FASTEST);
  67.     sensorMan.registerListener(listener, sensorMagn, SensorManager.SENSOR_DELAY_FASTEST);      
  68. }
  69.  
  70. public float[] getMatrix() {
  71.     return matrix;
  72. }
  73.  
  74. public void finish() {
  75.     sensorMan.unregisterListener(listener);
  76. }
  77.     }
  78.        
  79. package com.eliddell.AR;
  80.  
  81.     import javax.microedition.khronos.egl.EGLConfig;
  82.     import javax.microedition.khronos.opengles.GL10;
  83.     import android.opengl.GLU;
  84.     import android.content.Context;
  85.     import android.graphics.PixelFormat;
  86.     import android.hardware.Camera;
  87.     import android.opengl.GLSurfaceView;
  88.     import android.opengl.GLSurfaceView.Renderer;
  89.     import android.view.SurfaceHolder;
  90.  
  91.     public class GLLayer extends GLSurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback, Renderer {
  92.  
  93. private Context context;
  94. private Square square;   // the triangle to be drawn
  95.  
  96. private PhoneOrientation phoneOri;
  97. private boolean randomSelection[][][]=new boolean[10][10][10];
  98.  
  99. public GLLayer(Context context) {
  100.     super(context);
  101.  
  102.     this.context = context;
  103.     this.square = new Square();
  104.     phoneOri=new PhoneOrientation(context); // sensor manager and interpreter
  105.  
  106.     // settings for translucent glView
  107.     this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
  108.     this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
  109.  
  110.     // set render to inline
  111.     this.setRenderer(this);
  112.     phoneOri.start(context);
  113.  
  114. }
  115.  
  116. @Override
  117. public void onDrawFrame(GL10 gl) {
  118.     gl.glEnable(GL10.GL_TEXTURE_2D);
  119.     // clear Screen and Depth Buffer
  120.     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  121.  
  122.  
  123.     // Reset the Modelview Matrix
  124.     gl.glLoadIdentity();
  125.  
  126.     //GLU.gluLookAt(gl, eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
  127.     //GLU.gluLookAt(gl,0, 0, 4.2f, 0, 0, 0, 0, 1, 0);
  128.     GLU.gluLookAt(gl, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  129.  
  130.     float floatMat[]=phoneOri.getMatrix();
  131.     gl.glMatrixMode(GL10.GL_MODELVIEW);
  132.     gl.glLoadMatrixf(floatMat, 0);
  133.  
  134.  
  135.  
  136.     // Drawing
  137.     //gl.glNormal3f(0,0,1);
  138.     gl.glTranslatef(0.0f, 0.0f, -5.0f);     // move 5 units INTO the screen
  139.  
  140.     square.draw(gl);                       // Draw the square
  141.  
  142.  
  143. }
  144.  
  145. @Override
  146. public void onSurfaceChanged(GL10 gl, int width, int height) {
  147.     if(height == 0) {                    //Prevent A Divide By Zero By
  148.     height = 1;                         //Making Height Equal One
  149.     }
  150.     float ratio = (float) width / height;
  151.     gl.glViewport(0, 0, width, height);     //Reset The Current Viewport
  152.     gl.glMatrixMode(GL10.GL_PROJECTION);    //Select The Projection Matrix
  153.     gl.glLoadIdentity();                    //Reset The Projection Matrix
  154.                                             //Calculate The Aspect Ratio Of The Window
  155.  
  156.     //gl.glFrustumf(-ratio, ratio, -1, 1, 1, 100);
  157.     GLU.gluPerspective(gl, 35.0f, (float)width / (float)height, 0.1f, 100.0f);
  158.     gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select The Modelview Matrix
  159.     gl.glLoadIdentity();                    //Reset The Modelview Matrix
  160.  
  161.     GLU.gluLookAt(gl, 0, 0, 0f, 0, 0, 0, 0, 0, 0);
  162.  
  163.  
  164. }
  165.  
  166. @Override
  167. public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
  168.     // Load the texture for the square
  169.     square.loadGLTexture(gl, this.context);
  170.  
  171.     gl.glEnable(GL10.GL_TEXTURE_2D);            //Enable Texture Mapping ( NEW )
  172.     gl.glShadeModel(GL10.GL_SMOOTH);            //Enable Smooth Shading
  173.     gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    //Black Background
  174.     gl.glClearDepthf(1.0f);                     //Depth Buffer Setup
  175.     gl.glEnable(GL10.GL_DEPTH_TEST);            //Enables Depth Testing
  176.     gl.glDepthFunc(GL10.GL_LEQUAL);             //The Type Of Depth Testing To Do
  177.  
  178.     //Really Nice Perspective Calculations
  179.     gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
  180.  
  181. }
  182.  
  183. @Override
  184. public void onPreviewFrame(byte[] data, Camera camera) {
  185.     // TODO Auto-generated method stub
  186.  
  187. }
  188.  
  189.     }