- orientation and android openGl with sensor not working the way i expected
- package com.eliddell.AR;
- import android.content.Context;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.widget.Toast;
- /**
- * Uses the sensor API to determine the phones orientation.
- * Registering for events from the accelerator and the magnetometer (compass)
- * a rotation matrix is computed. This matrix can be used to rotate an
- * OpenGL scene.
- */
- public class PhoneOrientation {
- private SensorManager sensorMan;
- private Sensor sensorAcce;
- private Sensor sensorMagn;
- private SensorEventListener listener;
- private float matrix[]=new float[16];
- private Context ctx;
- public PhoneOrientation(Context context) {
- ctx = context;
- }
- public void start(Context context) {
- listener = new SensorEventListener() {
- private float orientation[]=new float[3];
- private float acceleration[]=new float[3];
- public void onAccuracyChanged(Sensor arg0, int arg1){}
- public void onSensorChanged(SensorEvent evt) {
- int type=evt.sensor.getType();
- //Smoothing the sensor data a bit seems like a good idea.
- if (type == Sensor.TYPE_MAGNETIC_FIELD) {
- orientation[0]=(orientation[0]*1+evt.values[0])*0.5f;
- orientation[1]=(orientation[1]*1+evt.values[1])*0.5f;
- orientation[2]=(orientation[2]*1+evt.values[2])*0.5f;
- } else if (type == Sensor.TYPE_ACCELEROMETER) {
- acceleration[0]=(acceleration[0]*2+evt.values[0])*0.33334f;
- acceleration[1]=(acceleration[1]*2+evt.values[1])*0.33334f;
- acceleration[2]=(acceleration[2]*2+evt.values[2])*0.33334f;
- }
- if ((type==Sensor.TYPE_MAGNETIC_FIELD) || (type==Sensor.TYPE_ACCELEROMETER)) {
- float newMat[]=new float[16];
- //Toast toast = Toast.makeText(ctx.getApplicationContext(), "accel", Toast.LENGTH_SHORT);
- //toast.show();
- SensorManager.getRotationMatrix(newMat, null, acceleration, orientation);
- SensorManager.remapCoordinateSystem(newMat,
- SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X,
- newMat);
- matrix=newMat;
- }
- }
- };
- sensorMan = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
- sensorAcce = sensorMan.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
- sensorMagn = sensorMan.getSensorList(Sensor.TYPE_MAGNETIC_FIELD).get(0);
- sensorMan.registerListener(listener, sensorAcce, SensorManager.SENSOR_DELAY_FASTEST);
- sensorMan.registerListener(listener, sensorMagn, SensorManager.SENSOR_DELAY_FASTEST);
- }
- public float[] getMatrix() {
- return matrix;
- }
- public void finish() {
- sensorMan.unregisterListener(listener);
- }
- }
- package com.eliddell.AR;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import android.opengl.GLU;
- import android.content.Context;
- import android.graphics.PixelFormat;
- import android.hardware.Camera;
- import android.opengl.GLSurfaceView;
- import android.opengl.GLSurfaceView.Renderer;
- import android.view.SurfaceHolder;
- public class GLLayer extends GLSurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback, Renderer {
- private Context context;
- private Square square; // the triangle to be drawn
- private PhoneOrientation phoneOri;
- private boolean randomSelection[][][]=new boolean[10][10][10];
- public GLLayer(Context context) {
- super(context);
- this.context = context;
- this.square = new Square();
- phoneOri=new PhoneOrientation(context); // sensor manager and interpreter
- // settings for translucent glView
- this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
- this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
- // set render to inline
- this.setRenderer(this);
- phoneOri.start(context);
- }
- @Override
- public void onDrawFrame(GL10 gl) {
- gl.glEnable(GL10.GL_TEXTURE_2D);
- // clear Screen and Depth Buffer
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
- // Reset the Modelview Matrix
- gl.glLoadIdentity();
- //GLU.gluLookAt(gl, eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
- //GLU.gluLookAt(gl,0, 0, 4.2f, 0, 0, 0, 0, 1, 0);
- GLU.gluLookAt(gl, 0, 0, 0, 0, 0, 0, 0, 0, 0);
- float floatMat[]=phoneOri.getMatrix();
- gl.glMatrixMode(GL10.GL_MODELVIEW);
- gl.glLoadMatrixf(floatMat, 0);
- // Drawing
- //gl.glNormal3f(0,0,1);
- gl.glTranslatef(0.0f, 0.0f, -5.0f); // move 5 units INTO the screen
- square.draw(gl); // Draw the square
- }
- @Override
- public void onSurfaceChanged(GL10 gl, int width, int height) {
- if(height == 0) { //Prevent A Divide By Zero By
- height = 1; //Making Height Equal One
- }
- float ratio = (float) width / height;
- gl.glViewport(0, 0, width, height); //Reset The Current Viewport
- gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix
- gl.glLoadIdentity(); //Reset The Projection Matrix
- //Calculate The Aspect Ratio Of The Window
- //gl.glFrustumf(-ratio, ratio, -1, 1, 1, 100);
- GLU.gluPerspective(gl, 35.0f, (float)width / (float)height, 0.1f, 100.0f);
- gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix
- gl.glLoadIdentity(); //Reset The Modelview Matrix
- GLU.gluLookAt(gl, 0, 0, 0f, 0, 0, 0, 0, 0, 0);
- }
- @Override
- public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
- // Load the texture for the square
- square.loadGLTexture(gl, this.context);
- gl.glEnable(GL10.GL_TEXTURE_2D); //Enable Texture Mapping ( NEW )
- gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading
- gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Black Background
- gl.glClearDepthf(1.0f); //Depth Buffer Setup
- gl.glEnable(GL10.GL_DEPTH_TEST); //Enables Depth Testing
- gl.glDepthFunc(GL10.GL_LEQUAL); //The Type Of Depth Testing To Do
- //Really Nice Perspective Calculations
- gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
- }
- @Override
- public void onPreviewFrame(byte[] data, Camera camera) {
- // TODO Auto-generated method stub
- }
- }