152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project/*
252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project *
452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project * you may not use this file except in compliance with the License.
652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project * You may obtain a copy of the License at
752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project *
852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project *
1052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project * See the License for the specific language governing permissions and
1452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project * limitations under the License.
1552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project */
1652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
1752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectpackage com.example.android.compass;
1852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
1952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport java.nio.ByteBuffer;
2052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport java.nio.ByteOrder;
2152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport java.nio.FloatBuffer;
2252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
2352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport javax.microedition.khronos.egl.EGL10;
2452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport javax.microedition.khronos.egl.EGLConfig;
2552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport javax.microedition.khronos.opengles.GL10;
2652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport static javax.microedition.khronos.opengles.GL10.*;
2752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
2852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport android.app.Activity;
2952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport android.content.Context;
3052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport android.hardware.Sensor;
3152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport android.hardware.SensorEvent;
3252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport android.hardware.SensorEventListener;
3352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport android.hardware.SensorManager;
3452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport android.opengl.GLSurfaceView;
3552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport android.opengl.GLSurfaceView.Renderer;
3652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport android.os.Bundle;
3752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectimport android.util.Log;
3852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
3952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project/**
4052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project * This class provides a basic demonstration of how to use the
4152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project * {@link android.hardware.SensorManager SensorManager} API to draw
4252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project * a 3D compass.
4352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project */
4452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Projectpublic class CompassActivity extends Activity implements Renderer, SensorEventListener {
4552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    private GLSurfaceView mGLSurfaceView;
4652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    private SensorManager mSensorManager;
4752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    private float[] mGData = new float[3];
4852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    private float[] mMData = new float[3];
4952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    private float[] mR = new float[16];
5052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    private float[] mI = new float[16];
5152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    private FloatBuffer mVertexBuffer;
5252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    private FloatBuffer mColorBuffer;
5352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    private ByteBuffer mIndexBuffer;
5452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    private float[] mOrientation = new float[3];
5552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    private int mCount;
5652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
5752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    public CompassActivity() {
5852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    }
5952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
6052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    /** Called with the activity is first created. */
6152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    @Override
6252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    public void onCreate(Bundle savedInstanceState) {
6352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        super.onCreate(savedInstanceState);
6452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
6552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
6652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mGLSurfaceView = new GLSurfaceView(this);
6752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mGLSurfaceView.setRenderer(this);
6852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        setContentView(mGLSurfaceView);
6952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    }
7052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
7152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    @Override
7252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    protected void onResume() {
7352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        // Ideally a game should implement onResume() and onPause()
7452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        // to take appropriate action when the activity looses focus
7552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        super.onResume();
7652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mGLSurfaceView.onResume();
7752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
7852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
7952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mSensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_GAME);
8052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mSensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_GAME);
8152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    }
8252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
8352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    @Override
8452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    protected void onPause() {
8552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        // Ideally a game should implement onResume() and onPause()
8652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        // to take appropriate action when the activity looses focus
8752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        super.onPause();
8852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mGLSurfaceView.onPause();
8952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mSensorManager.unregisterListener(this);
9052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    }
9152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
9252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    public void onDrawFrame(GL10 gl) {
9352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        /*
9452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * Usually, the first thing one might want to do is to clear
9552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * the screen. The most efficient way of doing this is to use
9652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * glClear().
9752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         */
9852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
9952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
10052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
10152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        /*
10252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * Now we're ready to draw some 3D objects
10352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         */
10452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
10552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glMatrixMode(GL10.GL_MODELVIEW);
10652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glLoadIdentity();
10752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glTranslatef(0, 0, -2);
10852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
10952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        /*
11052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * All the magic happens here. The rotation matrix mR reported by
11152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * SensorManager.getRotationMatrix() is a 4x4 row-major matrix.
11252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * We need to use its inverse for rendering. The inverse is
11352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * simply calculated by taking the matrix' transpose. However, since
11452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * glMultMatrixf() expects a column-major matrix, we can use mR
11552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * directly!
11652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         */
11752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glMultMatrixf(mR, 0);
11852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        // some test code which will be used/cleaned up before we ship this.
11952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        //gl.glMultMatrixf(mI, 0);
12052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
12152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glVertexPointer(3, GL_FLOAT, 0, mVertexBuffer);
12252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glColorPointer(4, GL_FLOAT, 0, mColorBuffer);
12352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glDrawElements(GL_LINES, 6, GL_UNSIGNED_BYTE, mIndexBuffer);
12452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    }
12552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
12652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    public void onSurfaceChanged(GL10 gl, int width, int height) {
12752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glViewport(0, 0, width, height);
12852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
12952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        /*
13052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * Set our projection matrix. This doesn't have to be done
13152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * each time we draw, but usually a new projection needs to
13252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * be set when the viewport is resized.
13352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         */
13452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
13552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        float ratio = (float) width / height;
13652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glMatrixMode(GL10.GL_PROJECTION);
13752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glLoadIdentity();
13852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
13952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    }
14052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
14152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
14252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        /*
14352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * By default, OpenGL enables features that improve quality
14452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * but reduce performance. One might want to tweak that
14552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * especially on software renderer.
14652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         */
14752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glDisable(GL10.GL_DITHER);
14852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
14952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        /*
15052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * Some one-time OpenGL initialization can be made here
15152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * probably based on features of this particular context
15252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         */
15352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glClearColor(1,1,1,1);
15452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glEnable(GL10.GL_CULL_FACE);
15552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glShadeModel(GL10.GL_SMOOTH);
15652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glEnable(GL10.GL_DEPTH_TEST);
15752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
15852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        /*
15952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         * create / load the our 3D models here
16052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project         */
16152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
16252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
16352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
16452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
16552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        float vertices[] = {
16652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project                0,0,0,
16752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project                1,0,0,
16852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project                0,1,0,
16952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project                0,0,1
17052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        };
17152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        float colors[] = {
17252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project                0,0,0,0,
17352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project                1,0,0,1,
17452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project                0,1,0,1,
17552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project                0,0,1,1
17652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        };
17752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        byte indices[] = { 0, 1, 0, 2, 0, 3 };
17852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
17952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        // Buffers to be passed to gl*Pointer() functions
18052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        // must be direct, i.e., they must be placed on the
18152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        // native heap where the garbage collector cannot
18252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        // move them.
18352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        //
18452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        // Buffers with multi-byte datatypes (e.g., short, int, float)
18552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        // must have their byte order set to native order
18652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
18752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        ByteBuffer vbb;
18852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        vbb = ByteBuffer.allocateDirect(vertices.length*4);
18952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        vbb.order(ByteOrder.nativeOrder());
19052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mVertexBuffer = vbb.asFloatBuffer();
19152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mVertexBuffer.put(vertices);
19252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mVertexBuffer.position(0);
19352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
19452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        vbb = ByteBuffer.allocateDirect(colors.length*4);
19552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        vbb.order(ByteOrder.nativeOrder());
19652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mColorBuffer = vbb.asFloatBuffer();
19752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mColorBuffer.put(colors);
19852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mColorBuffer.position(0);
19952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
20052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
20152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mIndexBuffer.put(indices);
20252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        mIndexBuffer.position(0);
20352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    }
20452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
20552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    public void onAccuracyChanged(Sensor sensor, int accuracy) {
20652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    }
20752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
20852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    public void onSensorChanged(SensorEvent event) {
20952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        int type = event.sensor.getType();
21052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        float[] data;
21152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        if (type == Sensor.TYPE_ACCELEROMETER) {
21252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project            data = mGData;
21352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        } else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
21452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project            data = mMData;
21552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        } else {
21652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project            // we should not be here.
21752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project            return;
21852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        }
21952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        for (int i=0 ; i<3 ; i++)
22052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project            data[i] = event.values[i];
22152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
22252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        SensorManager.getRotationMatrix(mR, mI, mGData, mMData);
22352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project// some test code which will be used/cleaned up before we ship this.
22452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project//        SensorManager.remapCoordinateSystem(mR,
22552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project//                SensorManager.AXIS_X, SensorManager.AXIS_Z, mR);
22652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project//        SensorManager.remapCoordinateSystem(mR,
22752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project//                SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, mR);
22852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        SensorManager.getOrientation(mR, mOrientation);
22952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        float incl = SensorManager.getInclination(mI);
23052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project
23152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        if (mCount++ > 50) {
23252d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project            final float rad2deg = (float)(180.0f/Math.PI);
23352d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project            mCount = 0;
23452d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project            Log.d("Compass", "yaw: " + (int)(mOrientation[0]*rad2deg) +
23552d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project                    "  pitch: " + (int)(mOrientation[1]*rad2deg) +
23652d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project                    "  roll: " + (int)(mOrientation[2]*rad2deg) +
23752d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project                    "  incl: " + (int)(incl*rad2deg)
23852d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project                    );
23952d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project        }
24052d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project    }
24152d4c30ca52320ec92d1d1ddc8db3f07f69c4f98The Android Open Source Project}
242