Android的传感器对OpenGL [英] Android Sensors for OpenGL

查看:304
本文介绍了Android的传感器对OpenGL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取Android传感器与OpenGL的工作,以旋转的OpenGL的摄像头无论手机指向。

I want to get the android sensors to work with opengl to rotate the opengl's camera to wherever the phone is pointing to.

要详细说明:如果玩家在看着东,我想对OpenGL的摄像头指向东太上的游戏;如果玩家指向天空,我想指出了OpenGL的摄像头向天空等。

To elaborate: if the player is looking at east, I'd like to the opengl's camera points east too on the game; if the player points to the sky, I'd like to point the opengl's camera to the sky and so on.

我已经试过用 getRotationMatrix 并加载了OpenGL的矩阵,但它只能在上下方向的,如果我把电池的两侧,有是没有区别的。这是我做了什么至今(应用程序是在横向模式下):

I've tried using getRotationMatrix and load the matrix on the opengl but it only works on the up-down direction, if I turn the cell to the sides, there is no difference. Here is what I've done so far (the app is in landscape mode):

    public void onSensorChanged(SensorEvent evt) {
    int type=evt.sensor.getType();
    float alpha = 0.8f;

    //Smoothing the sensor data a bit
    if (type == Sensor.TYPE_MAGNETIC_FIELD) {
      geomag[0]=geomag[0]*alpha+evt.values[0]*(1-alpha);
      geomag[1]=geomag[1]*alpha+evt.values[1]*(1-alpha);
      geomag[2]=geomag[2]*alpha+evt.values[2]*(1-alpha);
    } else if (type == Sensor.TYPE_ACCELEROMETER) {
      gravity[0]= gravity[0]*alpha+evt.values[0]*(1-alpha);
      gravity[1]= gravity[1]*alpha+evt.values[1]*(1-alpha);
      gravity[2]= gravity[2]*alpha+evt.values[2]*(1-alpha);
    }

    if ((type==Sensor.TYPE_MAGNETIC_FIELD) || (type==Sensor.TYPE_ACCELEROMETER)) {
      rotationMatrix = new float[16];
      SensorManager.getRotationMatrix(rotationMatrix, null, gravity, geomag);
      SensorManager.remapCoordinateSystem( 
        rotationMatrix, 
        SensorManager.AXIS_Y, 
        SensorManager.AXIS_MINUS_X, 
        rotationMatrix );
     if (AOGLRenderer.rotationmatrix==null) AOGLRenderer.rotationmatrix = new float[16];
     AOGLRenderer.rotationmatrix = rotationMatrix;
    }       
  }

而在OpenGL的code:

and in the opengl code:

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | // OpenGL docs.
                       GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();
    if (rotationmatrix!=null) gl.glLoadMatrixf(rotationmatrix, 0);
    gl.glPushMatrix();
    gl.glTranslatef(0, 0, -70);
    g.draw(gl);
    gl.glPopMatrix();
    alpha+=2f;
    gl.glTranslatef(0, -15, 0);
    c.draw(gl);

我想,有人究竟是谁做的可以分享他们的code!我真的AP preciate吧!谢谢大家!

I'd like that someone who actually did it could share their code! I'd really appreciate it! Thank you all!

推荐答案

你提供的倾斜度矩阵 - 这是不正确

You're supplying null for the inclination matrix - that's not correct.

SensorManager.getRotationMatrix(rotationMatrix, null, gravity, geomag);

有很多例子就如何使用的SensorManager的 getRotationMatrix(...)

There are lots of examples on how to use the SensorManager's getRotationMatrix(...).

float[] R = new float[16];
float[] I = new float[16];

if (SensorManager.getRotationMatrix(R, I, accelerometerValues, geomagneticValues)) {
    float[] anglesInRadians = new float[3];
    SensorManager.getOrientation(R, anglesInRadians);
    ...
}

也的旋转的OpenGL的摄像头无论手机指向的是比较模糊的。举例来说,如果你的意思是某种形式的增强现实的方法,那么你应该映射 AXIS_X AXIS_Z 。请注意,重映射可能甚至是必要的,如当你已经确定你的活动为横向模式。更多细节这里

Also "rotate the opengl's camera to wherever the phone is pointing to" is rather ambiguous. For instance, if you meant some sort of a augmented reality approach, then you should be mapping AXIS_X to AXIS_Z. Note that remapping might not even be necessary, e.g. when you already fix your activity to landscape mode. More details here.

一些例如codeS涉及到的传感器数据和OpenGL ES:

Some example codes involving sensor data and OpenGL ES:

  • <一个href="http://www.netmite.com/android/mydroid/cupcake/development/samples/Compass/src/com/example/android/compass/CompassActivity.java"相对=nofollow> CompassActivity
  • <一个href="http://stackoverflow.com/questions/2881128/how-to-use-onsensorchanged-sensor-data-in-combination-with-opengl">How要结合使用onSensorChanged传感器数据与OpenGL的

这篇关于Android的传感器对OpenGL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆