简体   繁体   中英

How do you convert quaternion/accelerometer data to opengl usable data

I have some data from a sensor device, ie, accelerometer x,y,z and a quaternion. Using this information i would like to render a "line image" in an Android OpenGL View. Can someone help me about the part on how to convert acceleration values to something that can be used by OpenGL glTranslatef and glRotatef function's.

You shouldn't be using the deprecate gltranslate or glrotate. instead just manage the correct transformation matrices directly.

converting a quaternion to matrix can be done with the following: (sourced from my previous code)

float[] quatToMat(quaternion q, float* result)
{
    //based on algorithm on wikipedia
    // http://en.wikipedia.org/wiki/Rotation_matrix#Quaternion
    float w = q.scalar ();
    float x = q.x();
    float y = q.y();
    float z = q.z();

    float n =  x*x + y*y + z*z + w*w;
    float s =  n == 0?  0 : 2 / n;
    float wx = s * w * x, wy = s * w * y, wz = s * w * z;
    float xx = s * x * x, xy = s * x * y, xz = s * x * z;
    float yy = s * y * y, yz = s * y * z, zz = s * z * z;

    return new float[]{ 1 - (yy + zz),         xy + wz ,         xz - wy ,0,
                             xy - wz ,    1 - (xx + zz),         yz + wx ,0,
                             xz + wy ,         yz - wx ,    1 - (xx + yy),0,
                             0 ,               0 ,               0 ,1  };
}

If you still want to use the fixed function pipeline then push that into glMultMatrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM