Android sensors for OpenGL

I want Android sensors to work with opengl to rotate the opengl camera where the phone points.

To clarify: if the player is looking east, I would like the opengl points of the camera east to be in the game too; if the player points to the sky, I would like to point the opengl camera to the sky, etc.

I tried using getRotationMatrix and loading the matrix into opengl, but it only works in the up direction, if I rotate the cell to the sides, there is no difference. Here is what I have done so far (the application 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; } } 

and in 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); 

I would like anyone who really did this to share their code! I would really appreciate it! Thanks everyone!

+4
source share
2 answers

You supply null for the tilt matrix - this is not true.

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

There are many examples of how to use the SensorManager 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); ... } 

In addition, โ€œrotating the opengl camera wherever the phone points toโ€ is rather ambiguous. For example, if you had in mind some kind of augmented reality approach, then you should map AXIS_X to AXIS_Z . Note that reassignment may not even be necessary, for example. when you are already recording your activity in landscape mode. More details here .

Some sample codes containing sensor data and OpenGL ES:

0
source

This is how I did the same (my application was also in the landscape). First, I get the orientation values โ€‹โ€‹(similar to how you do it):

 final float pi = (float) Math.PI; final float rad2deg = 180/pi; public static float x; //pitch public static float y; //roll public static float z; //azimuth float[] gravity = new float[3]; float[] geomag = new float[3]; float[] inOrientMatrix = new float[16]; float[] outOrientMatrix= new float[16]; float orientation[] = new float[3]; public static GLSurfaceView glView; // (...) public void onSensorChanged(SensorEvent event) { switch (event.sensor.getType()){ case Sensor.TYPE_ACCELEROMETER: gravity = event.values.clone(); break; case Sensor.TYPE_MAGNETIC_FIELD: geomag = event.values.clone(); break; } if (gravity != null && geomag != null){ if (SensorManager.getRotationMatrix(inOrientMatrix, null, gravity, geomag)){ SensorManager.remapCoordinateSystem(inOrientMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, outOrientMatrix); SensorManager.getOrientation(outOrientMatrix, orientation); x = orientation[1]*rad2deg; //pitch y = orientation[0]*rad2deg; //azimuth z = orientation[2]*rad2deg; //roll glView.requestRender(); ๏ฝ 

Then in my rendering in onDrawFrame (GL10 gl) I do:

  gl.glLoadIdentity(); GLU.gluLookAt(gl, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f); gl.glRotatef(MainActivity.x, 1.0f, 0.0f, 0.0f); gl.glRotatef(MainActivity.y, 0.0f, 1.0f, 0.0f); gl.glRotatef(MainActivity.z, 0.0f, 0.0f, 1.0f); //in case you have transformation, eg. the position of the object, you can do them here //gl.Translatef(0.0f, 0.0f, -DISTANCE NORTH); //gl.Translatef(0.0f, DISTANCE EAST, 0.0f); gl.glPushMatrix(); //object draw 
In other words, I rotate the whole world around me. It would be best to change the direction of the camera with glLookAt, with the eye located at (0,0,0) and (0,1,0) being a vector up, but I just could not get my center_view x, y, z right.

Hope this helps ...

0
source

Source: https://habr.com/ru/post/1387977/


All Articles