Question

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.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top