4

我尝试在相机预览顶部创建一个带有 opengl 视图的增强现实应用程序。我获得了相机预览和 opengl 视图,我从传感器管理器收集传感器数据,然后创建旋转矩阵以将其应用于我的 opengl 视图。到目前为止,一切都很好。我只使用横向手机方向。但这是我的问题:

我正在使用来自传感器的旋转矩阵,将其应用于 opengl,然后我想绘制我的模型,这些模型在 (0,0,0) 点周围不断移动。但是当我旋转我的手机时,我当然可以跟随我的对象移动,但是当我改变方向时(假设我向右旋转我的手机,然后将它改为相反 - 左。此时我的对象应该在他的路径上移动,但是他呆在原地,几乎没有后退。

我的代码和手机屏幕:

 private float[] rotationMatrix = new float[16];

onDrawFrame:
moveObjects(smoothedDeltaRealTime_ms);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glMultMatrixf(rotationMatrix, 0);
gl.glRotatef(90.0f, 1f, 0f, 0f);
drawObjects(gl);

 @Override
public void onSurfaceChanged(GL10 gl, int width, int height) {

    ArOpenGlLogger.info("onSurfaceChanged " + width + "x" + height);

    this.screenWidth = width;
    this.screenHeight = height;

    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Set color's clear-value 
    gl.glClearDepthf(1.0f); // Set depth's clear-value to farthest
    gl.glEnable(GL10.GL_DEPTH_TEST);   // Enables depth-buffer for hidden surface removal
    gl.glDepthFunc(GL10.GL_LEQUAL);      // The type of depth testing to do

    gl.glViewport(0, 0, width, height);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();

    GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();

    gl.glShadeModel(GL10.GL_SMOOTH);    // Enable smooth shading of color
    gl.glDisable(GL10.GL_DITHER);   // Disable dithering for better performance
    gl.glEnable(GL10.GL_TEXTURE_2D);    // Enable texture
}

  protected void draw(GL10 gl) {

    //loading textures
    if(!isTextureLoaded) loadTexture(gl);

    gl.glTranslatef(translation.getX(), translation.getY(), translation.getZ());
    gl.glScalef(scale.getX(), scale.getY(), scale.getZ());

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);  
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); 

    gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length / 3);

    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

}

传感器:

  rootMeanSquareBuffer(bufferedAccelerometrData, accelerometrData);
        rootMeanSquareBuffer(bufferedMagneticFieldData, magneticFieldData);
        SensorManager.getRotationMatrix(rotationMatrix, null, bufferedAccelerometrData, bufferedMagneticFieldData);

        if(deviceOrientation == ORIENTATION_LANDSCAPE) {

            float[] result = new float[16];
            SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, result);

            sensorCallback.onNewRotationMatrix(result);
        }
        else sensorCallback.onNewRotationMatrix(rotationMatrix);

  private void rootMeanSquareBuffer(float[] target, float[] values) {

      final float amplification = 200.0f;
      float buffer = 20.0f;

      target[0] += amplification;
      target[1] += amplification;
      target[2] += amplification;
      values[0] += amplification;
      values[1] += amplification;
      values[2] += amplification;

      target[0] = (float) (Math.sqrt((target[0] * target[0] * buffer + values[0] * values[0]) / (1 + buffer)));
      target[1] = (float) (Math.sqrt((target[1] * target[1] * buffer + values[1] * values[1]) / (1 + buffer)));
      target[2] = (float) (Math.sqrt((target[2] * target[2] * buffer + values[2] * values[2]) / (1 + buffer)));

      target[0] -= amplification;
      target[1] -= amplification;
      target[2] -= amplification;
      values[0] -= amplification;
      values[1] -= amplification;
      values[2] -= amplification;
}

设备屏幕: http://i.imgur.com/JJIyohL.png

4

0 回答 0