用于 OpenGL 的 Android 传感器

编程入门 行业动态 更新时间:2024-10-26 22:27:05
本文介绍了用于 OpenGL 的 Android 传感器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想让 android 传感器与 opengl 一起使用,以将 opengl 的摄像头旋转到手机指向的任何位置.

I want to get the android sensors to work with opengl to rotate the opengl's camera to wherever the phone is pointing to.

详细说明:如果玩家在看东方,我想在游戏中opengl的相机也指向东方;如果玩家指向天空,我想将opengl的相机指向天空等等.

To elaborate: if the player is looking at east, I'd like to the opengl's camera points east too on the game; if the player points to the sky, I'd like to point the opengl's camera to the sky and so on.

我尝试使用 getRotationMatrix 并在 opengl 上加载矩阵,但它只适用于上下方向,如果我将单元格转到两侧,则没有区别.这是我到目前为止所做的(应用程序处于横向模式):

I've tried using getRotationMatrix and load the matrix on the opengl but it only works on the up-down direction, if I turn the cell to the sides, there is no difference. Here is what I've done so far (the app 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; } }

在opengl代码中:

and in the 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'd like that someone who actually did it could share their code! I'd really appreciate it! Thank you all!

推荐答案

这是我做同样事情的方式(我的应用程序也是横向的).首先我得到方向值(类似于你做的方式):

Here is the way I did the same thing (my App was also in landscape). First I get the orientation values (similar to the way you do):

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(); }

然后在我的渲染中,在 onDrawFrame(GL10 gl) 我做:

Then in my rendered, in the 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

换句话说,我围绕着我旋转整个世界.更好的方法是使用 glLookAt 更改相机的方向,眼睛位于 (0,0,0) 和 (0,1,0) 是向上向量,但我根本无法获得我的 center_view x,y,z 值正确.

In other words, I rotate the whole world around me. A better way would be to change the direction of the camera using glLookAt, with the eye positioned at (0,0,0) and (0,1,0) being the up vector, but I simply couldn't get my center_view x,y,z values correctly.

希望这会有所帮助...

Hope this helps...

更多推荐

用于 OpenGL 的 Android 传感器

本文发布于:2023-11-24 05:45:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624144.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:传感器   OpenGL   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!