在 iPhone 中为 Opengl Es 获取定位设备 [英] Get orientation device in the iPhone for Opengl Es

查看:33
本文介绍了在 iPhone 中为 Opengl Es 获取定位设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换地磁和加速度计以在 opengl ES1 中旋转相机,我从 android 中找到了一些代码并为 iPhone 更改了此代码,实际上它或多或少地工作,但有一些错误,我'我找不到这个错误,我把代码,还有对Opengl Es1的调用:glLoadMatrixf((GLfloat*)matrix);

I'm trying to convert the geomagnetic and accelerometer to rotate the camera in opengl ES1, I found some code from android and changed this code for iPhone, actually it is working more or less, but there are some mistakes, I´m not able to find this mistake, I put the code, also the call to Opengl Es1: glLoadMatrixf((GLfloat*)matrix);

- (void) GetAccelerometerMatrix:(GLfloat *) matrix headingX: (float)hx headingY:(float)hy headingZ:(float)hz;
{

    _geomagnetic[0] = hx * (FILTERINGFACTOR-0.05) + _geomagnetic[0] * (1.0 - FILTERINGFACTOR-0.5)+ _geomagnetic[3] * (0.55);
    _geomagnetic[1] = hy * (FILTERINGFACTOR-0.05) + _geomagnetic[1] * (1.0 - FILTERINGFACTOR-0.5)+ _geomagnetic[4] *  (0.55);
    _geomagnetic[2] = hz * (FILTERINGFACTOR-0.05) + _geomagnetic[2] * (1.0 - FILTERINGFACTOR-0.5)+ _geomagnetic[5] *  (0.55);

    _geomagnetic[3]=_geomagnetic[0] ;
    _geomagnetic[4]=_geomagnetic[1];
    _geomagnetic[5]=_geomagnetic[2];


        //Clear matrix to be used to rotate from the current referential to one based on the gravity vector
    bzero(matrix, sizeof(matrix));



    //MAGNETIC
    float Ex = -_geomagnetic[1];    
    float Ey =_geomagnetic[0];
    float Ez =_geomagnetic[2];

    //ACCELEROMETER
    float Ax=  -_accelerometer[0];
    float Ay=  _accelerometer[1] ;
    float Az=  _accelerometer[2] ;


    float Hx = Ey*Az - Ez*Ay;
    float Hy= Ez*Ax - Ex*Az;
    float Hz = Ex*Ay - Ey*Ax;

    float normH = (float)sqrt(Hx*Hx + Hy*Hy + Hz*Hz);


    float invH = 1.0f / normH;
    Hx *= invH;
    Hy *= invH;
    Hz *= invH;

    float invA = 1.0f / (float)sqrt(Ax*Ax + Ay*Ay + Az*Az);
    Ax *= invA;
    Ay *= invA;
    Az *= invA;

    float Mx = Ay*Hz - Az*Hy;
    float My = Az*Hx - Ax*Hz;
    float Mz = Ax*Hy - Ay*Hx;

    // if (mOut.f != null) {

    matrix[0]  = Hx;    matrix[1]  = Hy;    matrix[2]  = Hz;   matrix[3]  = 0;
    matrix[4]  = Mx;    matrix[5]  = My;    matrix[6]  = Mz;   matrix[7]  = 0;
    matrix[8]  = Ax;    matrix[9]  = Ay;    matrix[10] = Az;   matrix[11] = 0;
    matrix[12] = 0;     matrix[13] = 0;     matrix[14] = 0;    matrix[15] = 1;


}

非常感谢您的帮助.

iPhone 一直处于横向,我知道有问题,因为在 Opengl Es 中绘制的对象出现了两次.

The iPhone it is permantly in landscape orientation and I know that something is wrong because the object painted in Opengl Es appears two times.

推荐答案

我找不到发布的代码有任何问题,并建议问题出在其他地方.如果有帮助,我对发布的代码的分析是:

I'm unable to find any problems with the code posted, and would suggest the problem is elsewhere. If it helps, my analysis of the code posted is that:

前六行,处理_geomagnetic 0–5,影响一个非常简单的低频过滤器,假设您定期调用该方法.所以你最终得到了磁力计矢量的一个版本,希望消除了高频抖动.

The first six lines, dealing with _geomagnetic 0–5, effect a very simple low frequency filter, which assumes you call the method at regular intervals. So you end up with a version of the magnetometer vector, hopefully with high frequency jitter removed.

bzero 将结果归零,准备好累加.

The bzero zeroes the result, ready for accumulation.

直到声明和分配给 Hz 的行采用磁力计和加速度计矢量并执行叉积.所以 H(x, y, z) 现在是一个与加速度计(假定为向下")和磁力计(向前+一些向上)成直角的向量.称之为边向量.

The lines down to the declaration and assignment to Hz take the magnetometer and accelerometer vectors and perform the cross product. So H(x, y, z) is now a vector at right angles to both the accelerometer (which is presumed to be 'down') and the magnetometer (which will be forward + some up). Call that the side vector.

invH 和 invA 的东西,直到 Az 乘以 invA 确保边向量和加速度计/向下向量是单位长度.

The invH and invA stuff, down to the multiplication of Az by invA ensure that the side and accelerometer/down vectors are of unit length.

M(x, y, z) 然后被创建,作为侧向和向下向量的叉积(即,与这两个向量成直角的向量).所以它给出了前向量.

M(x, y, z) is then created, as the cross product of the side and down vectors (ie, a vector at right angles to both of those). So it gives the front vector.

最后,三个向量用于填充矩阵,利用正交 3x3 矩阵的逆是它的转置这一事实(尽管这有点隐藏在事物的布局方式中 - 注意数组指数).您实际上直接在矩阵中设置了所有内容,因此在纯结果方面不需要 bzero.

Finally, the three vectors are used to populate the matrix, taking advantage of the fact that the inverse of an orthonormal 3x3 matrix is its transpose (though that's sort of hidden by the way things are laid out — pay attention to the array indices). You actually set everything in the matrix directly, so the bzero wasn't necessary in pure outcome terms.

glLoadMatrixf 然后是正确的使用方法,因为这是在 OpenGL ES 1.x 中乘以任意列主矩阵的方式.

glLoadMatrixf is then the correct thing to use because that's how you multiply by an arbitrary column-major matrix in OpenGL ES 1.x.

这篇关于在 iPhone 中为 Opengl Es 获取定位设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆