我正在开发一个小型 OpenGL 演示,它读取物理传感器的方向(以四元数格式)并因此旋转屏幕上的对象。现在我正在尝试将四元数形式的旋转转换为旋转 4x4 矩阵。我在网上找到了一个小算法可以做到这一点。但是,我很难将四元数处理成矩阵,因为我不明白如何在 C++ 函数周围传递数组。
所以我正在尝试这样做:
#include "MatrixMath.h"
// somewhere in my render loop
float aRotationMatrix[16];
readRotation(aRotationMatrix);
readRotation
函数定义为:
void readRotation(float *a4x4Flat16Matrix){
sQuaternionReader.read();
float aQuaternion[4];
aQuaternion[0] = sQuaternionReader.getQuarternionX();
aQuaternion[1] = sQuaternionReader.getQuarternionY();
aQuaternion[2] = sQuaternionReader.getQuarternionZ();
aQuaternion[3] = sQuaternionReader.getQuarternionW();
float *aMatrix = a4x4Flat16Matrix;
fromQuaternionToMatrix(aQuaternion, aMatrix); // this is declared faulty by the linker
}
当我尝试编译时,链接器给了我这个错误:
./src/main/CupNPlate.cpp:151: error: undefined reference to 'fromQuaternionToMatrix(float*, float*)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [CupNPlate] Error 1
该fromQuaternionToMatrix
函数在名为“MatrixMath.h”的文件中声明:
#ifndef MATRIXMATH_H_
#define MATRIXMATH_H_
void fromQuaternionToMatrix( float *aQuaternion,
float *a4x4Flat16Matrix );
#endif
并在名为“MatrixMath.cpp”的文件中实现:
#include "MatrixMath.h"
void fromQuaternionToMatrix( float *aQuaternion,
float *a4x4Flat16Matrix ){
// x := 0, y := 1, z := 2, w := 3
float x2 = aQuaternion[0] * aQuaternion[0];
float y2 = aQuaternion[1] * aQuaternion[1];
float z2 = aQuaternion[2] * aQuaternion[2];
float xy = aQuaternion[0] * aQuaternion[1];
float xz = aQuaternion[0] * aQuaternion[2];
float yz = aQuaternion[1] * aQuaternion[2];
float wx = aQuaternion[3] * aQuaternion[0];
float wy = aQuaternion[3] * aQuaternion[1];
float wz = aQuaternion[3] * aQuaternion[2];
a4x4Flat16Matrix[0] = 1.0f - 2.0f * (y2 + z2);
a4x4Flat16Matrix[1] = 2.0f * (xy - wz);
a4x4Flat16Matrix[2] = 2.0f * (xz + wy);
a4x4Flat16Matrix[3] = 0.0f;
a4x4Flat16Matrix[4] = 2.0f * (xy + wz);
a4x4Flat16Matrix[5] = 1.0f - 2.0f *(x2 + z2);
a4x4Flat16Matrix[6] = 2.0f * (yz - wx);
a4x4Flat16Matrix[7] = 0.0f;
a4x4Flat16Matrix[8] = 2.0f * (xz - wy);
a4x4Flat16Matrix[9] = 2.0f * (yz + wx);
a4x4Flat16Matrix[10] = 1.0f - 2.0f * (x2 + y2);
a4x4Flat16Matrix[11] = 0.0f;
a4x4Flat16Matrix[12] = 0.0f;
a4x4Flat16Matrix[13] = 0.0f;
a4x4Flat16Matrix[14] = 0.0f;
a4x4Flat16Matrix[15] = 1.0f;
}
链接器告诉我undefined reference to 'fromQuaternionToMatrix(float*, float*)'
,但我的函数的签名完全是fromQuaternionToMatrix( float *aQuaternion,
float *a4x4Flat16Matrix )
,所以我不明白它在抱怨什么。编译器没有给我任何错误。