Search code examples
opengl-es-2.0augmented-realityandroid-sensorsrotational-matrices

Sensor Orientiation -> GLRotation doesn't work properly


I want to use the Android orientation sensor data for my GLES camera - giving it the rotation matrix. I found a very good example here:

How to use onSensorChanged sensor data in combination with OpenGL

but this is only working with GL1.0 and I need to work on it for GLES2.0. Using my own shaders, everything works, moving the camera manuall is fine. But the moment I use the rotation matrix like in the example, it doesn't really work.

I generate the rotation matrix with:

SensorManager.getRotationMatrix(rotationMatrix, null, bufferedAccelGData, bufferedMagnetData);

My application is running in LANDSCAPe so I use that methode after (like in the example code):

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

It worked fine on my phone in his code but not in mine. My screen looks like that:

enter image description here

The rotation matrix seems to be rotated 90° to the right (almost as if I have forgotten to switch to landscape for my activity).

I was thinking of using the remap() method in a wrong way but in the example it makes sense, the camera movement works now. If I rotate to the left, the screen rotates to the left as well, even though, since everything is turned, it rotates "up" (compared to the ground, which is not on the bottom but on the right). It just looks like I made a wall instead of a ground but I'm sure my coordinates are right for the vertices.

I took a look ath the draw method for the GLSurface and I don't see what I might have done wrong here:

GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
MatrixStack.glLoadMatrix(sensorManager.getRotationMatrix());  // Schreibt die MVMatrix mit der ogn. Rotationsmatrix
GameRenderer.setPerspMatrix(); // Schreibt die Perspektivmatrix Uniform für GLES. Daran sollte es nicht liegen.
MatrixStack.mvPushMatrix();
  drawGround();    
MatrixStack.mvPopMatrix();

As I said, when moving my camera manually everything works perfect. So what is wrong with the rotation matrix I get?


Solution

  • Well, okay, it was a very old problem but now that I took a look at the code again I found the solution. Having the phone in landscape I had to remap the axis using

    SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, R);
    

    But that still didn't rotate the image - even though the mapping of the Y and -X Axis worked fine. So simply using

    Matrix.rotateM(R, 0, 90, 1, 0, 0);
    

    Does the job. Not really nicely but it works.

    I know it was a very old question and I don't see why I made this mistake but perhaps anyone else has the same problem one day.

    Hope this helps, Tobias