【问题标题】:Augmented reality with Android sensors and OpenGL使用 Android 传感器和 OpenGL 的增强现实
【发布时间】:2013-03-15 17:46:34
【问题描述】:

图像总是比大量文字说话,这就是我想要做的:

圆圈中心是用户的手机位置(原点)。该应用程序显示一个自定义的相机视图,它还显示一个 OpenGL 场景(取决于您正在查看的位置)。 OpenGL 场景仅由一个简单的立方体组成,当用户朝正确的方向看时,立方体就会被渲染。

我对 OpenGL 还很陌生,我已经实现了在摄像机前显示立方体,但它是静态的:我无法在 360° 视图中移动。

我从传感器获得设备的方向:

    int type = event.sensor.getType();
    float[] data;
    if (type == Sensor.TYPE_ACCELEROMETER) {
        mGData = data;
    } else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
        mMData = data;
    } else {
        // we should not be here.
        return;
    }
    for (int i=0 ; i<3 ; i++)
        data[i] = event.values[i];

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

据我了解,3 个同时正交的旋转角度存储在 mOrientation 中。但是然后呢?我想在onDrawFrame 方法中制作类似GLU.lookAt(0, 0, 0, X, Y, Z, ?, ?, ?) 的东西,但它没有用。我想做那个家伙说他做不到的东西(见这里的最后一段:https://stackoverflow.com/a/9114246/1304830)。

这是我在onDrawFram 中使用的代码:

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL10.GL_MODELVIEW);

    // Look in a direction (with the sensors)
    gl.glLoadIdentity();
    GLU.gluLookAt(gl, 0.0f, 0.0f, 0.0f, ?, ?, ?, ?, ?, ?); // Where I need help

    gl.glPushMatrix();  

    // Place the cube in the scene
    gl.glTranslatef(0, 0, -5);
    mCube.draw(gl);

感谢您的帮助

【问题讨论】:

  • 好吧,我自己从来没有尝试过这个,但是曾经想过如果我将旋转和方向矩阵按原样用作模型或视图矩阵会发生什么。

标签: android opengl-es opengl-es-2.0


【解决方案1】:

这将做那个人试图做的事情,但他的想法的问题是向上向量总是指向 y 轴。因此,如果您滚动手机,相机将不会随之滚动。

float pi = (float) Math.PI;
float rad2deg = 180/pi;

// Get the pitch, yaw and roll from the sensor. 

float yaw = orientation[0] * rad2deg;
float pitch = orientation[1] * rad2deg;
float roll = orientation[2] * rad2deg;

// Convert pitch, yaw and roll to a vector

float x = (float)(Math.cos( yaw ) * Math.cos( pitch ));
float y = (float)(Math.sin( yaw ) * Math.cos( pitch ));
float z = (float)(Math.sin( pitch ));

GLU.gluLookAt( gl, 0.0f, 0.0f, 0.0f, x, y, z, 0.0f, 1.0f, 0.0f );  

使用三个 glRotates 是 IMO 更好的选择,除非您出于某种原因想要锁定滚动。

注意:我不确定 Android 会相对于手机屏幕调用哪个方向,因此我的偏航、俯仰和滚动配置可能有误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 2013-02-27
    相关资源
    最近更新 更多