Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> 使用OpenGL開發一個指南針

使用OpenGL開發一個指南針

編輯:初級開發

在Android設備中具備了很多新的特性,比如各種感應器,GL圖形庫支持。在大部分Android設備中都有磁力感應器,相對於重力感應而言它可以感應出方向,今天android123就以Google的Samples的指南針例子和大家說下OpenGL的指南針的實現。

 public class CompassActivity extends Activity implements Renderer, SensorEventListener {
    private GLSurfaceView mGLSurfaceView; //GL VIEw
    private SensorManager mSensorManager; 
    private float[] mGData = new float[3];
    private float[] mMData = new float[3];
    private float[] mR = new float[16];
    private float[] mI = new float[16];
    private FloatBuffer mVertexBuffer;
    private FloatBuffer mColorBuffer;
    private ByteBuffer mIndexBuffer;
    private float[] mOrIEntation = new float[3];
    private int mCount;

    public CompassActivity() {
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        mGLSurfaceView = new GLSurfaceVIEw(this);
        mGLSurfaceVIEw.setRenderer(this);
        setContentView(mGLSurfaceVIEw);
    }

    @Override
    protected void onResume() { //Activity切換到前端時觸發
 
        super.onResume();
        mGLSurfaceVIEw.onResume();
        Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //加速感應器
        Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); //磁力感應器
        mSensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onPause() { //Activity切換到後台時觸發
 
        super.onPause();
        mGLSurfaceVIEw.onPause();
        mSensorManager.unregisterListener(this); //主要是為了省電
    }

    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0, 0, -2);

        gl.glMultMatrixf(mR, 0);
        gl.glVertexPointer(3, GL_FLOAT, 0, mVertexBuffer);
        gl.glColorPointer(4, GL_FLOAT, 0, mColorBuffer);
        gl.glDrawElements(GL_LINES, 6, GL_UNSIGNED_BYTE, mIndexBuffer);
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glVIEwport(0, 0, width, height);

        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        gl.glDisable(GL10.GL_DITHER);

  
        gl.glClearColor(1,1,1,1);
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glEnable(GL10.GL_DEPTH_TEST);

        gl.glEnableClIEntState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClIEntState(GL10.GL_COLOR_ARRAY);

        float vertices[] = {
                0,0,0,
                1,0,0,
                0,1,0,
                0,0,1
        };
        float colors[] = {
                0,0,0,0,
                1,0,0,1,
                0,1,0,1,
                0,0,1,1
        };
        byte indices[] = { 0, 1, 0, 2, 0, 3 };

        ByteBuffer vbb;
        vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asFloatBuffer();
        mVertexBuffer.put(vertices);
        mVertexBuffer.position(0);

        vbb = ByteBuffer.allocateDirect(colors.length*4);
        vbb.order(ByteOrder.nativeOrder());
        mColorBuffer = vbb.asFloatBuffer();
        mColorBuffer.put(colors);
        mColorBuffer.position(0);

        mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
        mIndexBuffer.put(indices);
        mIndexBuffer.position(0);
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public void onSensorChanged(SensorEvent event) { //實現SensorEventListener 接口
        int type = event.sensor.getType();
        float[] data;
        if (type == Sensor.TYPE_ACCELEROMETER) {
            data = mGData;
        } else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
            data = mMData;
        } else {
            return;
        }
        for (int i=0 ; i<3 ; i++)
            data[i] = event.values[i];

        SensorManager.getRotationMatrix(mR, mI, mGData, mMData);

        SensorManager.getOrientation(mR, mOrIEntation);
        float incl = SensorManager.getInclination(mI);

        if (mCount++ > 50) {
            final float rad2deg = (float)(180.0f/Math.PI);
            mCount = 0;
            Log.d("Compass", "yaw: " + (int)(mOrIEntation[0]*rad2deg) +
                    "  pitch: " + (int)(mOrIEntation[1]*rad2deg) +
                    "  roll: " + (int)(mOrIEntation[2]*rad2deg) +
                    "  incl: " + (int)(incl*rad2deg)
                    );
        }
    }
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved