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

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

问题描述

我正在尝试转换地磁和加速度计以在opengl ES1中旋转相机,我发现了一些来自android的代码并为iPhone更改了此代码,实际上它或多或少都有效,但是有一些错误,我我无法找到这个错误,我把代码,也调用了Opengl Es1: glLoadMatrixf((GLfloat *)矩阵);

   - (void)GetAccelerometerMatrix:(GLfloat *)矩阵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];


//清除矩阵,用于根据重力向量将当前参照值旋转为一个
bzero(matrix,sizeof(matrix));



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

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


float Hx = Ey * Az - Ez * Ay;
浮动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;

浮动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;
矩阵[4] = Mx; matrix [5] =我的; matrix [6] = Mz; matrix [7] = 0;
矩阵[8] = Ax; matrix [9] = Ay; matrix [10] = Az; matrix [11] = 0;
矩阵[12] = 0; matrix [13] = 0; matrix [14] = 0; matrix [15] = 1;


}

非常感谢您的帮助。



编辑:iPhone在横向上是永久性的,我知道出现问题,因为在Opengl Es中绘制的对象出现了两次。

解决方案

我无法发现所发布代码的任何问题,并建议问题出在其他地方。如果它有帮助,我对发布的代码的分析是:



处理_geomagnetic 0-5的前六行影响一个非常简单的低频滤波器,它假设你定期调用这个方法。所以你最终得到了一个磁力计矢量版本,希望能够去除高频抖动。



bzero将结果归零,准备积累。



声明和分配给Hz的线路采用磁力计和加速度计矢量并执行交叉乘积。因此,H(x,y,z)现在是与加速度计(假设为向下)和磁力计(将向前+向上)成直角的向量。调用侧向量。



invH和invA的东西,直到invA乘以Az,确保侧面和加速度计/向下矢量具有单位长度。然后创建M(x,y,z)作为侧向和向下矢量的叉积(即,与这两个矢量成直角的矢量)。所以它给出了前向量。



最后,这三个向量用于填充矩阵,利用了正交3x3矩阵的逆是其转置的事实(虽然这有点被事物的布局隐藏 - 注意数组索引)。你实际上直接在矩阵中设置了所有内容,所以在纯结果条件下不需要bzero。



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


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;


}

Thank you very much for the help.

Edit: 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:

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.

The bzero zeroes the result, ready for accumulation.

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.

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) 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.

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 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天全站免登陆