开发者

Using SensorManager to rotate in OpenGL?

开发者 https://www.devze.com 2023-04-01 15:27 出处:网络
I\'m building a pretty standard AR app that overlays stuff on the camera and am having trouble working with the SensorManager.I\'m basically trying to get the rotation matrix from the sensor manager a

I'm building a pretty standard AR app that overlays stuff on the camera and am having trouble working with the SensorManager. I'm basically trying to get the rotation matrix from the sensor manager and then call glMultMatrix to rotate everything accordingly. It looks like in the documentation for SensorManager.getRotationMatrix that this is possible. However when I do multiply with the rotation matrix everything just gets screwy and moves around not like you'd expect from seeing othe开发者_运维技巧r ar apps. I got some code below:

private float[] gravity = new float[3];
private float[] geomagnetic = new float[3];
private float filteringFactor = 0.01f;
private float[] rotationMatrixOut = new float[16];

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        geomagnetic[0] = event.values[0] * filteringFactor + geomagnetic[0] * (1.0f - filteringFactor);
        geomagnetic[1] = event.values[1] * filteringFactor + geomagnetic[1] * (1.0f - filteringFactor);
        geomagnetic[2] = event.values[2] * filteringFactor + geomagnetic[2] * (1.0f - filteringFactor);
    } else if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        gravity[0] = event.values[0] * filteringFactor + gravity[0] * (1.0f - filteringFactor);
        gravity[1] = event.values[1] * filteringFactor + gravity[1] * (1.0f - filteringFactor);
        gravity[2] = event.values[2] * filteringFactor + gravity[2] * (1.0f - filteringFactor);
    }

    SensorManager.getRotationMatrix(rotationMatrixOut, null, gravity, geomagnetic);
}

I have also tried using SensorManager.remapCoordinateSystem but with no luck. My code for drawing is below, it's pretty standard stuff, I commented out lookat cause I wasn't sure if that was causing trouble

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

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

    //GLU.gluLookAt(gl, mCamera.position.x, mCamera.position.y, mCamera.position.z, 
    //                mCamera.target.x, mCamera.target.y, mCamera.target.z,
    //                mCamera.up.x, mCamera.up.y, mCamera.up.z);

    gl.glMultMatrixf(rotationMatrixOut, 0);


    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

    gl.glLineWidth(1.0f);

    gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);

    gl.glDrawArrays(GL10.GL_LINES, 0, 2 * 201 * 2);

I been losing way too much sleep over this if anyone can help that be greatly appreciated. Thanks


Just guessing, but could it be, that the data you receive from SensorManager is in row major order? OpenGL expects the data in column major order, so if you have a row major order matrix passed to OpenGL it were interpreted transposed.

And you're right, you don't need gluLookAt there (assuming you, i.e. the device at the origin, and displaying the world around it).

0

精彩评论

暂无评论...
验证码 换一张
取 消