Orientación del sensor -> GLRotation no funciona correctamente

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:

Cómo utilizar los datos del sensor onSensorChanged en combinación con 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).

Estaba pensando en usar el 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?

preguntado el 31 de julio de 12 a las 13:07

besides here is a post that looks liky my problem, I think. Perhaps its easier to understand there: stackoverflow.com/questions/9342283/… -

3 Respuestas

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

Respondido el 04 de junio de 14 a las 16:06

If it is (was) working on a specific phone but not on yours, I guess the android version may play a role here. We faced the issue in mixare Augmented Reality Engine where a Surfaceview is superimposed over a Camera view. Pleas consider that the information here may not apply to your case since we are not using OpenGL.

Modern version of android will return a default orientation, whereas previously portrait was the default. You can check how we query this in the Compatibility class. This information is then used to apply different values to the RemapCoordinateSystem call check lines 739 and onwards of this file. Mixare is as well using landscape mode by default, so I guess our values for the remapping should apply to your case just as well. As I said earlier, we are using the 3x3 matrices, since we are not using OpenGL, but I guess this should be the same for OpenGL compatible matrices.

Respondido 31 Jul 12, 13:07

Hey thank you for your answer. The thing is, it is working on my device but with OpenGL 1.0 but when I just changed everything to GL2.0 (GLES) it didn't work any longer (or simply turned the whole image to the right). I am aware of the orientation change. Since I use it only in Landscape I did exactly the same you do: SensorManager.AXIS_X, SensorManager.AXIS_MINUS_Z but as you can see in the screenshot suddenly everything is rotated to the right... weird. But thank you for trying at least! :-) - tobias reich

Oh and by the way I don't think it is only a GL2.0 problem since it appears to be the same problem here: stackoverflow.com/questions/9342283/… which is made in GL1.1 :-( - tobias reich

Well, I got an idea. At desarrollador.android.com/reference/android/hardware/… is mentioned: "The axes are not swapped when the device's screen orientation changes." So I assume this is the problem. Has anyone any idea how to change that since I have Landscape mode not Portrait? I'd like to try this. Thank you very much! - tobias reich

Take time, play with the orientation matrix, you will find a column that contains useful vaule.

Besides Log vales for each column, see which one is useful, try quaternions, keep playing with values never try the code directly in renderer, first check the values

Because later, you will have more options for input like touch, there too you have to test the values, play with them, use sensitivity constants with matrices too

Respondido 05 ago 12, 17:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.